Spelling checking is another function I wish ColdFusion supported out of the box. This Christmas I took over the adventure of writing a spelling checker with pure ColdFusion, so it could even works in shared hosting environments, but gave up the journey in the middle of writing a bloom filter realizing I was taking a wrong way. Unlike other adventurers, I got discouraged by my first mistake and turn into Java. But to be fair to myself, everyone in the house asking me to make Kottu for Christmas dinner.
Jazzy is a free and open source Java spelling checker API. It is incredibly fast, works smooth and easy to bring into ColdFusion. Download the jazzy-core.jar, copy into your ColdFusion server's lib folder or brining it in with javaloader or any other way you like.
Jazzy woks with Open Office dictionaries and Open Office support plenty of languages or you can download British and US dictionaries here too. Load Jazzy library and point into your main dictionary file.
1: <cfset Jazzy = CreateObject("Java","com.swabunga.spell.engine.SpellDictionaryHashMap").Init(CreateObject("java","java.io.File").Init("#ExpandPath('dictionaries/en_USx.dic')#"))> If you want to add more than one dictionary file, you can do that too.
1: <cfset Jazzy = CreateObject("Java","com.swabunga.spell.engine.SpellDictionaryHashMap").Init(CreateObject("java","java.io.File").Init("#ExpandPath('dictionaries/en_USx.dic')#"))> 2: <cfset Application.Jazzy.addDictionary(CreateObject("java","java.io.File").Init( ExpandPath('dictionaries/en_UK.dic') ))> 3: <cfset Application.Jazzy.addDictionary(CreateObject("java","java.io.File").Init( ExpandPath('dictionaries/en_UK2.dic') ))> The object creation took around 300 tick counts and it is not necessary nor smart to bring in the library on each and every word we want to spell check. Better load into an Application or Server variable.
1: <cfif not StructKeyExists(Application,'Jazzy')> 2: <cfset Application.Jazzy = CreateObject("Java","com.swabunga.spell.engine.SpellDictionaryHashMap").Init(CreateObject("java","java.io.File").Init("#ExpandPath('dictionaries/en_USx.dic')#"))> 3: </cfif>
Here is how to check if the spelling is correct or not - let's test the word "Collywobblas"
1: Application.Jazzy.isCorrect(
'Collywobblas')
This would return Yes or No, and off cause this returns No in this case. Let's ask for suggestions:
1: Application.Jazzy.getSuggestions(
'Collywobblas',1)
This returns an array of suggestions. Convert that to into a string
1: Application.Jazzy.getSuggestions(
'Collywobblas',1).
toString() gives me the correct word [collywobbles]
Adding words to your dictionary is also simple.
1: Application.Jazzy.addWord(
'Batrachomyomachy')
permanently add the word "Batrachomyomachy" into your main dictionary file. All those functions run in less than a single tick count. Sweet.
Jazzy haven't updated since 2005, it is quite an old project. But I don't think much altered in English language since Shakespeare either. Here I put together simple raw working example and also you can download the example files. (You need to load jar file into CFM for get it to work).
Posted by Saman W Jayasekara at Wednesday 26 December 2012 01:23 PM
.
ColdFusion