Page 1 of 2

SpellCheck lib/include (32bit and 64bit) - hunspell wrapper

Posted: Sun Feb 19, 2012 5:57 pm
by PrincieD
Hi Guys!

I've wrote a nice simple little SpellCheck library/include for Windows that wraps hunspell (compiles for both x86 and x64), based on the original thread here: http://www.purebasic.fr/english/viewtop ... =7&t=43739

Hunspell uses UTF8 encoding for unicode strings and I've modified the library accordingly :)

SpellCheck Library - 32bit/64bit Userlibs, pre-compiled Hunspellx86.dll/Hunspellx64.dll (V1.3.2 - Latest), dictionaries (US/UK English), source and example.
http://www.progui.co.uk/downloads/SpellCheck.zip

Commands in the Lib
SpellCheck_Dictionary(dicPath.s) ; Sets the current dictionary, returns nonzero if successful
SpellCheck(word.s) ; Checks spelling of specified word, returns true if correct spelling, false otherwise
SpellCheck_Suggest(word.s) ; Offers suggestions for a word, returns string containing comma separated list
SpellCheck_Ignore(word.s) ; Adds a new temporary word to the current spellcheck session, having the effect of ignoring the specified word
SpellCheck_Add(word.s) ; Adds a new word to the custom user dictionary of the currently opened dictionary

More dictionary files for different languages can be downloaded here: http://wiki.services.openoffice.org/wiki/Dictionaries

Please feel free to modify/improve/use as you want :)

Example

Code: Select all

SpellCheck_Dictionary("Dictionaries\en_GB.dic")

If SpellCheck("imagination")
  Debug "spelling correct"
Else
  Debug "spelling wrong!"
EndIf

suggestions$ = SpellCheck_Suggest("imagintaion")
If suggestions$ <> ""
  i = CountString(suggestions$, ",")
  For n = 1 To i+1
    Debug StringField(suggestions$, n, ",")
  Next
Else
  Debug "no suggestions!"
EndIf
Hope it's useful! :)

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Sun Feb 19, 2012 7:26 pm
by Zebuddi123
line 50 *hunspell = HSInit(StringField(dicPath, 1, ".")+".aff", dicPath)

Error Procedure Stack has been corrupted !!!

Zebuddi

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Sun Feb 19, 2012 7:56 pm
by PrincieD
Zebuddi123 wrote:line 50 *hunspell = HSInit(StringField(dicPath, 1, ".")+".aff", dicPath)

Error Procedure Stack has been corrupted !!!

Zebuddi
Whoops! I've fixed it now, the prototypes should have been prototypeC :oops: worked fine on x64 though :?

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Mon Feb 20, 2012 1:14 am
by electrochrisso
Thanks Chris, I will add this one to my spell check folder. :)

Are you able to make a PureLib version too. :?:

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Mon Feb 20, 2012 1:21 am
by ts-soft
Hello chris, can you compile a unicode version?
I use the old V 0.9.2.0, but in unicode, is IMHO more usefull :wink:

Greetings - Thomas

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Mon Feb 20, 2012 8:15 am
by jesperbrannmark
Would this be possible to release cross platform?

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Mon Feb 20, 2012 5:40 pm
by PrincieD
electrochrisso wrote:Thanks Chris, I will add this one to my spell check folder. :)

Are you able to make a PureLib version too. :?:
No worries mate :) yup I'll make it into PureLib version too.
ts-soft wrote:Hello chris, can you compile a unicode version?
I use the old V 0.9.2.0, but in unicode, is IMHO more usefull :wink:

Greetings - Thomas
Hey Thomas mate, yes that makes more sense lol I was just getting it working and testing it out really :)
jesperbrannmark wrote:Would this be possible to release cross platform?
Yes the include file should work with little modification but the hunspell DLL's would need to be compiled/built separately for each OS (hunspell is cross-platform)

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Mon Feb 20, 2012 9:32 pm
by PrincieD
New unicode compatible version with UserLibs :D see top of thread!

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Tue Feb 21, 2012 3:46 am
by electrochrisso
That was quick, thanks mate. :wink:

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Tue Feb 21, 2012 5:29 am
by ts-soft
Thanks Chris :D

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Tue Feb 21, 2012 2:00 pm
by PrincieD
You're welcome guys! :)

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Thu May 17, 2012 8:09 pm
by fiver
Thanks Chris!

I tweaked the include a bit to work on 32bit Linux, should be easy to do for osx as well, only the function names seem to differ slightly.

Download link for recent Linux library and example :

http://goo.gl/94bH4

Tweaked include:

Code: Select all

#hunspellDLLx86 = "Hunspellx86.dll"
#hunspellDLLx64 = "Hunspellx64.dll"
#hunspellSOx86linux = "./libhunspell-1.3.so"

Global *hunspellLib ; stores handle to hunspell lib
Global *hunspell ; stores handle to initialised hunspell object
Global hunspell_dicpath.s ; stores path to current dictionary file
Global hunspellReturnString$ ; used in PureBasic Userlib to return string

; hunspell prototypes
PrototypeC.i HSInit_PT(aff_file.p-utf8, dic_file.p-utf8) : Global HSInit.HSInit_PT
PrototypeC.i HSFree_PT(*hunspell) : Global HSFree.HSFree_PT
PrototypeC.i HSSpell_PT(*hunspell, word.p-utf8) : Global HSSpell.HSSpell_PT
PrototypeC.i HSSuggest_PT(*hunspell, *elements, word.p-utf8) : Global HSSuggest.HSSuggest_PT
PrototypeC.i HSAdd_PT(*hunspell, word.p-utf8) : Global HSAdd.HSAdd_PT

