Created
October 4, 2015 15:23
-
-
Save flovv/09eff77ad7d36ae9e3b8 to your computer and use it in GitHub Desktop.
Spelling correction using google's "did you mean ..." function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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