R 中预定义单词列表的颜色突出显示文本

Sam*_* S. 4 text r highlight cpu-word

假设我有一个文档集合,例如:

text = c("is it possible to highlight text for some words" , 
      "suppose i want words like words to be red and words like text to be blue")
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以使用 R 的预定义单词列表的颜色来突出显示文档(特别是对于大型语料库)。列表中的每个单词都会获得特定的颜色。例如,将“单词”突出显示为红色,将“文本”突出显示为蓝色,如下所示。

在此输入图像描述

Ofe*_*lia 7

这是完整的调试应用程序代码!

首先,需要的库:

library(shiny)
library(tidyverse)
library(DT)
library(magrittr)
Run Code Online (Sandbox Code Playgroud)

然后,添加HTML标签的函数:

wordHighlight <- function(SuspWord,colH = 'yellow') {
  paste0('<span style="background-color:',colH,'">',SuspWord,'</span>')
}
Run Code Online (Sandbox Code Playgroud)

现在UI部分:

ui <- fluidPage(
   titlePanel("Text Highlighting"),
   sidebarLayout(
      sidebarPanel(
        textInput("wordSearch", "Word Search")
      ),
    mainPanel(
        DT::dataTableOutput("table")
      )   
   )  
   )
Run Code Online (Sandbox Code Playgroud)

最后,在服务器端:

server <- function(input, output) {
   sentence <- "The term 'data science' (originally used interchangeably with 'datalogy') has existed for over thirty years and was used initially as a substitute for computer science by Peter Naur in 1960."
   sentence2 = "One of the things we will want to do most often for social science analyses of text data is generate a document-term matrix."
   YourData = data.frame(N = c('001','002'), T = c(sentence,sentence2), stringsAsFactors=FALSE)
   highlightData <- reactive({
     if (input$wordSearch!="")
      {
        patterns = input$wordSearch
        YourData2 = YourData
        YourData2[,2] %<>% str_replace_all(regex(patterns, ignore_case = TRUE), wordHighlight)
        return(YourData2)
      }
    return(YourData)
  })
    output$table <- DT::renderDataTable({
      data <- highlightData() 
    }, escape = FALSE)
}
Run Code Online (Sandbox Code Playgroud)

运行应用程序:

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)


Ind*_*til 5

对于这个问题来说,这是一个有点黑客的解决方案,对于大型语料库来说扩展性不太好。我很好奇是否有一种更简洁、更优雅、更可扩展的方法来做到这一点。

library(tidyverse)
library(crayon)

# define text
text <- c("is it possible to highlight text for some words" , 
         "suppose i want words like words to be red and words like text to be blue")

# individuate words
unique_words <- function(x) {
  purrr::map(.x = x,
             .f = ~ unique(base::strsplit(x = ., split = " ")[[1]],
                           collapse = " "))
}

# creating a dataframe with crayonized text
df <- 
  tibble::enframe(unique_words(x = text)) %>%
  tidyr::unnest() %>%
# here you can specify the color/word combinations you need 
  dplyr::mutate(.data = .,
                value2 = dplyr::case_when(value == "text" ~ crayon::blue(value),
                                          value == "words" ~ crayon::red(value),
                TRUE ~ value)) %>%
  dplyr::select(., -value) 

# printing the text
print(cat(df$value2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

PS 不幸的是,reprex它不适用于彩色文本,因此无法生成完整的表示。