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

Share your advanced PureBasic knowledge/code with the community.
PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post 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.
Last edited by PrincieD on Mon Feb 20, 2012 9:43 pm, edited 5 times in total.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

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

Post by Zebuddi123 »

line 50 *hunspell = HSInit(StringField(dicPath, 1, ".")+".aff", dicPath)

Error Procedure Stack has been corrupted !!!

Zebuddi
malleo, caput, bang. Ego, comprehendunt in tempore
PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post 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.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

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

Post by electrochrisso »

Thanks Chris, I will add this one to my spell check folder. :)

Are you able to make a PureLib version too. :?:
Last edited by electrochrisso on Mon Feb 20, 2012 3:50 am, edited 1 time in total.
PureBasic! Purely one of the best 8)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

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

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

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

Post by jesperbrannmark »

Would this be possible to release cross platform?
PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post 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.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post by PrincieD »

New unicode compatible version with UserLibs :D see top of thread!

Chris.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
electrochrisso
Addict
Addict
Posts: 980
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

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

Post by electrochrisso »

That was quick, thanks mate. :wink:
PureBasic! Purely one of the best 8)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

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

Post by ts-soft »

Thanks Chris :D
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post by PrincieD »

You're welcome guys! :)

Chris.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

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

Post 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

PrincieD
Enthusiast
Enthusiast
Posts: 652
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

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

Post by PrincieD »

You're welcome Fiver! and good work on adding Linux support! :)

Chris.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

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

Post by Fangbeast »

Anyone still using this code with the newest versions of PB 32 bit for Windows? I have a use for it.
Amateur Radio, D-STAR/VK3HAF
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

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

Post 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
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply