【问题标题】:Html tag inside paste0() function as replacement in str_replace_all using rpaste0() 中的 Html 标记用作 str_replace_all 中的替换,使用 r
【发布时间】:2019-02-24 23:32:47
【问题描述】:

例句

sentence <-'When Sebastian Thrun started at Google in 2007, few people outside of the company took him seriously.'

这是从 spacyr 包中提取的实体:

spacy_extract_entity(sentence)

我想为 ent_type 分配我自己的颜色的查找列表

ent_type <- c('PERSON', 'ORG',  'DATE')
color    <- c('#a3de2a', '#45c4f9', '#2ebaad')

如何返回 ent_type 和颜色值,并在句子中使用 paste0() 函数替换为 str_replace_all 中的 html 标记。

示例:

paste0('<span style=\"background-color:', color, '\ ">',text,' #<span style=\"font-size:8px;font-weight:bold;background-color:white;">',ent_type,'</span></span>')

【问题讨论】:

  • 将示例输出显示为代码而不是图像。

标签: r regex stringr


【解决方案1】:

使用实体名称和颜色创建一个数据框,从解析的句子text 列中构建一个动态正则表达式,同时转义每个值并将替代链从最长到最短排序,然后将其全部插入@987654322 @:

library(spacyr)
library(stringr)

## Escaping function
regex.escape <- function(string) {
  gsub("([][{}()+*^${|\\\\?.])", "\\\\\\1", string)
}
## Sorting by length in the descending order function
sort.by.length.desc <- function (v) v[order( -nchar(v)) ]

## Input data
sentence <- "IBM is an MNC with headquarters in New York. Oracle is a cloud company in California.James When Sebastian Thrun started at Google in 2007, works in IBM. Oracle hired John for cloud expertise. They give 100% to their profession."
ent_type <- c('PERSON', 'ORG',  'DATE')
color    <- c('#a3de2a', '#45c4f9', '#2ebaad')
ec <- data.frame(ent_type, color)             ## Dataframe with color built
e <- spacy_extract_entity(sentence)

## Build the regex pattern
pat <- paste(regex.escape(sort.by.length.desc(e$text)), collapse="|")
#pat <- paste0("\\b(?:", paste(regex.escape(e$text), collapse="|"), ")\\b") # If whole word search needed use this pattern


str_replace_all(sentence, pat, function(x) 
  paste0('<span style="background-color:', ec$color[ec$ent_type==e$ent_type[e$text == x][1]][1], ' ">',x,' #<span style="font-size:8px;font-weight:bold;background-color:white;">',e$ent_type[e$text == x][1],'</span></span>')
)
## => [1] "<span style=\"background-color:#45c4f9 \">IBM #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">ORG</span></span> is an <span style=\"background-color:#45c4f9 \">MNC #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">ORG</span></span> with headquarters in <span style=\"background-color:NA \">New York #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">GPE</span></span>. Oracle is a cloud company in <span style=\"background-color:NA \">California #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">GPE</span></span>.<span style=\"background-color:#a3de2a \">James When #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">PERSON</span></span> <span style=\"background-color:#a3de2a \">Sebastian Thrun #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">PERSON</span></span> started at <span style=\"background-color:#45c4f9 \">Google #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">ORG</span></span> in <span style=\"background-color:#2ebaad \">2007 #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">DATE</span></span>, works in <span style=\"background-color:#45c4f9 \">IBM #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">ORG</span></span>. Oracle hired <span style=\"background-color:#a3de2a \">John #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">PERSON</span></span> for cloud expertise. They give <span style=\"background-color:NA \">100% #<span style=\"font-size:8px;font-weight:bold;background-color:white;\">PERCENT</span></span> to their profession."

【讨论】:

  • 谢谢。我想问如果我的句子超过25个单词,为什么会出现错误。以下是错误消息:字符串 [[i]] *vtmp* 中的错误:提供的元素多于要替换的元素。
  • @bea 将数据添加到问题中以便我可以重现。
  • 这是我的例句:“IBM 是一家跨国公司,总部位于纽约。Oracle 是一家位于加利福尼亚的云公司。James Sebastian Thrun 2007 年在 Google 开始工作时,在 IBM 工作。Oracle 聘请了 John云专业知识。他们为自己的专业付出了 100%。”
  • @bea 抱歉,我没有预见到text clolumn 中有任何欺骗行为。您可以先对其进行重复数据删除,或者直接使用我更新的解决方案。
猜你喜欢
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多