; ******************************* public commands here ********************************************************

Procedure SpellCheck_Dictionary(dicPath.s) ; Sets the current dictionary, returns nonzero if successful
  
  If *hunspellLib = 0
  
    CompilerIf #PB_Compiler_OS = #PB_OS_Linux
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        hunspell$ = #hunspellSOx86linux
      CompilerEndIf
    CompilerEndIf
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      CompilerIf  #PB_Compiler_Processor = #PB_Processor_x86
        hunspell$ = #hunspellDLLx86
      CompilerElse
        hunspell$ = #hunspellDLLx64
      CompilerEndIf  
    CompilerEndIf
    
    *hunspellLib = OpenLibrary(#PB_Any, hunspell$)
    If *hunspellLib
      CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        HSInit = GetFunction(*hunspellLib, "hunspell_initialize")
        HSFree = GetFunction(*hunspellLib, "hunspell_uninitialize")
        HSSpell = GetFunction(*hunspellLib, "hunspell_spell")
        HSSuggest = GetFunction(*hunspellLib,"Hunspell_suggest")
        HSAdd = GetFunction(*hunspellLib,"Hunspell_add")
      CompilerEndIf
      CompilerIf #PB_Compiler_OS = #PB_OS_Linux
        HSInit = GetFunction(*hunspellLib, "Hunspell_create")
        HSFree = GetFunction(*hunspellLib, "Hunspell_free")
        HSSpell = GetFunction(*hunspellLib, "Hunspell_spell")
        HSSuggest = GetFunction(*hunspellLib,"Hunspell_suggest")
        HSAdd = GetFunction(*hunspellLib,"Hunspell_add")
      CompilerEndIf
    Else
      MessageRequester("Error!","Can't open library "+hunspell$+"!")
    EndIf
    
  EndIf
  
  If *hunspellLib <> 0
    If *hunspell
      HSFree(*hunspell)
    EndIf
    *hunspell = HSInit(StringField(dicPath, 1, ".")+".aff", dicPath)
    hunspell_dicpath = dicPath
    datFile = ReadFile(#PB_Any, StringField(hunspell_dicpath, 1, ".")+".dat")
    If datFile
      While Eof(datFile) = 0
        HSAdd(*hunspell, ReadString(datFile, #PB_UTF8))
      Wend
      CloseFile(datFile)
    EndIf
  EndIf
  
  ProcedureReturn *hunspell
  
EndProcedure

Procedure SpellCheck(word.s) ; Checks spelling of specified word, returns true if correct spelling, false otherwise
  
  If *hunspell
    ProcedureReturn HSSpell(*hunspell, word)
  EndIf
  
EndProcedure

Procedure.s SpellCheck_Suggest(word.s) ; Offers suggestions for a word, returns string containing comma separated list
  
  If *hunspell
    hunspellReturnString$ = ""
    *elements = AllocateMemory(1024)
    If *elements
      suggestions = HSSuggest(*hunspell, *elements, word)
      If suggestions > 0
        For i = 0 To Suggestions-1
          hunspellReturnString$ + PeekS(PeekI(PeekI(*elements)+(i*SizeOf(*elements))), -1, #PB_UTF8)
          If i < Suggestions-1
            hunspellReturnString$ + ","
          EndIf
        Next
      EndIf
      FreeMemory(*elements)
    EndIf
  EndIf
  
  ProcedureReturn hunspellReturnString$
  
EndProcedure

Procedure SpellCheck_Ignore(word.s) ; Adds a new temporary word to the current spellcheck session, having the effect of ignoring the specified word
  
  If *hunspell
    HSAdd(*hunspell, word)
    ProcedureReturn #True
  EndIf
  
EndProcedure

Procedure SpellCheck_Add(word.s) ; Adds a new word to the custom user dictionary of the currently opened dictionary
  
  ; word not already learnt
  If *hunspell And SpellCheck(word) = 0
    datFile = OpenFile(#PB_Any, StringField(hunspell_dicpath, 1, ".")+".dat")
    If datFile
      FileSeek(datFile, Lof(datFile))
      WriteStringN(datFile, word, #PB_UTF8)
      CloseFile(datFile)
      HSAdd(*hunspell, word)
      ProcedureReturn #True
    EndIf
  EndIf
  
EndProcedure

Procedure SpellCheck_End() ; Free the hunspell library
  
  If *hunspell
    HSFree(*hunspell)
    CloseLibrary(*hunspellLib)
    *hunspell = #False
    *hunspellLib = #False
  EndIf
  
EndProcedure


Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Fri May 18, 2012 3:44 pm
by PrincieD
You're welcome Fiver! and good work on adding Linux support! :)

Chris.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Thu May 05, 2016 6:35 am
by Fangbeast
Anyone still using this code with the newest versions of PB 32 bit for Windows? I have a use for it.

Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap

Posted: Thu May 05, 2016 3:17 pm
by blueb
Fangbeast...

Yes, it works. (has the British and US spelling libraries included)... but absolutely no Aussie slang. :mrgreen:

It is a slightly newer version of the 'Hunspell.dlls' than Thomas (ts-soft) Schulz's Spellchecker (inside RichEdit.zip), which has an excellent example.

HTH