C# 如何删除错误列表中已存在的单词
我有一个在单词中运行拼写检查的方法,但是我想要的是,如果单词已经存在于我的错误列表中,那么它不应该被添加为错误。下面是我的代码C# 如何删除错误列表中已存在的单词,c#,mvvm,hunspell,spelling,C#,Mvvm,Hunspell,Spelling,我有一个在单词中运行拼写检查的方法,但是我想要的是,如果单词已经存在于我的错误列表中,那么它不应该被添加为错误。下面是我的代码 public void CheckSpelling() { int lineno = 0; bool start = false; foreach (string line in _contentList) { lineno++; if (line.C
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);
}
}
}
}
}
}
有人能帮忙吗,我不想在我的错误列表中有任何重复的单词。你可以使用LINQ来检查你的
errorlist
是否已经包含一个ErrorModel
对象,该对象具有一个Text
属性,该属性具有你正在检查的word
的值
使用FirstOrDefault
可以检查它是否返回空值。如果在列表中找不到项,将返回null作为默认值,因为列表是类对象的集合(其默认值为null)
如果FirstOrDefault
返回null,则单词
尚未添加到错误列表
有关更多信息,请参阅
您可以使用LINQ检查您的
ErrorList
是否已经包含一个ErrorModel
对象,该对象具有一个Text
属性,该属性的值为您正在检查的word
使用FirstOrDefault
可以检查它是否返回空值。如果在列表中找不到项,将返回null作为默认值,因为列表是类对象的集合(其默认值为null)
如果FirstOrDefault
返回null,则单词
尚未添加到错误列表
有关更多信息,请参阅
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;
}
}
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);
}
}
}