r - 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>')
解决方案
使用实体名称和颜色创建一个数据框,从解析的句子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."
推荐阅读
- javascript - Angular 组件与 Input() 的交互未同步
- c++ - Qt - 为 QML 定义全局函数
- r - 如何将文本文件中的“Inf”字符串转换为数字
- netsuite - Netsuite 脚本组合/合并多个语句
- javascript - Vue.js 中 JSON 对象的值
- node.js - 从Angular 5中的http请求获取数据
- python - 异常类型:MissingSchema / beautifulsoup
- c# - ASP.net WebAPI Post 方法在发布时返回 405
- python - Python 轮子包 Linux vs windows
- sqlite - 为什么生成的变量之间的 SQLite 条件不能正确比较?