It is currently Wed Jun 19, 2013 8:43 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: SpellCheck lib/include (32bit and 64bit) - hunspell wrapper
PostPosted: Sun Feb 19, 2012 5:57 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
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/viewtopic.php?f=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:
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.

_________________
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk


Last edited by PrincieD on Mon Feb 20, 2012 9:43 pm, edited 5 times in total.

Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Sun Feb 19, 2012 7:26 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Feb 01, 2012 3:30 pm
Posts: 276
Location: Nottinghamshire UK
line 50 *hunspell = HSInit(StringField(dicPath, 1, ".")+".aff", dicPath)

Error Procedure Stack has been corrupted !!!

Zebuddi


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Sun Feb 19, 2012 7:56 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
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


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Mon Feb 20, 2012 1:14 am 
Offline
Enthusiast
Enthusiast

Joined: Mon May 14, 2007 2:13 am
Posts: 736
Location: Darling River
Thanks Chris, I will add this one to my spell check folder. :)

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

_________________
PureBasic Rocks! Even More! And More!
PureBasic 5, Now We're Really Rockin!


Last edited by electrochrisso on Mon Feb 20, 2012 3:50 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Mon Feb 20, 2012 1:21 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4740
Location: Berlin - Germany
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.11 | Windows 7 SP1 (x64) | Linux Mint 15 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Mon Feb 20, 2012 8:15 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Feb 16, 2009 10:42 am
Posts: 531
Location: sweden
Would this be possible to release cross platform?


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Mon Feb 20, 2012 5:40 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
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


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Mon Feb 20, 2012 9:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
New unicode compatible version with UserLibs :D see top of thread!

Chris.

_________________
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Tue Feb 21, 2012 3:46 am 
Offline
Enthusiast
Enthusiast

Joined: Mon May 14, 2007 2:13 am
Posts: 736
Location: Darling River
That was quick, thanks mate. :wink:

_________________
PureBasic Rocks! Even More! And More!
PureBasic 5, Now We're Really Rockin!


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Tue Feb 21, 2012 5:29 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4740
Location: Berlin - Germany
Thanks Chris :D

_________________
PureBasic 5.11 | Windows 7 SP1 (x64) | Linux Mint 15 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Tue Feb 21, 2012 2:00 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
You're welcome guys! :)

Chris.

_________________
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk


Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Thu May 17, 2012 8:09 pm 
Offline
User
User
User avatar

Joined: Wed May 05, 2004 8:21 pm
Posts: 29
Location: An outer spiral arm of the Milky Way
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:
#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



Top
 Profile  
 
 Post subject: Re: SpellCheck lib/include (32bit and 64bit) - hunspell wrap
PostPosted: Fri May 18, 2012 3:44 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Aug 10, 2005 2:08 pm
Posts: 559
Location: Yorkshire, England
You're welcome Fiver! and good work on adding Linux support! :)

Chris.

_________________
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: Psychophanta and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye