在HTML中对文本进行着色和高亮显示,用于R

在HTML中对文本进行着色和高亮显示,用于R,html,r,colors,shiny,highlight,Html,R,Colors,Shiny,Highlight,我在一个大型语料库上运行了一系列NLP算法,我想探索我的结果。换句话说,我希望能够基于模型提取的功能以HTML格式格式化文本,以便在一个闪亮的web应用程序中查看它们,如下所示: 因为我对HTML一无所知,你能通过告诉我应该考虑哪些方式来指导我的研究吗?是否存在一些R包来执行此类任务?有光泽的功能是否足够?如果是,哪些功能?不确定这是否准确回答了问题,但可能与此相关: require(tidyverse) require(spacyr) require(shiny) tempDir <

我在一个大型语料库上运行了一系列NLP算法,我想探索我的结果。换句话说,我希望能够基于模型提取的功能以HTML格式格式化文本,以便在一个闪亮的web应用程序中查看它们,如下所示:


因为我对HTML一无所知,你能通过告诉我应该考虑哪些方式来指导我的研究吗?是否存在一些R包来执行此类任务?有光泽的功能是否足够?如果是,哪些功能?

不确定这是否准确回答了问题,但可能与此相关:

require(tidyverse)
require(spacyr)
require(shiny)

tempDir <- tempfile()
dir.create(tempDir)
htmlFile <- file.path(tempDir, "index.html")
viewer <- getOption("viewer")

s = spacy_parse(sample(stringr::sentences, 25), dependency=T, nounphrase=T) %>% as_tibble()
print(s)

# A tibble: 224 x 11
   doc_id sentence_id token_id token lemma pos   head_token_id dep_rel entity nounphrase
   <chr>        <int>    <int> <chr> <chr> <chr>         <dbl> <chr>   <chr>  <chr>     
 1 text1            1        1 Brea… brea… VERB              1 ROOT    ""     ""        
 2 text1            1        2 deep  deep  ADJ               1 acomp   ""     ""        
 3 text1            1        3 and   and   CCONJ             1 cc      ""     ""        
 4 text1            1        4 smell smell VERB              1 conj    ""     ""        
 5 text1            1        5 the   the   DET               7 det     ""     "beg"     
 6 text1            1        6 piny  piny  ADJ               7 amod    ""     "mid"     
 7 text1            1        7 air   air   NOUN              4 dobj    ""     "end_root"
 8 text1            1        8 .     .     PUNCT             1 punct   ""     ""        
 9 text10           1        1 Fill  fill  VERB              1 ROOT    ""     ""        
10 text10           1        2 the   the   DET               4 det     ""     "beg"     
# … with 214 more rows, and 1 more variable: whitespace <lgl>

cols = sample(rainbow(15))
pos_types = s %>% count(pos, sort=T) %>% print() %>% .[['pos']]

sink(htmlFile)
cat('<h4>')
  for(i in 1:nrow(s)){
    col = cols[match(s$pos[i], pos_types)]
    cat(as.character(span(s$token[i], style=glue::glue('color:{col}'), 
                          title = paste(s$pos[i], s$dep_rel[i]))))
    cat(' ')
  }
cat('</h4>')
sink()

viewer(htmlFile)

我建议上传一个例子,因为这是一个相当普遍的问题。一个有用的线索可能是使用span函数包装单词,并添加style参数,如:spanyour_string,style=color:red;非常感谢yanirmor这是我需要开始的