gpt4 book ai didi

c# - 如何删除errorList中已经存在的单词

转载 作者:行者123 更新时间:2023-12-03 10:44:28 25 4
gpt4 key购买 nike

我有一个在单词中运行拼写检查的方法,但是我想要的是,如果单词在我的错误列表中已经存在,则不应将其添加为错误。下面是我的代码。

public void CheckSpelling()
{
int lineno = 0;
bool start = false;
foreach (string line in _contentList)
{
lineno++;
if (line.Contains("<text>"))
{
start = true;
}
if (start)
{
foreach (Match match in Regex.Matches(line, "<.*?>[^<]+</(.*?)>", RegexOptions.IgnoreCase))
{
List<string> customdiclist = new List<string>(File.ReadAllLines(Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["customdic"])));
string[] strArray = Regex.Replace(match.Value, "</?[^>]+>", string.Empty).Split(' ');
foreach (string word in strArray)
{
if ((word.Trim() != string.Empty) && ((word.Substring(0, 1) != word.Substring(0, 1).ToUpper()) && !_helper.CheckSpelling(Regex.Match(word, "[a-zA-Z]+").Value) && !customdiclist.Contains(word)))
{
ErrorModel errorModel = new ErrorModel()
{
LineNumber = lineno,
ErrorMessage = "Please Check Misspelled words",
Text = word
};
ErrorList.Add(errorModel);
}
}
}
}
}
}

_helper.CheckSpelling
class Helper
{
private static readonly string DictPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["dictionary"]);

private readonly Hunspell _splCheck = new Hunspell(DictPath + @"\nl_NL.aff", DictPath + @"\nl_NL.dic");
public bool CheckSpelling(string strWord)
{
if(!_splCheck.Spell(strWord.Trim()))
{
return false;
}
return true;
}
}

能帮上什么忙,我不要我的错误列表中有重复的单词。

最佳答案

您可以使用LINQ来检查ErrorList是否已包含具有ErrorModel属性的Text对象,该属性具有您要检查的word的值。

使用FirstOrDefault可以检查它是否返回null。如果在列表中找不到项目,则将返回null作为默认值,因为您的列表是类对象的集合(其默认值为null)。

如果FirstOrDefault返回null,则word尚未添加到ErrorList中。

有关更多信息,请参见When to use .First and when to use .FirstOrDefault with LINQ?

foreach (string word in strArray)
{
if ((word.Trim() != string.Empty) && ((word.Substring(0, 1) != word.Substring(0, 1).ToUpper()) && !_helper.CheckSpelling(Regex.Match(word, "[a-zA-Z]+").Value) && !customdiclist.Contains(word)))
{
if (ErrorList.FirstOrDefault(e => e.Text == word) == null)
{
ErrorModel errorModel = new ErrorModel()
{
LineNumber = lineno,
ErrorMessage = "Please Check Misspelled words",
Text = word
};
ErrorList.Add(errorModel);
}
}
}

关于c# - 如何删除errorList中已经存在的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com