【问题标题】:Replace the exact sentence from the paragraph in R替换R中段落中的确切句子
【发布时间】:2020-05-17 13:55:06
【问题描述】:

我正在尝试匹配段落中的句子并替换它们。

下面是数据框 -

fulltext = c(rep("<span style=\"font-family:Calibri\"><span style=\"font-size:18px\">__ - Now</span>\r\n\r\n<strong><span style=\"font-size:24px\">X - Soon</span></strong>\r\n\r\n<span style=\"font-size:18px\">__ - N</span></span><span style=\"font-family:Calibri\"><span style=\"font-size:18px\">ext Scheduled Maintenance or Inspection</span></span>",3),
  "<span style=\"font-size:20px\"><strong><span style=\"font-family:&quot;Calibri&quot;,sans-serif\">What is Triggering this Expert Alert?</span></strong></span>")

cleantext = c("__ - Now", "X - Soon", "ext Scheduled Maintenance or Inspection", "What is Triggering this Expert Alert?")

replacetext = c("__ - Nu", "X - Binnenkort", "ext Gepland onderhoud of inspectie", "Wat veroorzaakt deze expertwaarschuwing?")
data5 = data.frame(fulltext, cleantext, replacetext)

这就是我想要做的 -

  1. 从cleantext中取出句子
  2. 与全文匹配
  3. 将 cleantext 替换为全文中的 replacetext

例如。 是什么触发了这个 daert 警报?

以上是完整的段落,我想用 Wat veroorzaakt deze Expertwaarschuwing 替换粗体句子?

输出应该看起来 - Wat veroorzaakt deze expertwaarschuwing?

这是我迄今为止尝试过的。现在我尝试了几种方法来做到这一点。

  1. 使用字符串替换
  2. 尝试将 ^ 和 $ 添加到句子的开头和结尾,然后使用 gsub 将其匹配为正则表达式模式。但我认为这只适用于文字。下面是我的尝试,但没有奏效。

data5$cleantext2 = paste0("^",data5$cleantext,"$") gsub(data1$Cleantext2[1], data1$replacetext[1], data1$fulltext[1])

【问题讨论】:

  • ^$ 会挫败你的尝试,因为它之前和之后似乎都有一些东西。您可以尝试使用单词边界,也许是paste0("\\b", data5$cleantext, "\\b")
  • 顺便说一句:gsub 一次只能处理一种模式;如果您需要在整个框架中执行此操作,可能是mapply(gsub, data1$Clean2, data1$dutch, data1$en_us)
  • 是的,我尝试使用 mapply 应用 gsub。它不按我想要的方式工作。我现在将尝试 paste0 解决方案。
  • @r2evans 添加 \\b 不起作用 - paste0("\\b", data5$cleantext, "\\b")
  • 哦,我认为这将是一个正则表达式解决方案。我正在用 R 和 Python 构建应用程序。

标签: python html r regex string


【解决方案1】:

不需要循环。此外,您的 ^$ 将不起作用,因为您的替换模式是中间字符串。您可以使用固定模式缓解不匹配问题。

由于您想将所有模式/替换应用于每个 fulltext(不仅仅是 1-for-1),那么我认为您可以 Reduce 它。

Reduce(function(s, ptn) gsub(ptn[1], ptn[2], s, fixed = TRUE), 
       Map(c, cleantext, replacetext),
       init = fulltext)
# [1] "<span style=\"font-family:Calibri\"><span style=\"font-size:18px\">__ - Nu</span>\r\n\r\n<strong><span style=\"font-size:24px\">X - Binnenkort</span></strong>\r\n\r\n<span style=\"font-size:18px\">__ - N</span></span><span style=\"font-family:Calibri\"><span style=\"font-size:18px\">ext Gepland onderhoud of inspectie</span></span>"
# [2] "<span style=\"font-family:Calibri\"><span style=\"font-size:18px\">__ - Nu</span>\r\n\r\n<strong><span style=\"font-size:24px\">X - Binnenkort</span></strong>\r\n\r\n<span style=\"font-size:18px\">__ - N</span></span><span style=\"font-family:Calibri\"><span style=\"font-size:18px\">ext Gepland onderhoud of inspectie</span></span>"
# [3] "<span style=\"font-family:Calibri\"><span style=\"font-size:18px\">__ - Nu</span>\r\n\r\n<strong><span style=\"font-size:24px\">X - Binnenkort</span></strong>\r\n\r\n<span style=\"font-size:18px\">__ - N</span></span><span style=\"font-family:Calibri\"><span style=\"font-size:18px\">ext Gepland onderhoud of inspectie</span></span>"
# [4] "<span style=\"font-size:20px\"><strong><span style=\"font-family:&quot;Calibri&quot;,sans-serif\">Wat veroorzaakt deze expertwaarschuwing?</span></strong></span>"                                                                                                                                                                           

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2014-02-26
    相关资源
    最近更新 更多