Commits

Nerzhul500 committed 2e6d400

Refactoring in process

Comments (0)

Files changed (8)

ReSpeller/ReSpeller.csproj

     <Compile Include="Settings\ReSpellerSettingsControl.xaml.cs">
       <DependentUpon>ReSpellerSettingsControl.xaml</DependentUpon>
     </Compile>
+    <Compile Include="SpellEngine\DictionaryFactory.cs" />
+    <Compile Include="SpellEngine\CustomizableDictionary.cs" />
+    <Compile Include="SpellEngine\HunspellDictionary.cs" />
+    <Compile Include="SpellEngine\IDictionary.cs" />
+    <Compile Include="SpellEngine\ICustomizableDictionary.cs" />
     <Compile Include="SpellEngine\ISpellChecker.cs" />
     <Compile Include="SpellEngine\NamingStyleConverter.cs" />
     <Compile Include="SpellEngine\RangeUtils.cs" />
     <Compile Include="SpellEngine\SpellChecker.cs" />
     <Compile Include="SpellEngine\SuggestionGenerator.cs" />
     <Compile Include="SpellEngine\TextSplitter.cs" />
+    <Compile Include="Util.cs" />
     <Compile Include="Workflows\ReflectionUtils.cs" />
     <Compile Include="Workflows\RenameNamespaceControlDecorator.cs" />
     <Compile Include="Workflows\RenameNamespaceWorkflowDecorator.cs" />

ReSpeller/SpellEngine/CustomizableDictionary.cs

+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace ReSpeller.SpellEngine
+{
+  public class CustomizableDictionary : ICustomizableDictionary
+  {
+    private readonly IDictionary myBaseDict;
+    private readonly string myUserDictFilename;
+
+    public CustomizableDictionary(IDictionary baseDict, string language, string userDictDir)
+    {
+      myBaseDict = baseDict;
+      myUserDictFilename = Path.Combine(userDictDir, language + ".dic");
+      if (!File.Exists(myUserDictFilename))
+      {
+        throw new FileNotFoundException("User dictionary for language {0} not found: {1}".F(language, myUserDictFilename));
+      }
+    }
+
+    public CustomizableDictionary(string userDictFilename) 
+    {
+      if (File.Exists(userDictFilename))
+      {
+        throw new FileNotFoundException("User dictionary not found: {0}".F(userDictFilename));
+      }
+      myUserDictFilename = userDictFilename;
+    }
+
+    private void LoadWordsFromFile(string userDictFile)
+    {
+      List<string> words = File.ReadAllLines(userDictFile).ToList();
+      words.ForEach(s => myBaseDict.AddToEngine(s));
+    }
+
+    public void AddUserWord(string word)
+    {
+      string[] words = File.ReadAllLines(myUserDictFilename);
+      HashSet<string> content = new HashSet<string>(words);
+      content.Add(word);
+      File.WriteAllLines(myUserDictFilename, content.ToArray());
+    }
+
+    public void Dispose()
+    {     
+      myBaseDict.Dispose();
+    }
+
+    public string Language { get { return myBaseDict.Language; } }
+
+    public bool Check(string word)
+    {
+      return myBaseDict.Check(word);
+    }
+
+    public List<string> Suggests(string word)
+    {
+      return myBaseDict.Suggests(word);
+    }
+
+    public void AddToEngine(string word)
+    {
+      myBaseDict.AddToEngine(word);
+    }
+  }
+}

ReSpeller/SpellEngine/DictionaryFactory.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace ReSpeller.SpellEngine
+{
+  public interface IDictionaryFactory
+  {
+    IDictionary CreateDictionary(string language);
+    ICustomizableDictionary CreateCustomizableDictionary(IDictionary baseDict, string language);
+    ICustomizableDictionary CreateCustomizableDictionary(string language);
+  }
+
+  public class HunspellDictionaryFactory : IDictionaryFactory
+  {
+    private readonly string myMainDictionariesDir;
+    private readonly string myUserDictionariesDir;
+
+
+    public HunspellDictionaryFactory(string mainDictionariesDir, string userDictionariesDir)
+    {
+      myMainDictionariesDir = mainDictionariesDir;
+      myUserDictionariesDir = userDictionariesDir;
+    }
+
+    public IDictionary CreateDictionary(string language)
+    {
+      throw new NotImplementedException();
+    }
+
+    public ICustomizableDictionary CreateCustomizableDictionary(IDictionary baseDict, string language)
+    {
+      throw new NotImplementedException();
+    }
+
+    public ICustomizableDictionary CreateCustomizableDictionary(string language)
+    {
+      throw new NotImplementedException();
+    }
+  }
+}

