首页 > 其他 > 详细

基于R的拼写纠正器

时间:2017-01-16 22:48:54      阅读:316      评论:0      收藏:0      [点我收藏+]
mom=readLines("http://www.norvig.com/big.txt")
mom1=tolower(paste(mom,collapse=" "))
mom2=strsplit(mom1,"[^a-z]+")
mom3=sort(table(mom2),decreasing = TRUE)
want=names(mom3)

corrector=function(needCorrect){
  y=want[adist(needCorrect,want)==min(adist(needCorrect,want),2)]
  return(c(y,needCorrect)[1])
}

> corrector("hia")
[1] "his"

> corrector("andd")
[1] "and"

> corrector("fof")
[1] "of"

 

Modify a little bit, only the word with same letters(if it exist) will be returned.

mom=readLines("http://www.norvig.com/big.txt")
mom1=tolower(paste(mom,collapse=" "))
mom2=strsplit(mom1,"[^a-z]+")
mom3=sort(table(mom2),decreasing = TRUE)
want=names(mom3)

corrector=function(needCorrect){
  y=want[adist(needCorrect,want)==min(adist(needCorrect,want),2)]
  if (length(y)>1) {
      y=y[nchar(y)==nchar(needCorrect)]
  }
  return(c(y,needCorrect)[1])
}

> corrector("fof")
[1] "for"

 

 

Reference:

http://norvig.com/spell-correct.html

http://www.sumsar.net/blog/2014/12/peter-norvigs-spell-checker-in-two-lines-of-r/

 

yant07

基于R的拼写纠正器

原文:http://www.cnblogs.com/yant07/p/6291115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!