因此,我编写了在两个文本文件中找到最长单词的代码,如果该代码在第一个文本文件中是唯一单词,则会将其写入文件。但是我需要在第一个文本文件中找到唯一的单词,然后从这些唯一的单词中找到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;
}
答案 0 :(得分:0)
当然不是最好的选择,但是我能得到的最快的选择。 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();
答案 1 :(得分:0)
这种方式:
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;
}
}