Найдите несколько самых длинных слов и посчитайте, сколько раз они повторяются - PullRequest
0 голосов
/ 26 ноября 2018

Итак, я создал код, который находит самые длинные слова в двух текстовых файлах, и если это уникальное слово в первом текстовом файле, он записывает в файл.Но мне нужно найти уникальные слова в первом текстовом файле, а затем из этих уникальных слов найти 10 самых длинных слов.Затем эти 10 слов сортируются от самого длинного к короткому и подсчитывают, сколько раз оно встречается в первом текстовом файле.

        string[] LongestWrods(string[] longest1, string[] text2, int longestCount1, out int longestWordText, char[] @char)
    {
        string[] LongestWordsText1 = new string[10];
        longestWordText = 0;
        for (int i = 0; i < longestCount1; i++)
        {
            if (RepeatTimes(text2, longest1[i], @char) == 0)
                LongestWordsText1[longestWordText++] = longest1[i];
        }
        return LongestWordsText1;
    }

Ответы [ 2 ]

0 голосов
/ 26 ноября 2018

Таким образом:

class Program
{
    static void Main(string[] args)
    {
        List<string> wordsToCut = File.ReadAllLines("text2.txt").Distinct().ToList();

        List<UniqueWord> uniqueWords = new List<UniqueWord>();

        foreach (string word in File.ReadAllLines("text1.txt"))
        {
            if (wordsToCut.Contains(word) == false)
            {
                UniqueWord uniqueWord = uniqueWords.Where(x => x.Word == word).FirstOrDefault();

                if (uniqueWord != null)
                {
                    uniqueWord.Occurrence++;
                }
                else
                {
                    uniqueWords.Add(new UniqueWord(word));
                }
            }
        }

        uniqueWords = uniqueWords.OrderByDescending(x => x.Word.Length).Take(10).ToList();
    }
}

public class UniqueWord
{
    public string Word { get; set; }
    public int Occurrence { get; set; }

    public UniqueWord(string word)
    {
        Word = word;
        Occurrence = 1;
    }
}
0 голосов
/ 26 ноября 2018

Конечно, не самый лучший вариант, но самый быстрый, который я мог получить.largestWordsCount содержит все 10 самых больших уникальных слов и время его появления в тексте для каждого.

var text = "The standard Lorem Ipsum passage, standard used standard since the 1500s \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed";
var splittedText = text.Split(' ');
var uniqueWords = new HashSet<string>(splittedText);
var tenLargestWords = uniqueWords.ToList().OrderByDescending(x => x.Length).Take(10);
var largestWordsCount = tenLargestWords.Select(word => new KeyValuePair<string, int>(word, Regex.Matches(text, word).Count)).ToList();               
...