我的数据框中包含拼写正确且错误的单词以及从用户收集的单独的单词列表。我需要检查每个单词并从数据框中找到拼写正确的版本。
下面的代码可以正常工作,并且完全按照我的要求进行,但由于我正在使用的数据类型,因此需要进行过多的近似,我需要它来准确匹配单词。有谁知道我怎么能这样做?
TDM.frame
是根据用户输入生成的术语文档矩阵,是一个包含数千个条目的csv。
spellDB <- read.csv("spellcheck.csv")
words <- row.names(TDM.frame)
k <- 0
wordLoc <- NULL
badWord <- NULL
goodWord <-NULL
for (i in 1:nrow(TDM.frame)){
if(length(grep(words[i],spellDB$Incorrect))>0){
k <- k + 1
wordLoc[k] <- grep(words[i],spellDB$Incorrect,fixed = TRUE)
badWord[k] <- words[i]
goodWord[k] <- as.character(spellDB$Correct[wordLoc[k]])
corrections <- cbind(goodWord,badWord)
}
}
这输出以下内容:
> corrections
goodWord badWord
[1,] "account" "accounts"
[2,] "account" "accout"
[3,] "activate" "act"
[4,] "faction" "action"
[5,] "activate" "activate"
[6,] "activate" "activated"
1,2,5和6是正确的,因为它们在spellDB
但是3和4不是那样不应该匹配。
我也试过使用这个(以及其他)Regex但是这根本不起作用 - 我得到的唯一结果是整数(0)
grep(paste0("?=.\\b",words[12],"\\b"),spellDB$Incorrect)
我的目标是纠正数据的拼写,以便术语文档矩阵正确并且单词计数准确,如果有更好的方法来做到这一点那么好,这感觉就像处理它的混乱方式但我我是R的新手,但却未能找到替代品。
感谢阅读!
编辑: 我引用的单词列表是1143个条目,但是头部是:
> head(words)
[1] "absolute" "absolutely" "acceptedcompleted" "accidently" "accounts" "accout"
spellDB
读起来像这样:
Correct Incorrect
1 ability abilities
2 account aacount accound accoun accountc acount accout accounts
3 adventure adventur adventures
4 amazon amazoncom amazonid amazons
5 android andoid
6 apple appleid
EDIT2:
可悲的是,我不能发布所有的dput,因为其中一些是敏感数据..但是我已经删除了有问题的单词,因为它们无论如何都不相关......
> dput(head(spellDB))
structure(list(Correct = structure(c(1L, 2L, 5L, 8L, 9L, 10L), .Label = c("ability",
"account", "achievment", "activate", "adventure"), class = "factor"),
Incorrect = structure(c(4L, 3L, 119L, 120L, 121L, 122L), .Label = c("",
"", " aacount accound accoun accountc acount accout accounts ",
" abilities ", " acheiv acheivements acheivment achi achiement achiev achievcement achieve achieved achieveent achievements achievemetn achievments achievmnet achiv achive achived achivement achivements achivment achivmenti achivments achv achviement avhivemnt",
"andoid", "appleid", "cbind"), class = "factor")), .Names = c("Correct",
"Incorrect"), row.names = c(NA, 6L), class = "data.frame")`
答案 0 :(得分:1)
您的dput
数据对我不起作用,所以我重新创建了它:
spellDB <- read.table(text=" Correct, Incorrect
ability, abilities
account, aacount accound accoun accountc acount accout accounts
adventure, adventur adventures
amazon, amazoncom amazonid amazons
android , andoid
apple, appleid", sep=",", as.is=T, header=T)
spellDB[,1] <- gsub(" +", " ", spellDB[,1])
spellDB[,1] <- gsub("^\\s", "", spellDB[,1])
spellDB[,2] <- gsub(" +", " ", spellDB[,2])
spellDB[,2] <- gsub("^\\s|\\s$", "", spellDB[,2])
此解决方案有效,但我不确定它对您的行数非常有效。它通过检查每个单词是否存在于大黑名单中来工作,如果是,它会找到相应的正确单词并添加到新的向量中。
incorrects.list <- strsplit(spellDB$Incorrect, " ")
incorrects.unlist <- unlist(incorrects.list)
words <- c("absolute","absolutely","acceptedcompleted","accidently", "accounts","accout")
newwords <- rep(NA, length(words))
for (w in 1:length(words)) {
if (words[w] %in% incorrects.unlist) {
pos <- sapply(seq_along(incorrects.list), function(i) (words[w] %in% incorrects.list[[i]]))
newwords[w] <- spellDB$Correct[pos]
} else {
newwords[w] <- words[w]
}
}