简体   繁体   English

替换 R 中段落中的确切句子

[英]Replace the exact sentence from the paragraph in R

I am trying to match the sentence from a paragraph and replace them.我正在尝试匹配段落中的句子并替换它们。

Below is the Dataframe -以下是数据框 -

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)

This is what I am trying to do -这就是我想要做的 -

  1. Take the sentence from cleantext从cleantext中取出句子
  2. Match with fulltext与全文匹配
  3. replace cleantext with replacetext in fulltext在全文中用replacetext替换cleantext

For example.例如。 <span style=\\"font-size:20px\\"><strong><sp an style=\\"font-family:"Calibri",sans-serif\\" > What is Triggering this daert Alert? <span style=\\"font-size:20px\\"><strong><sp an style=\\"font-family:"Calibri",sans-serif\\" >是什么触发了这个 daert 警报? </ span></strong></ span> </span></strong></span>

Above is the complete paragraph, I want to replace the sentence in bold with Wat veroorzaakt deze expertwaarschuwing?以上是完整的段落,我想用Wat veroorzaakt deze Expertwaarschuwing替换粗体的句子

Output Should look - <span style=\\"font-size:20px\\"><strong><sp an style=\\"font-family:"Calibri",sans-serif\\" > Wat veroorzaakt deze expertwaarschuwing?输出应该看起来 - <span style=\\"font-size:20px\\"><strong><sp an style=\\"font-family:"Calibri",sans-serif\\" > Wat veroorzaakt deze Expertwaarschuwing? </ span></strong></ span> </span></strong></span>

This is what I have tried so far.这是我到目前为止所尝试的。 Now I have tried a couple of ways to do it.现在我已经尝试了几种方法来做到这一点。

  1. using string replace使用字符串替换
  2. tried adding ^ and $ to the start and end of the sentence, then use gsub to match that as regex pattern.尝试将 ^ 和 $ 添加到句子的开头和结尾,然后使用 gsub 将其匹配为正则表达式模式。 But I think that works only with words.但我认为这仅适用于文字。 Below is my try, but it did not work.下面是我的尝试,但没有奏效。

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

No loop necessary.不需要循环。 Also, your ^ and $ are not going to work since your replacement patterns are mid-string.此外,您的^$将不起作用,因为您的替换模式是中间字符串。 You can mitigate mis-matches with fixed patterns.您可以使用固定模式减少不匹配。

Since you want to apply all patterns/replacements to each of fulltext (not just a 1-for-1), then I think you can Reduce it.由于您想将所有模式/替换应用于每个fulltext (不仅仅是fulltext ),那么我认为您可以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>"                                                                                                                                                                           

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

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