首页 > 解决方案 > paste0() 中的 Html 标记用作 str_replace_all 中的替换,使用 r

问题描述

例句

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')

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

例子:

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

标签: rregexstringr

解决方案


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

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."

推荐阅读