首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的文本列-尝试按顺序计数关键字

R中的文本列-尝试按顺序计数关键字
EN

Stack Overflow用户
提问于 2022-02-06 02:53:52
回答 3查看 153关注 0票数 0

我正在处理一个具有文本列的数据集。这篇文章有许多用分号分隔的句子。我正试图在dataframe的一个新列中获得一个单词计数,用于匹配我的关键字的单词。然而,在一句话中,如果有重复的关键字,则应只考虑一次。

例如-

  1. 关于电池和组件的第201节太阳能贸易案;关于进口商品
  2. 太阳能电池板的第201条关税、Tawian关税、贸易
  3. 贸易问题影响太阳能产业

的问题

是dataframe的一列中的文本。我的关键词包括太阳能,太阳能电池板,201节

我想计数每个句子中与我的关键字匹配的单词,但是如果句子中有两个或所有的单词,那么它只被计数一次。字数应该只考虑不同句子中的关键词。如果一个句子没有特定的关键字,那么就转向查找第二个关键字。

我的输出应该是- word_count 2(因为在两个句子中都提到了201节,我们不搜索太阳,因为关键字列表中的第一个单词匹配)1(因为只有太阳词)1(因为只有太阳词)

请提出解决这一问题的办法。这是我研究工作的一个关键部分。谢谢。

亲切的问候,佳肴

EN

回答 3

Stack Overflow用户

发布于 2022-02-06 05:45:18

我首先使用tidyr::separate()将文本列拆分为多个列;然后使用stringr::str_detect()或基正则表达式在每个列中搜索关键短语;然后使用rowSums(dplyr::across())跨列进行求和。就像这样:

代码语言:javascript
复制
library(tidyverse)
keywords <- paste("solar", "section 201", sep = "|")
df <- tibble(
  text = c(
    "The section 201 solar trade case on cells and modules; Issues relating to section 201 tariffs on imported goods",
"Solar panels; Tawian tariffs; trade",
"Trade issues impacting the solar industry"
  )
)

df <- df %>%
  separate(text, into = c("text1", "text2", "text3"), sep = ";", fill = "right") %>%
  mutate(
    count = rowSums(
      across(text1:text3, ~ str_detect(str_to_lower(.x), keywords)), 
      na.rm = TRUE
    )
  )

然后,count列包含您预测的结果:

代码语言:javascript
复制
df %>%
  select(count)

# A tibble: 3 x 1
#   count
#   <dbl>
# 1     2
# 2     1
# 3     1
票数 0
EN

Stack Overflow用户

发布于 2022-02-06 05:49:26

我认为对于你的例子中的每个数字,你想要考虑一个单独的列表项目。如果有分号,您希望将每个列表项目作为单独的句子来考虑。然后,您希望在每个列表项中查找关键字的第一次出现。它将成为该列表项的目标关键字。然后,您希望在每个列表项中的每个句子中只计算目标关键字的第一次出现:

代码语言:javascript
复制
  library(dplyr)
  library(stringr)
# I modified your example sentences to include "section 201" twice in sentence 2 in list item #1 to show it will only count once in that sentence
# I modified the order of your keywords, otherwise solar will be detected first in all sentences
  sentences <- list("The section 201 solar trade case on cells and modules; Issues relating to section 201 section 201 tariffs on imported goods", "Solar panels, Tawian tariffs, trade", "Trade issues impacting the solar industry")
  keywords <- c("section 201", "solar", "solar panels")
# Go through by each list item
  lapply(sentences, function(sentence){
# For each list item, sentences, split into separate sentences by ;
# Also I changed each sentence to lowercase, otherwise solar != Solar
    str_split(tolower(sentence), ";") -> split_string
# Apply each keyword and detect if the keyword is in the list_item using str_detect from stringr
    sapply(keywords, function(keyword) str_detect(split_string, keyword)) -> output 
# Output is the keywords that are in each list item
# Choose the first occurrence of a keyword: keywords[output == T][1]
# Detect in each sentence if the keyword is included, then sum the number of occurrences for each list item. Name as the keyword that was detected
      setNames(str_detect(split_string[[1]], keywords[output == T][1]) %>% sum(), keywords[output == T][1])
})

给出一个列表,其中识别了第一个出现的关键字,以及列表项中每个句子中该关键字的首次出现次数:

代码语言:javascript
复制
[[1]]
section 201 
          2 

[[2]]
solar 
    1 

[[3]]
solar 
    1 
票数 0
EN

Stack Overflow用户

发布于 2022-02-06 06:49:35

如果关键字列表不是太长(例如10或20个单词),那么您可以查看每个文本字符串的所有关键字的计数。我在每个文本字符串的末尾添加;,这样一个句子总是以;结尾。模式paste0("[^;]*", key, "[^;]*;")标识包含单词(存储在) key中的任何句子。

代码语言:javascript
复制
txt <- c("The section 201 solar trade case on cells and modules; Issues relating to section 201 tariffs on imported goods",
         "Solar panels, Tawian tariffs, trade",
         "Trade issues impacting the solar industry")

keys <- c("section 201", "solar panels", "solar")
counts <- sapply(keys, function(key) stringr::str_count(paste0(txt, ";"), regex(paste0("[^;]*", key, "[^;]*;"), ignore_case = T)))

接下来,您可以查看counts的每一行,并查看第一个非零元素,它应该是您要查找的值。

代码语言:javascript
复制
sapply(1:nrow(counts), function(i) {
  a <- counts[i, ]
  a[a != 0][1]
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71003859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档