Я пытаюсь найти и заменить текст на основе нечеткого соответствия следующим образом.
Цель
Я хочу сделать это для списка поиска и замен.Я не знаю, как расширить текущую функцию, чтобы это произошло.
Ввод
Ввод текста
df <- data.frame(textcol=c("In this substring would like to find the radiofrequency ablation of this HALO",
"I like to do endoscopic submuocsal resection and also radifrequency ablation",
"No match here","No mention of this radifreq7uency ablati0on thing"))
Попытка
##### Lower case the text ##########
df$textcol<-tolower(df$textcol)
#Need to define the pattern to match and what to replace it with
matchPattern <- "radiofrequency ablation"
findAndReplace<-function(matchPattern,rawText,replace)
{
positions <- aregexec(matchPattern, rawText, max.distance = 0.1)
regmatches(rawText, positions)
res <- regmatches(df$textcol, positions)
res[lengths(res)==0] <- "XXXX" # deal with 0 length matches somehow
#################### Term mapping ####################
df$out <- Vectorize(gsub)(unlist(res), replace, rawText)
df$out
}
matchPatternRFA <- c("radiofrequency ablation")
repRF<-findAndReplace(matchPatternRFA,rawText,"RFA")
repRF
Проблема Вышесказанное прекрасно работает для замены одного термина, но что если я захочу также заменить эндоскопическую «подслизистую резекцию» на «EMR» и «HALO» на «катетер»?
В идеале я хотел бы создать список соответствующих терминов, но тогда как мне также указать, как их заменить?