Using the Google API Spelling Checker in ASP Classic
When I was re-writing the search engine for Quest-net, I decided to try to use the Google API to correct spelling mistakes in the search terms.
There's quite a lot of info on using the Google API out there - mostly for ASP.NET unfortunately - plus, I found that our web hosting service didn't support the "MSSOAP.SoapClient30" component used by most of the ASP Classic examples available.
Below is a function that gets round that limitation. You'll need to download the Google API Resource Kit (to get a copy of "doSpellingSuggestion.xml") and obtain a License Key.
This is not a good example of programming: It's a cludge. It does however work well with the resources available on our web server. If you can use SOAP you should.
You can find out more about the Google API, apply for your License Key and download the resource kit here
The Code
<%
Function doSpelling(words)
Dim Google_Web_APIs_license_key, Search_String2
Google_Web_APIs_license_key = "YOUR GOOGLE API KEY HERE"
Search_String2 = words
' The SOAP message (doGoogleSearch.xml)
' was downloaded from the Google API soap-examples.
' Dump the SOAP message into an XML document and
' set the key value, search string value and start index.
Set objInputXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
objInputXMLDoc.load Server.MapPath("doSpellingSuggestion.xml")
objInputXMLDoc.selectSingleNode("//key").Text = Google_Web_APIs_license_key
objInputXMLDoc.selectSingleNode("//phrase").Text = Search_String2
' Post the SOAP message.
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.open "post", "http://api.google.com/search/beta2", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml"
objXMLHTTP.setRequestHeader "SOAPAction", "doSpellingSuggestion"
objXMLHTTP.send objInputXMLDoc
dummy = Server.HTMLEncode(objXMLHTTP.responseText)
' here's the cludge
startPos = InStr(dummy,"xsd:string") + 20
endPos = InStr(startPos,dummy,"return") -5
dummy = mid(dummy, startPos, endPos - startPos)
if objXMLHTTP.responseText > "" then%>
<p><b>Did you mean: </b>
<a href="search.asp?search=<%=dummy%>"><%=dummy%></a>
<b>?</b></p>
<%end if
End function %>
|