Как сопоставить шаблон для списка строк - PullRequest
0 голосов
/ 20 декабря 2018

Я пытаюсь найти и заменить текст на основе нечеткого соответствия следующим образом.

Цель

Я хочу сделать это для списка поиска и замен.Я не знаю, как расширить текущую функцию, чтобы это произошло.

Ввод

Ввод текста
 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» на «катетер»?

В идеале я хотел бы создать список соответствующих терминов, но тогда как мне также указать, как их заменить?

Ответы [ 2 ]

0 голосов
/ 20 декабря 2018

Определите asub для замены приближенных совпадений строкой замены и определите список совпадений L, который для каждого имени определяет его замену.Затем выполните Reduce для выполнения замен.

asub <- function(pattern, replacement, x, fixed = FALSE, ...) {
  m <- aregexec(pattern, x, fixed = fixed)
  r <- regmatches(x, m)
  lens <- lengths(r)
  if (all(lens == 0)) return(x) else
  replace(x, lens > 0, mapply(sub, r[lens > 0], replacement, x[lens > 0]))
}

L <- list("radiofrequency ablation" = "RFA", 
      "endoscopic submucosal resection" = "EMR",
      "HALO" = "cathetar")

Reduce(function(x, nm) asub(nm, L[[nm]], x), init = df$textcol, names(L))

, что дает:

[1] "In this substring would like to find the RFA of this cathetar"
[2] "I like to do EMR and also RFA"                                
[3] "No match here"                                                
[4] "No mention of this RFA thing"
0 голосов
/ 20 декабря 2018

Вы можете создать таблицу соответствия с шаблонами и необходимыми заменами:

dt <-
  data.table(
    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"
    )
  )

dt_gsub <- data.table(
  textcol = c("submucosal resection",
              "HALO",
              "radiofrequency ablation"),
  textcol2 = c("EMR", "catheter", "RFA")
)

for (i in 1:nrow(dt))
  for (j in 1:nrow(dt_gsub))
    dt[i]$textcol <-
  gsub(dt_gsub[j, textcol], dt_gsub[j, textcol2], dt[i, textcol])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...