paste0()中的HTML标记用作使用r在str_replace_all中的替换

时间:2019-02-24 23:32:48

标签: r regex stringr

例句>

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

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

spacy_extract_entity(sentence)

enter image description here

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

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

我如何返回ent_type和color值,并使用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>')

1 个答案:

答案 0 :(得分:1)

使用实体名称和颜色创建一个数据框,从解析的句子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."