繁体   English   中英

在R中的小标题内用空格替换单词而不进行反连接

[英]Replacing words with spaces within a tibble in R without anti-join

我有类似这样的句子:

小标题:1,782 x 1

Chat
<chr>                                                                                                                                                                    
1 Hi i would like to find out more about the trials
2 Hello I had a guest 
3 Hello my friend overseas right now
...

我想做的是删除停用词,例如“ I”,“ hello”。 我已经有了它们的列表,并且想用空格替换这些停用词。 我尝试使用mutate和gsub,但它只接受一个正则表达式。 反连接在这里不起作用,因为我正在尝试使用双字组/三字组。我没有一个单词列来反连接停用词。

有没有办法替换R中每个句子中的所有这些单词?

我们可以replace标记的嵌套,用空格( " "replace 'stop_words''word'列中找到的'word',并在按'lines'分组后paste 'word'

library(tidytext)
library(tidyverse)
rowid_to_column(df1, 'lines') %>% 
     unnest_tokens(word, Chat) %>% 
     mutate(word = replace(word, word %in% stop_words$word, " ")) %>% 
     group_by(lines) %>% 
     summarise(Chat = paste(word, collapse=' ')) %>%
     ungroup %>%
     select(-lines)

注意:这会将“ stop_words”数据集中找到的停用词替换为" "如果我们只需要替换停用词的自定义子集,则创建这些元素的vector并在mutate步骤中进行更改

v1 <- c("I", "hello", "Hi")
rowid_to_column(df1, 'lines') %>%
  ...
  ...
  mutate(word = replace(word %in% v1, " ")) %>%
  ...
  ...

我们可以使用“ \\\\b停用词\\\\b ”构造一个模式,然后使用gsub将其替换为“”。 这是一个例子。 请注意,我将ignore.case = TRUE设置为同时包含小写和大写字母,但是您可能需要根据需要进行调整。

dat <- read.table(text = "Chat
                  1 'Hi i would like to find out more about the trials'
                  2 'Hello I had a guest' 
                  3 'Hello my friend overseas right now'",
                  header = TRUE, stringsAsFactors = FALSE)

dat
#                                                Chat
# 1 Hi i would like to find out more about the trials
# 2                               Hello I had a guest
# 3                Hello my friend overseas right now

# A list of stop word
stopword <- c("I", "Hello", "Hi")
# Create the pattern
stopword2 <- paste0("\\b", stopword, "\\b")
stopword3 <- paste(stopword2, collapse = "|")

# View the pattern
stopword3
# [1] "\\bI\\b|\\bHello\\b|\\bHi\\b"

dat$Chat <- gsub(pattern = stopword3, replacement = " ", x = dat$Chat, ignore.case = TRUE)
dat
#                                               Chat
# 1     would like to find out more about the trials
# 2                                      had a guest
# 3                     my friend overseas right now

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM