我正在尝试获取所有词干列表及其原始形式。
这是一个例子
library(tm)
text <- c("Very Impressed with the shipping time, it arrived a few days earlier than expected", "it was very helpful","It was a wonderful experience")
corpus<-Corpus(VectorSource(text))
corpus<-tm_map(corpus,stemDocument)
我在数据框中寻找这样的答案
orginal_word stemmed
Impressed Impress
shipping ship
very veri
helpful help
wonderful wonder
experience experi
答案 0 :(得分:0)
这可能对您有所帮助。 wordStem()
包中有一个名为SnowballC
的函数。使用它,您可以执行以下操作。由于我在unnest_tokens()
包中使用tidytext
,因此我首先创建了一个数据框。该函数拆分单词并创建长格式数据集。您似乎想删除停用词,因此我使用了filter()
。最后一步对你来说至关重要。我在wordStem()
包中使用SnowballC
来提取数据中剩余字词的词干。结果可能不完全是你想要的。但我希望这会在某种程度上帮助你。
library(dplyr)
library(tidytext)
library(SnowballC)
mydf <- data_frame(id = 1:length(text),
text = text)
data(stop_words)
mydf %>%
unnest_tokens(input = text, output = word) %>%
filter(!word %in% stop_words$word) %>%
mutate(stem = wordStem(word))
# id word stem
# <int> <chr> <chr>
# 1 1 impressed impress
# 2 1 shipping ship
# 3 1 time time
# 4 1 arrived arriv
# 5 1 days dai
# 6 1 earlier earlier
# 7 1 expected expect
# 8 2 helpful help
# 9 3 wonderful wonder
#10 3 experience experi
答案 1 :(得分:0)
这比@ jazzurro的回答更有效:
library("corpus")
text <- c("Very Impressed with the shipping time, it arrived a few days earlier than expected", "it was very helpful","It was a wonderful experience")
word <- text_types(text, collapse = TRUE, drop = stopwords_en, drop_punct = TRUE)
stem <- SnowballC::wordStem(word, "english")
data.frame(word, stem)
结果:
word stem
1 arrived arriv
2 days day
3 earlier earlier
4 expected expect
5 experience experi
6 helpful help
7 impressed impress
8 shipping ship
9 time time
10 wonderful wonder
(text_types
函数也接受tm
语料库对象,如果这对你很重要。)