textBox.Lines = ReplaceWithAcronyms(textBox.Lines, File.ReadAllLines(acronymsPath)).ToArray();
private static IEnumerable<string> ReplaceWithAcronyms(IEnumerable<string> lines, IEnumerable<string> acronyms)
{
foreach (string line in lines)
{
yield return string.Join(" ",
line.Split(' ').Select(word => ReplaceWithAcronym(word, acronyms)));
}
}
private static string ReplaceWithAcronym(string word, IEnumerable<string> acronyms)
{
string acronym = acronyms.FirstOrDefault(ac => ac == word.ToUpperInvariant());
if (acronym == null)
{
return word;
}
return acronym;
}
ReplaceWithAcronyms取文本框的行和文件的行,其中每行是一个首字母缩写词。 然后它将每行分割成单词并将每个单词传递给ReplaceWithAcronym。如果该单词是其中一个缩写词,它将返回,否则它将返回不变的单词。 通过使用string.Join,单词是“未被分离的”。结果转换为数组,然后分配回文本框行。
我没有检查数百行的速度。为了提高性能,您可以使用HashSet作为首字母缩略词。我不认为几百行是真的有问题。在尝试提高性能之前,我会试试看。也许它已经够好了。
是仅仅使它们全部大写还是拼写更正的问题? – AbZy 2013-02-12 20:06:07
它只是使它们成为大写字母,相对于原始列表。 – Jeagr 2013-02-12 20:16:11
因此,如果电视不在原始列表中,它应该保持不变? – AbZy 2013-02-12 20:18:06