Source

Resharper Spell Checker / ReSpeller / SpellEngine / ConfigurableWordChecker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReSpeller.SpellEngine.Dictionaries;

namespace ReSpeller.SpellEngine
{
  public class ConfigurableWordChecker
  {
    private const int MaxLengthForSplit = 30;

    private readonly List<ILanguageDictionary> myDictionaries;

    public ConfigurableWordChecker(List<ILanguageDictionary> dictionaries)
    {
      myDictionaries = dictionaries;
      Config = new SpellCheckerConfig();
    }

    public SpellCheckerConfig Config { get; set; }

    public bool Check(string word)
    {
      if (word.Length < Config.MinWordLength)
        return true;
      // try to check in many styles
      bool wholeWordCheck = CheckInDictionaries(word.ToLower())
        || CheckInDictionaries(word.ToUpper())
        || CheckInDictionaries(NamingStyleConverter.Convert(word, "Aaa"));
      if (wholeWordCheck)
        return true;

      //try to split into pair of words
      if (Config.SplitInPairs)
      {
        if (word.Length < 4 || word.Length > MaxLengthForSplit) //min size of each word == 2, e.g. inplace -> in+place
          //also very long words will be ignored
          return false;
        for (int i = 2; i < word.Length - 1; ++i)
        {
          string first = word.Substring(0, i);
          string second = word.Substring(i);
          if (CheckInDictionaries(first) && CheckInDictionaries(second))
            return true;
        }
      }
      return false;
    }

    private bool CheckInDictionaries(string word)
    {
      return myDictionaries.Any(dictionary => dictionary.Check(word));
    }    
  }
}