Skip to content

Instantly share code, notes, and snippets.

@flovv
Created October 4, 2015 15:23
Show Gist options
  • Save flovv/09eff77ad7d36ae9e3b8 to your computer and use it in GitHub Desktop.
Save flovv/09eff77ad7d36ae9e3b8 to your computer and use it in GitHub Desktop.
Spelling correction using google's "did you mean ..." function
library(RCurl)
didYouMean=function(input){
input=gsub(" ", "+", input)
doc=getURL(paste("http://www.google.com/search?q=",input,"/", sep=""))
dym=gregexpr(pattern ='Did you mean',doc)
srf=gregexpr(pattern ='Showing results for',doc)
if(length(dym[[1]])>1){
doc2=substring(doc,dym[[1]][1],dym[[1]][1]+1000)
s1=gregexpr("?q=",doc2)
s2=gregexpr("/&",doc2)
new.text=substring(doc2,s1[[1]][1]+2,s2[[1]][1]-1)
return(gsub("[+]"," ",new.text))
break
}
else if(srf[[1]][1]!=-1){
doc2=substring(doc,srf[[1]][1],srf[[1]][1]+1000)
s1=gregexpr("?q=",doc2)
s2=gregexpr("/&",doc2)
new.text=substring(doc2,s1[[1]][1]+2,s2[[1]][1]-1)
return(gsub("[+]"," ",new.text))
break
}
else(return(gsub("[+]"," ",input)))
}
didYouMean("hambburg")
didYouMean('campary')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment