[Library] Spell v2.1

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

[Library] Spell v2.1

25 Oct 2014, 14:29

Post History
Version 1.x of this library was first released on the original AutoHotkey forum by majkinetor. You can find the original post here. New versions of the library, if any, will be posted on this forum.

Introduction
Hunspell is a spell check subsystem that was created in 2003. Because it's free and open source, it is incorporated into many applications including OpenOffice, LibreOffice, Mozilla Thunderbird & Firefox, and Google Chrome, just to name a few.

The Spell library is a wrapper for the Hunspell API with additional functions to support custom dictionaries.

Spell Library
Key features:
  • Compatibility. [v2.1+] The Spell library is designed to work on a recent version of AutoHotkey v1.1+; ANSI, Unicode, and Unicode x64.
  • Custom Dictionaries. [v1.1+] Words from one or more custom dictionaries can be quickly added to the active dictionary.
  • Portable and Independent. All programs and files used by this library (Hunspell DLLs, Hunspell dictionaries, and custom dictionary files) can be stored anywhere. This library does not use any other programs, services, or files.
What's New in Version 2.1?
Version 2.1 is a fairly minor release but there are a few important changes.
  • Compatibility. This version of the library is designed to run on a recent version of AutoHotkey v1.1; ANSI, Unicode, and Unicode x64. Future versions (if any) will require AutoHotkey v2.0+.
  • Script breakers. If using a custom dictionary, there are a few script breakers. See the Release Notes and the function documentation for more information.
  • Documentation. The general library documentation has been moved to the _Spell.Doc.ahk file. Natural Docs v2.3 is used to generate HTML documentation from this file and from the function documentation. See the Release Notes for more information.
The Code
The pertinent files are as follows:
  • Project: Spell.zip
    This project archive includes the Spell library, the Hunspell DLL files, example scripts, all the files needed to run the example scripts, and the project documentation.
  • Documentation: Starting with v2.1, the project documentation is included in the project archive file (above).
Issues/Considerations
Stuff to know:
  • Hunspell Dictionaries. Thanks to Apache OpenOffice, Hunspell dictionaries for a dozen or so languages can be found on their web site but Hunspell dictionaries for other languages may be hard to find. There are many languages for which there are no Hunspell dictionaries. Please note that my ability to do any serious tests on dictionaries other than US English, is severely limited. If you experience any problems with your dictionary, please let me know. I will try to correct the problem.
  • Examples. If any of the examples don't work as expected, please let me know. I will try to fix the problem.
Release Notes
Last edited by jballi on 22 Nov 2023, 03:06, edited 2 times in total.
User avatar
joedf
Posts: 8966
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: [Library] Spell v2.0 (Preview)

26 Oct 2014, 10:26

Datz eh kool Zkript! :D
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
Gavin
Posts: 12
Joined: 14 Jul 2020, 03:35

Re: [Library] Spell v2.0 (Preview)

17 May 2023, 14:42

Hi,

Many thanks for this library and the precompiled DLLs.

Having placed the DLLs in my hunspell folder, they work flawlessly, even with my old version of AHK/AHK2EXE [1].

I have made an editor using the HiEditor control for AHK [2].

Would you consider adding a stand-alone example with a GUI (SpellCheck Dialog)?

- Either loading text from a file or from an AHK variable.
- Updating would need to be after all the checking.

I can't seem to download code from the original Spell 1.1 library post - I recall there was a GUI example, not sure if it would work with Spell 2.0.

Kind Regards Gavin Holt

[1] Ahk2Exe v1.0.48.05, AutoHotkeySC.bin 25.09.2009
[2] https://github.com/Gavin-Holt/APEditor
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: [Library] Spell v2.0 (Preview)

17 May 2023, 17:02

Hey Gavin. Thank you for your interest.
Gavin wrote:
17 May 2023, 14:42
I can't seem to download code from the original Spell 1.1 library post - I recall there was a GUI example, not sure if it would work with Spell 2.0.
Yes, I think there was a GUI example included with Spell v1.1 but it would take a significant effort to convert it to work with Spell v2.0. Most of the effort would be wasted because the Spell v1.1 library has been deprecated for almost 10 years.
Gavin wrote:
17 May 2023, 14:42
Would you consider adding a stand-alone example with a GUI (SpellCheck Dialog)?
As I discovered a long time ago, creating a stand-alone example for the Spell v2.0 library is a significant task. A lot of code is necessary just to create a simple example. The good news is that an example for the Spell Unicode library is included with the Edit v2.0 library. You can find it here.

