Question

i obtenu le contrôle RichTextBox dans la forme et un fichier texte. Je reçois un fichier texte à un tableau et d'obtenir RichTextBox1.Text d'autre tableau que comparer et compter les mots correspondant. Mais par exemple, il y a deux mots « nom » dans un fichier texte et trois « et » mot richtextbox .. Donc, s'il y a deux même mot dans un fichier texte dans richtextbox il ne peut pas être 3 ou plus après 2, il doit être bon mot si il ne doit pas être compté. Mais HashSet est compte des valeurs uniques ne recherche pas de doublons dans le fichier texte. Je veux comparer chaque mot dans un fichier texte avec des mots dans RichTextBox .. (Sorr pour mon anglais.)

Mes codes ici;

        StreamReader sr = new StreamReader("c:\\test.txt",Encoding.Default);
        string[] word = sr.ReadLine().ToLower().Split(' ');
        sr.Close();
        string[] word2 = richTextBox1.Text.ToLower().Split(' ');
        var set1 = new HashSet<string>(word);
        var set2 = new HashSet<string>(word2);
        set1.IntersectWith(set2);

        MessageBox.Show(set1.Count.ToString());
Était-ce utile?

La solution

Inférer que vous voulez:

fichier:

foo
foo
foo
bar

zone de texte:

foo
foo
bar
bar

pour aboutir à '3' (2 foos et une barre)

Dictionary<string,int> fileCounts = new Dictionary<string, int>();
using (var sr = new StreamReader("c:\\test.txt",Encoding.Default))
{
    foreach (var word in sr.ReadLine().ToLower().Split(' '))
    {
        int c = 0;
        if (fileCounts.TryGetValue(word, out c))
        {
            fileCounts[word] = c + 1;
        }
        else
        {
            fileCounts.Add(word, 1);
        }                   
    }
}
int total = 0;
foreach (var word in richTextBox1.Text.ToLower().Split(' '))
{
    int c = 0;
    if (fileCounts.TryGetValue(word, out c))
    {
        total++;
        if (c - 1 > 0)
           fileCounts[word] = c - 1;                
        else
            fileCounts.Remove(word);
    }
}
MessageBox.Show(total.ToString());

Notez que cela modifie destructivement le dictionnaire de lecture, vous pouvez éviter ce (ont donc qu'à lire le dictionnaire une fois) acheter comptant simplement la zone de texte riche de la même manière, puis prendre le Min des comptes individuels et en les sommant .

Autres conseils

Vous avez besoin des chefs d'accusation d'être le même? Vous devez compter les mots, puis ...

    static Dictionary<string, int> CountWords(string[] words) {
        // use (StringComparer.{your choice}) for case-insensitive
        var result = new Dictionary<string, int>();
        foreach (string word in words) {
            int count;
            if (result.TryGetValue(word, out count)) {
                result[word] = count + 1;
            } else {
                result.Add(word, 1);
            }
        }
        return result;
    }
        ...
        var set1 = CountWords(word);
        var set2 = CountWords(word2);

        var matches = from val in set1
                      where set2.ContainsKey(val.Key)
                         && set2[val.Key] == val.Value
                      select val.Key;
        foreach (string match in matches)
        {
            Console.WriteLine(match);
        }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top