Fra*_*xxx 1 r string-matching grepl data.table
我有一个数据帧df:
df <- structure(list(page = c(12, 6, 9, 65),
text = structure(c(4L,2L, 1L, 3L),
.Label = c("I just bought a brand new AudiA6", "Get 2 years engine replacement warranty on BMW X6",
"Volkswagen is the parent company of BMW", "ToyotaCorolla is offering new car exchange offers"),
class = "factor")), .Names = c("page","text"), row.names = c(NA, -4L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
另外,我有一个单词列表:
wordlist <- c("Audi", "BMW", "extended", "engine", "replacement", "Volkswagen", "company", "Toyota","exchange", "brand")
Run Code Online (Sandbox Code Playgroud)
我通过取消列出文本和使用grepl来查找wordlist中的单词是否存在于列文本中.
library(data.table)
setDT(df)[, match := paste(wordlist[unlist(lapply(wordlist, function(x) grepl(x, text, ignore.case = T)))], collapse = ","), by = 1:nrow(df)]
Run Code Online (Sandbox Code Playgroud)
问题是,我想找到列文本中出现的词汇表的确切单词.使用grepl它也显示部分匹配的单词,例如来自文本的AudiA6也部分匹配单词列表中的奥迪单词.此外,我的数据帧非常大,使用grepl需要花费大量时间来运行代码.如果可能的话,请推荐任何其他方法.我想要这样的东西:
df <- structure(list(page = c(12, 6, 9, 65),
text = structure(c(4L,2L, 1L, 3L),
.Label = c("I just bought a brand new AudiA6", "Get 2 years engine replacement warranty on BMW X6",
"Volkswagen is the parent company of BMW", "ToyotaCorolla is offering new car exchange offers"),
class = "factor"), match = c("exchange", "BMW,engine,replacement",
"brand", "BMW,Volkswagen,company")), row.names = c(NA, -4L),
class = c("data.table", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
您可以在为要提取的每个单词添加单词边界()之后使用str_extract_all,因此只考虑完整匹配(并且您需要折叠所有单词以指示"或"):stringr\\b"|"
sapply(stringr::str_extract_all(df$text, paste("\\b", wordlist, "\\b", sep="", collapse="|")), paste, collapse=",")
# [1] "exchange" "engine,replacement,BMW" "brand" "Volkswagen,company,BMW"
Run Code Online (Sandbox Code Playgroud)
如果你想把它放在你的data.table:
df[, match:=sapply(stringr::str_extract_all(text, paste("\\b", wordlist, "\\b", sep="", collapse="|")), paste, collapse=",")]
df
# page text match
#1: 12 ToyotaCorolla is offering new car exchange offers exchange
#2: 6 Get 2 years engine replacement warranty on BMW X6 engine,replacement,BMW
#3: 9 I just bought a brand new AudiA6 brand
#4: 65 Volkswagen is the parent company of BMW Volkswagen,company,BMW
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |