3

To load .dic and .aff files we use the following code.

 using (var hunspell = new Hunspell("en.aff", "en.dic"))
                {

                }

But,how do I load NHunspell .dic and .aff files if embedded as resource using c# ?

I am trying out the following code, but it is damn slow.

using (var hunspell = new Hunspell(GetBytes(Properties.Resources.enaff), GetBytes(Properties.Resources.endic)))
                {

                }
static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
jeff
  • 684
  • 1
  • 11
  • 30

1 Answers1

3

Assuming you have both files as embedded resources in one of your application assemblies, for example:

  • Assembly name: MyApp.MyAssemblyWithResources
  • Resource name of the aff file: MyApp.MyAssemblyWithResources.AffFile
  • Resource name of the dict file: MyApp.MyAssemblyWithResources.DictFile

Then to load them and use them, do the following:

// These buffers will receive the content of the embedded resource files.
byte[] affFileBytes = null;
byte[] dictFileBytes = null;

// We have to load the resource files from the assembly in which they were embedded.
var myAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.Equals("MyApp.MyAssemblyWithResources")).Single();

// To do so we need to get a stream that allows us to read them.
using (var affResourceStream = myAssembly.GetManifestResourceStream("MyApp.MyAssemblyWithResources.AffFile"))
using (var dictResourceStream = myAssembly.GetManifestResourceStream("MyApp.MyAssemblyWithResources.DictFile"))
{
    // Now we know their size and can allocate room for the buffer.
    affFileBytes = new byte[affResourceStream.Length];

    // And read them from the resource stream into the buffer.
    affResourceStream.Read(affFileBytes, 0, affFileBytes.Length);

    // Same thing for the dictionary file.
    dictFileBytes = new byte[dictResourceStream.Length];
    dictResourceStream.Read(dictFileBytes, 0, dictFileBytes.Length);
}

// Now the loaded buffers can be used for the NHunspell instance.
using (var hunspell = new Hunspell(affFileBytes, dictFileBytes))
{
    // Do stuff with spellin and gramma.
}
Alex
  • 13,024
  • 33
  • 62
  • I have made some editing in my question which also does the same thing. But the problem is now the spell check is damn slow. Why? – jeff Aug 17 '13 at 03:23
  • Well one of the reasons might be that in the code you are currently using, you are loading the binary files as strings, so that may be the problem. The `Hunspell` code (see http://nhunspell.svn.sourceforge.net/viewvc/nhunspell/trunk/NHunspell/Hunspell.cs?revision=87&view=markup) constructor you used before with file name arguments also simply read the file's bytes and then loads it with the byte buffers, the exact same code. So if it has become slower, something must be wrong with the content of the byte buffers. – Alex Aug 17 '13 at 03:57
  • Your code is working fine. Thanks for solving my problem – jeff Aug 17 '13 at 04:09