1

単語でスペルチェックを実行するメソッドがありますが、ここで必要なのは、その単語がエラーリストに既に存在する場合、エラーとして追加しないことです。以下は私のコードです。

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

エラーリストに重複した単語を入れたくありません。

4

1 に答える 1

0

LINQ を使用して、チェックしているの値を持つプロパティを持つオブジェクトがErrorList既に含まれているかどうかを確認できます。ErrorModelTextword

を使用FirstOrDefaultすると、null が返されるかどうかを確認できます。リストに項目が見つからない場合、リストはクラス オブジェクトのコレクションであるため (デフォルト値は null)、null がデフォルト値として返されます。

FirstOrDefaultが null を返す場合、はwordに追加されていませんErrorList

詳細については、いつ .First を使用するか、いつ .FirstOrDefault を 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);
        }
    }
}
于 2015-11-05T06:54:49.413 に答える