【问题标题】:How to remove a word that already exist in errorList如何删除errorList中已经存在的单词
【发布时间】:2016-02-05 21:46:24
【问题描述】:

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

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

有人可以帮忙吗,我不想在我的错误列表中出现任何重复的单词。

【问题讨论】:

    标签: c# mvvm hunspell spelling


    【解决方案1】:

    您可以使用 LINQ 检查您的 ErrorList 是否已经包含一个带有 Text 属性的 ErrorModel 对象,该属性具有您正在检查的 word 的值。

    使用FirstOrDefault 可以检查它是否返回空值。如果在列表中找不到项目,将返回 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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 2011-07-19
      • 1970-01-01
      • 2021-09-29
      • 2014-12-31
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多