簡體   English   中英

從 R 中的文本中刪除單詞和符號

[英]Removing words and symbols from text in R

考慮以下示例。 是否可以從text刪除stopwords

library(tm)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- ("≤µm", "≥°f", "±μgm")

你可以像下面這樣嘗試gsub

gsub(paste0(stopwords, collapse = "|"),"",text)

首先,您的示例字符串中有一些錯誤。 text缺少引號並且stopwords缺少括號前的c

text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")

您可以使用 stringr 從字符串中刪除停用詞中的值,如下所示:

library(stringr)
str_replace_all(text, paste(stopwords, collapse = "|"), "")

由於您要進行文本挖掘,您可能希望將輸入字符串轉換為單詞向量。 如果是這樣,您可以通過子集輕松刪除停用詞。

library(stringr)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
text <- unlist(str_split(text, " "))
text[!(sapply(text, function (x) any(str_detect(stopwords, x))))]

如果你的工作讓你把你的話放在 data.frame 或類似的文件中,那么還有另一種方法:

library(dplyr)
library(stringr)
text <- c("this is exercise for text mining ≤µm ≥°f ±μgm")
stopwords <- c("≤µm", "≥°f", "±μgm")
text <- unlist(str_split(text, " "))
data.frame(words = text) %>% anti_join(data.frame(words = stopwords))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM