from lxml.builder import ElementMaker
from lxml.etree import tostring, parse, XPath
from soap import *


class CheckSpelling(Soap):
    
    def __init__(self):
        theDomain = "ws.cdyne.com"
        theURL = "/spellchecker/check.asmx"
        theNamespace = "http://ws.cdyne.com/"
        theServiceName = SOAP_NO_NAME
        self.methodName = "CheckTextBody"
        super(CheckSpelling,self).__init__(theDomain,theURL,theNamespace,theServiceName)
    
    def buildRequestElement(self,text):
        em = ElementMaker(namespace=self.namespace, nsmap={None: self.namespace})
        elementCheckBody  = em.CheckTextBody
        elementBodyText = em.BodyText
        return elementCheckBody(elementBodyText(text))

    def parseResponse(self,responseXML):
        misspellings = self.XPathWithQuery('//thens:MisspelledWord')
        suggestions = self.XPathWithQuery('//thens:Suggestions/text()')
        word = self.XPathWithQuery('//thens:word/text()')
        suggestioncount = self.XPathWithQuery('//thens:SuggestionCount/text()')
        return [ {'Suggestions':suggestions(m),'for word':word(m),'count':suggestioncount(m)}  for m in misspellings(responseXML) ]

    def lookupWord(self,word):
        return self.parseResponse(self.makeRequest(self.methodName,self.buildRequestElement(word)))


######### EXAMPLE USAGE ############# 
checkit = CheckSpelling()
print checkit.lookupWord("recieved")


