我正在处理一个具有文本列的数据集。这篇文章有许多用分号分隔的句子。我正试图在dataframe的一个新列中获得一个单词计数,用于匹配我的关键字的单词。然而,在一句话中,如果有重复的关键字,则应只考虑一次。
例如-
的问题
是dataframe的一列中的文本。我的关键词包括太阳能,太阳能电池板,201节
我想计数每个句子中与我的关键字匹配的单词,但是如果句子中有两个或所有的单词,那么它只被计数一次。字数应该只考虑不同句子中的关键词。如果一个句子没有特定的关键字,那么就转向查找第二个关键字。
我的输出应该是- word_count 2(因为在两个句子中都提到了201节,我们不搜索太阳,因为关键字列表中的第一个单词匹配)1(因为只有太阳词)1(因为只有太阳词)
请提出解决这一问题的办法。这是我研究工作的一个关键部分。谢谢。
亲切的问候,佳肴
发布于 2022-02-06 05:45:18
我首先使用tidyr::separate()将文本列拆分为多个列;然后使用stringr::str_detect()或基正则表达式在每个列中搜索关键短语;然后使用rowSums(dplyr::across())跨列进行求和。就像这样:
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列包含您预测的结果:
df %>%
select(count)
# A tibble: 3 x 1
# count
# <dbl>
# 1 2
# 2 1
# 3 1发布于 2022-02-06 05:49:26
我认为对于你的例子中的每个数字,你想要考虑一个单独的列表项目。如果有分号,您希望将每个列表项目作为单独的句子来考虑。然后,您希望在每个列表项中查找关键字的第一次出现。它将成为该列表项的目标关键字。然后,您希望在每个列表项中的每个句子中只计算目标关键字的第一次出现:
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])
})给出一个列表,其中识别了第一个出现的关键字,以及列表项中每个句子中该关键字的首次出现次数:
[[1]]
section 201
2
[[2]]
solar
1
[[3]]
solar
1 发布于 2022-02-06 06:49:35
如果关键字列表不是太长(例如10或20个单词),那么您可以查看每个文本字符串的所有关键字的计数。我在每个文本字符串的末尾添加;,这样一个句子总是以;结尾。模式paste0("[^;]*", key, "[^;]*;")标识包含单词(存储在) key中的任何句子。
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的每一行,并查看第一个非零元素,它应该是您要查找的值。
sapply(1:nrow(counts), function(i) {
a <- counts[i, ]
a[a != 0][1]
})https://stackoverflow.com/questions/71003859
复制相似问题