识别 R 中数据框中的无意义或乱码文本.有没有办法将字符串/单词部分匹配到字典



我希望在我的数据框中创建一个变量(列),用于识别可疑的无意义文本(例如"asdkjhfas"),或相反。这是通用脚本的一部分,可帮助我的团队清理调查数据。

我在stackoverflow上找到的一个功能(下面的链接和信用)允许我将单个单词与字典匹配,它不会识别多个单词。

有什么方法可以与字典进行部分匹配(而不是严格匹配)吗?

library(qdapDictionaries) # install.packages(qdap)
is.word  <- function(x) x %in% GradyAugmented
x <- c(1, 2, 3, 4, 5, 6)
y <- c("this is text", "word", "random", "Coca-cola", "this is meaningful                 
asdfasdf", "sadfsdf")
df <- data.frame(x,y)

df$z  [is.word(df$y)] <- TRUE
df

在一个完美的世界里,我会得到一个专栏:df$z 真真真

我的实际结果是: df$z NA TRUE TRUE NA NA

NA我会非常满意: df$z 真真真纳真纳真纳

我发现函数是.word在这里 从 R 中的语料库中删除无意义的单词 感谢用户帕斯

这适用于 dplyr 和 tidytext。比我预期的要长一点。某处可能有捷径。

我检查句子中是否有单词并计算 TRUE 值的数量。如果此值大于 0,则其中包含文本,否则不包含。

library(tidytext)
library(dplyr)
df %>% unnest_tokens(words, y) %>% 
mutate(text = words %in% GradyAugmented) %>% 
group_by(x) %>% 
summarise(z = sum(text)) %>% 
inner_join(df) %>% 
mutate(z = if_else(z > 0, TRUE, FALSE))

Joining, by = "x"
# A tibble: 6 x 3
x z     y                          
<dbl> <lgl> <chr>                      
1     1 TRUE  this is text               
2     2 TRUE  word                       
3     3 TRUE  random                     
4     4 TRUE  Coca-cola                  
5     5 TRUE  this is meaningful asdfasdf
6     6 FALSE sadfsdf     

这是一个使用purrr的解决方案(以及dplyrstringr):

library(tidyverse)
your_data <- tibble(text = c("this is text", "word", "random", "Coca-cola", "this is meaningful asdfasdf", "sadfsdf"))
your_data %>%
# split the text on spaces and punctuation
mutate(text_split = str_split(text, "\s|[:punct:]")) %>% 
# see if some element of the provided text is an element of your dictionary
mutate(meaningful = map_lgl(text_split, some, is.element, GradyAugmented)) 
# A tibble: 6 x 3
text                        text_split meaningful
<chr>                       <list>     <lgl>     
1 this is text                <chr [3]>  TRUE      
2 word                        <chr [1]>  TRUE      
3 random                      <chr [1]>  TRUE      
4 Coca-cola                   <chr [2]>  TRUE      
5 this is meaningful asdfasdf <chr [4]>  TRUE      
6 sadfsdf                     <chr [1]>  FALSE     

谢谢,@Ben G & @phiver

两种解决方案都奏效了。需要注意的一件事是,tidytext仅适用于tibbles。我做了一些微小的调整以将其恢复到数据框中,并认为我也会分享(以防万一其他人需要这种格式)。

x <- c(1, 2, 3, 4, 5, 6)
y <- c("this is text", "word", "random", "Coca-cola", "this is meaningful asdfasdf", 
"sadfsdf")
my_tibble <- tibble(x,y)
my_tibble_new = my_tibble %>%
unnest_tokens(output=word, input="y", token = "words") %>%
mutate(text = word %in% GradyAugmented) %>%
group_by(x) %>%
summarise(z = sum(text)) %>%
inner_join(my_tibble) %>%
mutate(z = if_else(z > 0, TRUE, FALSE))
df = as.data.frame(my_tibble_new)

最新更新