[英]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:"Calibri",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 -这就是我想要做的 -
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.
现在我已经尝试了几种方法来做到这一点。
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:"Calibri",sans-serif\">Wat veroorzaakt deze expertwaarschuwing?</span></strong></span>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.