Friday, August 7, 2009

Google Spell Checker

Inspired by the php blog on google spell checker, Here is my Groovy version:

def spellchecker(String word){

def path = "/tbproxy/spell?lang=en&hl=en";
def urlString = "https://www.google.com"+path
def url = new URL(urlString)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true
def post ='<spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="1" ignoreallcaps="0"><text>'+word+'</text></spellrequest>'
def writer = new OutputStreamWriter(connection.outputStream)
writer.write(post)
writer.flush()
writer.close()
connection.connect()
println "The query:"+word
println "The response:\n"+connection.content.text
}

def x="""
The suggestions are tab-delineated. The .o. attribute is an offset from the start of your query to the misspelled word. .l. is the length of the misspelled w
ord. .s. is the confidence of Google.s suggestion (presumably higher is better, but I.ve only gotten 0 or 1).
"""
println x
spellchecker("love is patient")
spellchecker("weee nottt sxdsasd")
spellchecker("xxx")
spellchecker("toatlygrbg")

The following is the printout:

The query:love is patient
The response:

The query:weee nottt sxdsasd
The response:
weer wee were whee weednot knotty Natty Netty nattysadsasd
The query:xxx
The response:

The query:toatlygrbg
The response:
totally grbg

2 comments:

  1. If you want to process the response, easy:
    def spellresult = new XmlSlurper().parseText(connection.content.text)

    def error=spellresult?.@error.text()
    def clipped=spellresult?.@clipped.text()
    def charschecked=spellresult?.@charschecked.text()
    def c =spellresult?.c
    def co=c?.@o.text()
    def cl=c?.@l.text()
    def cs=c?.@s.text()
    println "error:"+error +" clipped:"+clipped+" charschecked: "+charschecked
    println "c o:"+co+" l:"+cl+" s:"+cs

    ReplyDelete
  2. Recently, the program offers obtained auto correct—the first time I've previously used just about any publishing program achievable function. I know I am extremely not even close the 1st individual to see this specific, yet auto corrects operate appears to be proper some typing blunders along with help make every one of relax hidden for you to Spellchecker. If you interested this comment please share your friend. Thanks for read this comment.

    ReplyDelete