Создайте фрейм данных с именами и цветом сущностей, создайте динамическое регулярное выражение из столбца синтаксического предложения 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."