Spelling and Things

My speeling spelling is pretty awful sometimes.  I can look at a word and know it’s spelt incorrectly, but can’t for the life of me see why it’s wrong.  I’m sure I must have some form of low-grade dyslexia or something!  Luckily, most writing programs (including the excellent Windows Live Writer – highly recommended) have decent spell-checkers built in; but I don’t always have such a program open and it can take me a while to decide whether to open Word, go to IE and browse around or just grab a good-old printed dictionary.

I do usually have a PowerShell session open somewhere though (and if I don’t, there’s always the Sidebar PowerShell Gadget), so I thought I’d write a "Spell" command that would just check a spelling for me.  Although I have an ancient (16-bit, circa Windows 3.1) software version of the Concise Oxford English Dictionary it doesn’t have a COM interface – so it seemed a Web Service was the way to go.

A quick search showed Yahoo! and Cdyne to be the most useful candidates (Google also provide a service, but it requires registration – not a big deal, but why do I want to manage a registration key for ever-more when I don’t need to?)

Writing the PowerShell script is then a complete pushover, PowerShell just makes this kind of thing so easy.  [Gratuitous praise (no payment required…) It’s even more of a  pushover when you use PowerShell Plus (I’ve just download the latest version and it just keeps getting better and better – well done Karl et al!)].     The code just gets a new System.Net.WebClient object, composes the required URLs and reads back the resulting XML from Yahoo! and Cdyne.  Assuming something useful was returned it’s displayed accordingly.  Here’s some sample output:

PS> spell advanceed
Yahoo! Suggestions:
advanced
Cdyne Suggestions:
advanced
advance ed
advancer
advance
PS> spell acomoddate
Yahoo! Suggestions:
accommodate
Cdyne Suggestions:
accommodate
accommodated
accommodates
accommodative
PS> speel collaberation
Yahoo! Suggestions:
collaboration
Cdyne Suggestions:
collaboration
collaborations
calibration
collaborative

 

Here’s the code (I dot-source this in my profile) – Enjoy!

# Get-Spelling
# Uses Yahoo! and cdyne Web Services to check spelling
# Chris Warwick, April 2008

Function Get-Spelling ([String]$Word = $(Throw "Specify a word to spell-check")) {

	$WebClient = New-Object System.Net.WebClient

	#Create the Yahoo! Service Url, get the result and cast to XML 
	$url = "http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion?appid=CJWGet-Spelling&query=$word"
	$YahooResponse = [xml]$WebClient.DownloadString($url)

	#Create the cdyne Service Url, get the result and cast to XML 
	$url = "http://ws.cdyne.com/SpellChecker/check.asmx/CheckTextBody?BodyText=$word&licensekey="
	$CdyneResponse = [xml]$WebClient.DownloadString($url)

	If ($YahooResponse.ResultSet.Result -ne $null) {
		"Yahoo! Suggestions:"
		$YahooResponse.ResultSet.Result
	}
	else {
		"'$Word' - no alternative suggestions from Yahoo!"
	}

	If ($CdyneResponse -ne $null -and [int]$CdyneResponse.DocumentSummary.MisspelledWord.SuggestionCount -gt 0) {
		"Cdyne Suggestions:"
		$CdyneResponse.DocumentSummary.MisspelledWord.Suggestions[0..3] # (Show only top 4 results)
	}
	else {
		"'$Word' - no alternative suggestions from Cdyne"
	}
}

Set-Alias Spell Get-Spelling
Set-Alias Speel Get-Spelling		#   :-)

One thought on “Spelling and Things”

Leave a reply to Aleksandar Cancel reply