简体   繁体   English

如何用 R 中的正确单词替换拼写错误的单词

[英]How to replace the wrong spelt word with the correct one in R

I know similar question might have asked but I feel my requirement is peculiar.我知道可能会问类似的问题,但我觉得我的要求很特殊。 I have two data frames: one with wrongly spelt words and another data frame with corrected words.我有两个数据框:一个带有拼写错误的单词,另一个带有更正单词的数据框。

I need to replace each incorrect word with the correct word in another data frame.我需要用另一个数据框中的正确单词替换每个不正确的单词。 Could you please let me know if there is any best possible way.如果有最好的方法,请告诉我。

a <- data.frame(reported_terms=c('abdome pain','adominal ache','adomen'),
                stringsAsFactors = FALSE)

b <- data.frame(wrong=c('adomen','adominal','abdome'),correct=c('abdomen','abdominal','abdomen'),
                stringsAsFactors = FALSE)

I am trying with the following code but not getting the expected output我正在尝试使用以下代码,但没有得到预期的 output

corr_report_terms=list() # created empty list.

for(i in a){
  str_split(i," ")
  if(any(i %in% b))
    corr_report_terms <- b$correct
}

Expected output:预期 output:

abdomen pain
abdominal ache
abdomen

Tricky but works:棘手但有效:

library(stringi) 
stri_replace_all_regex(a$reported_terms, "\\b"%s+%b$wrong%s+%"\\b", b$correct, vectorize_all=FALSE)
[1] "abdomen pain"   "abdominal ache" "abdomen"   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM