1

I have a method that runs spellcheck in a word, but what i want here is that if the word already exist in my errorlist it should not be add as error. below is my code.

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

Can some one help, I dont want any duplicate word in my errorlist.

1 Answers1

1

You can use LINQ to check if your ErrorList already contains an ErrorModel object with a Text property which has the value of the word you are checking.

Using FirstOrDefault you can check to see if it returns a null. If an item cannot be found in the list, a null will be returned as the default value because your list is a collection of class objects (whose default value is null).

If the FirstOrDefault returns null then the word has not been added to the ErrorList.

For more information see 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);
        }
    }
}
Community
  • 1
  • 1
Darren H
  • 450
  • 3
  • 5