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

koaltpgm  于 2023-01-28  发布在  其他
关注(0)|答案(4)|浏览(145)

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

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")

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

fkvaft9z

fkvaft9z1#

这里是完整的调试应用程序代码!
第一,所需的库:

library(shiny)
library(tidyverse)
library(DT)
library(magrittr)

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

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

当前UI部分:

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

最后,在服务器端:

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)
}

运行应用程序:

shinyApp(ui = ui, server = server)
wnavrhmk

wnavrhmk2#

对于这个问题,这是一个有点笨拙的解决方案,对于大型语料库来说不是很容易扩展。我很好奇是否有一个更简洁、优雅和可扩展的方法来实现这一点。

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))

不幸的是,reprex不能处理彩色文本,所以不能生成完整的reprex。

lhcgjxsq

lhcgjxsq3#

Indrajeet的回答很棒。这是一个基于Indrajeet的答案的答案,只是一点点变化。

unique_words <- lapply(strsplit(text, " "), function(x){x[!x ==""]})

# creating a dataframe with crayonized text
df <- 
  tibble::enframe(unique_words) %>%
  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)

在两条不同的线(Collapse text by group in data frame)中具有输出:

df <- data.table(df)
df <- df[, list(text = paste(value2, collapse=" ")), by = name]

如果我想在R控制台中打印,答案看起来可以。如果我想在R shinyapp中输出,它是如何工作的?
寻找其他替代方案,并感谢您的帮助。

jvlzgdj9

jvlzgdj94#

这是突出显示文档中存在的单词列表的代码。这段代码在Rmarkdown中用于创建word文档
{r突出显示=真}
单词列表〈- c(“苹果”、“香蕉”、“梨”)

相关问题