如何删除errorList中已存在的单词

时间:2015-11-05 06:38:05

标签: c# mvvm hunspell spelling

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

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;
        }
    }

可以帮助一些人,我不希望我的错误列表中有任何重复的单词。

1 个答案:

答案 0 :(得分:0)

您可以使用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);
        }
    }
}