There are considerations. Although the Spell v2.0 library was designed to run on all versions of AutoHotkey at the time (not AutoHotkey v2), the Edit v2.0 library will only run on AutoHotkey v1.1+.

I hope this is helpful.
User avatar
Gavin
Posts: 12
Joined: 14 Jul 2020, 03:35

Re: [Library] Spell v2.0 (Preview)

21 May 2023, 18:17

Hi

Although not an IT professional, I thought I would give it a go myself, fully realizing I am writing for a deprecated target!

This is some working code #included as part of my APEditor script:

Code: Select all

; This is my spellcheck plugin - built upon the shoulders of giants!
; Requirements
;	Designed to be #included in APEditor
;		https://github.com/Gavin-Holt/APEditor
; 		Using very old AHK/AHK2EXE (ver 1.0.48.05)
; 	Spell.ahk (2.0) jballi
;		https://www.autohotkey.com/boards/viewtopic.php?t=4971
;		I copied Hunspellx86.dll to my Hunspell folder
;	See hard coded paths throughout
#include inc\Spell.ahk

$SpellCheck(hEdit, sText){

    ; Destroy previous iterations
    Spell_Uninit(hSpell)
    tText =
    Replacement =

	; Initialize
	if not Spell_Init(hSpell,"O:\MyProfile\editor\hunspell\en_GB.aff", "O:\MyProfile\editor\hunspell\en_GB.dic","O:\MyProfile\editor\hunspell\Hunspellx86.dll")
		return

	; Load the custom dictionary
	if not Spell_InitCustom(hSpell,"O:\MyProfile\editor\hunspell\user_dict.dic","L")
		return

	; Check for selection
	if Strlen(sText) < 1
	{
		sText := $GetAllText(hEdit)
	}
	if Strlen(sText) < 1
	{
		; This is an empty file
		msgbox, No text to spellcheck
		return
	}

    ; Pad the text
    sText = %A_Space%%sText%%A_Space%

	; Remove all unwanted characters
	StringReplace, tText, sText, %A_Tab%, %A_Space% , All
	StringReplace, tText, tText, `r, %A_Space% , All
	StringReplace, tText, tText, `n, %A_Space% , All
	StringReplace, tText, tText, `,, %A_Space% , All
	StringReplace, tText, tText, `;, %A_Space% , All
	StringReplace, tText, tText, `%, %A_Space% , All
	StringReplace, tText, tText, ., %A_Space% , All
	StringReplace, tText, tText, ?, %A_Space% , All
	StringReplace, tText, tText, !, %A_Space% , All
	StringReplace, tText, tText, ), %A_Space% , All
	StringReplace, tText, tText, (, %A_Space% , All
	StringReplace, tText, tText, [, %A_Space% , All
	StringReplace, tText, tText, ], %A_Space% , All
	StringReplace, tText, tText, #, %A_Space% , All
	StringReplace, tText, tText, @, %A_Space% , All
	StringReplace, tText, tText, ~, %A_Space% , All
	StringReplace, tText, tText, |, %A_Space% , All
	StringReplace, tText, tText, \, %A_Space% , All
	StringReplace, tText, tText, /, %A_Space% , All
	StringReplace, tText, tText, ", %A_Space% , All
; 	StringReplace, tText, tText, ', %A_Space% , All
	StringReplace, tText, tText, :, %A_Space% , All
	StringReplace, tText, tText, _, %A_Space% , All
	StringReplace, tText, tText, %A_Space%-%A_Space%, %A_Space% , All
	StringReplace, tText, tText, %A_Space%%A_Space%, %A_Space% , All

	; Filter down to just misspelled words
	Loop, Parse, tText, %A_Space%
	{
		Word = %A_LoopField%

		; Short circuit if single letter
		if strlen(Word) < 2
		{
			continue
		}

		; Short cut if its properly spelled
		if (Spell_Spell(hSpell, Word))
		{
			continue
		}

		; --- From here we have a new word to check

		; Generate suggestions
 		Spell_Suggest(hSpell, Word, sList)
 		if not sList
		{
			sList = No suggestions
		}

    	; Call GUI for decision 
    	Replacement := $SpellCheckGUI(Word, sList)

    	; Remember the word to avoid re-asking
    	Spell_Add(hSpell,Word)

    	if not Replacement
    	{
    		continue
    	} else {			
    		; Replace in the original string (whole words)
			sText := RegExReplace(sText,"m)\b" . Word . "\b",Replacement)
    	}

	}

	; Close the Spell GUI
	Gui, 2:Destroy
	Gui, 1:Show

    ; Trim the text padding
    StringTrimLeft, sText, sText, 1
    StringTrimRight, sText, sText, 1

	; Write back the new text
	HE_ReplaceSel(hEdit,sText)

	; Release DLL memory
	Spell_Uninit(hSpell)

    ; Clear variables
    sText =
    tText =
    Replacement =
}

$SpellCheckGUI(Word, sList){
	Global
	; Ensure all global variables

	; Clear variables
	Suggestion =

	; Setup the GUI
	Gui, 2:+Owner +ToolWindow +AlwaysOnTop +Delimiter`n
	Gui, 2:Add, Text, 		x7 		y7 		w250 	h21 , %Word% not in dictionary
	Gui, 2:Add, Text, 		x7 		y30 	w250 	h13 , Replace:
	Gui, 2:Add, Edit, 		x7 		y44 	w250 	h21 , %Word%
	Gui, 2:Add, Text, 		x7 		y72 	w250 	h13 , Suggestions:
	Gui, 2:Add, ListBox, 	x7 		y86 	w250 	h102 Choose1 vSuggestion, %sList%

	Gui, 2:Add, Button, 	x265 	y53 	w90 	h23 , &Ignore
	Gui, 2:Add, Button, 	x265 	y86 	w90 	h23 , &Replace
	Gui, 2:Add, Button, 	x265 	y119 	w90 	h23 , A&dd Word
	Gui, 2:Add, Button, 	x265 	y152 	w90 	h23 , Cancel

	; Show child
	Gui, 2:Show, 			x142 	y369 	w363	h193, Spell Check

	; Loop until a decision is made  (GUI cannot be modal and stop a loop)
	NextWord = 0
	Loop,
	{
		if NextWord > 0
		{
			Break
		} else {
			Sleep, 300
			Continue
		}
	}

	; This is Submit
	Gui 2:Destroy
	Gui, 1:show
	Return %Suggestion%

; Gui subroutines
2ButtonIgnore:
    ; Record so we don't ask again
    Spell_Add(hSpell, Word, "L")
	Suggestion = 0
	NextWord = 1
	Return

2ButtonReplace:
	Gui, Submit, NoHide
	if (Suggestion = "No suggestions")
	{
		Suggestion = 0
	}
	NextWord = 1
	Return

; TODO this does not work!!
2ButtonAdd:
;  	Spell_AddCustom("O:\MyProfile\editor\hunspell\USER_DICT.dic",Word)
	Suggestion = 0
	NextWord = 1
	Return

2ButtonCancel:
2GuiClose:
2GuiEscape:
	Gui, 2:Destroy
	Suggestion = 0
	Return

Exit
}
I've done a little testing, adding words to the user_dict.dic does not work in this script.

Many thanks for the compiled DLLS, I can now use the same Hunspell dictionaries with all my editors (SciTE, micro, and APEditor).

Kind Regards Gavin Holt

NB For any other readers - sorry I don't know how to update this code for AHK 1.1 or 2.0.
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: [Library] Spell v2.1

22 Nov 2023, 03:09

v2.1
Most of the development for this version of the library was completed many years ago. I have used this version internally with my own projects but I never got around to publishing it. I even forgot that it was never published. I only figured it out when I compared this version with what was last published. Since this version contains some compatibility fixes for projects that may be published in the future, now is the time to put this version out there.

This is a fairly minor release. If using a custom dictionary, there may be some script breakers. See the Release Notes section of the first post for all the changes.

This version of the project only runs on a recent version of AutoHotkey v1.1+. AutoHotkey v2.0+ is not supported. With the exception of bug fixes, this will be the last release that uses AutoHotkey v1.1+. Future versions of this project (if any) will require AutoHotkey v2.0+.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: remo6to6 and 118 guests