ReSpeller/SpellEngine/HunspellDictionary.cs

+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NHunspell;
+
+namespace ReSpeller.SpellEngine
+{
+  public class HunspellDictionary : IDictionary
+  {
+    private readonly string myLanguage;
+    public const string HunspellAffixExt = "aff";
+    public const string HunspellDicExt = "dic";
+
+    private readonly Hunspell myHunspell;
+    private readonly string myAdditionalDicPath;
+
+    public static string NativeDllPath
+    {
+      get { return Hunspell.NativeDllPath; }
+      set { Hunspell.NativeDllPath = value; }
+    }
+
+    public HunspellDictionary(string mainDictDir, string language)
+    {
+      myLanguage = language;
+      string affixFile = Path.Combine(mainDictDir, language + "." + HunspellAffixExt);
+      if (!File.Exists(affixFile))
+        throw new FileNotFoundException("Affix file for language {0} not found: {1}".F(language, affixFile));
+      string dictFile = Path.Combine(mainDictDir, language + "." + HunspellDicExt);
+      if (!File.Exists(dictFile))
+        throw new FileNotFoundException("Dictionary file for language {0} not found: {1}".F(language, dictFile));
+      myHunspell = new Hunspell(affixFile, dictFile);
+    }
+
+    public HunspellDictionary(string mainDictDir, string language, string additionalDictFilename) : this(mainDictDir, language)
+    {
+      myAdditionalDicPath = Path.Combine(mainDictDir, additionalDictFilename);
+      if (!File.Exists(myAdditionalDicPath))
+      {
+        myHunspell.Dispose();
+        throw new FileNotFoundException("Additional dictionary for languge {0} not found: {1}".F(language, myAdditionalDicPath));
+      }
+      List<string> additionalDict = File.ReadAllLines(myAdditionalDicPath).ToList();
+      additionalDict.ForEach(s => myHunspell.Add(s));
+    }
+
+    protected Hunspell Hunspell
+    {
+      get { return myHunspell; }
+    }
+
+    public string Language
+    {
+      get { return myLanguage; }
+    }
+
+    public bool Check(string word)
+    {
+      return myHunspell.Spell(word);
+    }
+
+    public List<string> Suggests(string word)
+    {
+      return myHunspell.Suggest(word);
+    }
+
+    public void AddToEngine(string word)
+    {
+      myHunspell.Add(word);
+    }
+
+    public void AddAdditionalWord(string word)
+    {
+      string downcased = word.ToLower();
+      string[] content = File.ReadAllLines(myAdditionalDicPath);
+      HashSet<string> hashSet = new HashSet<string>(content);
+      hashSet.Add(downcased);
+      File.WriteAllLines(myAdditionalDicPath, hashSet.ToArray());
+      myHunspell.Add(downcased);
+    }
+
+    public void Dispose()
+    {
+      if (myHunspell != null && !myHunspell.IsDisposed)
+        myHunspell.Dispose();
+    }
+  }
+}

ReSpeller/SpellEngine/ICustomizableDictionary.cs

+namespace ReSpeller.SpellEngine
+{
+  public interface ICustomizableDictionary : IDictionary
+  {
+    void AddUserWord(string word);
+  }
+}

ReSpeller/SpellEngine/IDictionary.cs

+using System;
+using System.Collections.Generic;
+
+namespace ReSpeller.SpellEngine
+{
+  public interface IDictionary : IDisposable
+  {
+    string Language { get; }
+    bool Check(string word);
+    List<string> Suggests(string word);
+    void AddToEngine(string word);
+  }
+}

ReSpeller/SpellEngine/SpellChecker.cs

       myStore = store.BindToContextTransient(ContextRange.ApplicationWide);      
       myPluginPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof (SpellChecker)).Location) +
                      Path.DirectorySeparatorChar;
-      Hunspell.NativeDllPath = myPluginPath;
+      HunspellDictionary.NativeDllPath = myPluginPath;
       myHunspell = new Hunspell(myPluginPath + "dic/en_us.aff", myPluginPath + "dic/en_us.dic");
 
       try

ReSpeller/Util.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace ReSpeller
+{
+  public static class Util
+  {
+    public static string F(this string s, params object[] args)
+    {
+      return string.Format(s, args);
+    }
+  }
+}