r - tm 包中的 StemDocment 不适用于过去时词

标签 r nlp tm stemming snowball

我有一个文件“check_text.txt”,其中包含“said say say make made”。我想对其执行词干以获得“说说说做做”。我尝试在tm包中使用stemDocument,如下所示,但只得到“said say say make made”。有没有办法对过去时态单词进行词干提取?在现实世界的自然语言处理中是否有必要这样做?谢谢!

filename = 'check_text.txt'
con <- file(filename, "rb")
text_data <- readLines(con,skipNul = TRUE)
close(con)
text_VS <- VectorSource(text_data)
text_corpus <- VCorpus(text_VS)
text_corpus <- tm_map(text_corpus, stemDocument, language = "english")
as.data.frame(text_corpus)$text

编辑:我还在SnowballC包中尝试了wordStem

> library(SnowballC)
> wordStem(c("said", "say", "says", "make", "made"))
[1] "said" "sai"  "sai"  "make" "made"

最佳答案

如果包中有一个不规则英语动词的数据集,这个任务就很容易了。我只是不知道有任何包含此类数据的包,所以我选择通过抓取来创建自己的数据库。我不确定这个网站是否涵盖了所有不规则单词。如果有必要,您想搜索更好的网站来创建自己的数据库。拥有数据库后,您就可以开始执行任务了。

首先,我使用了 stemDocument() 并使用 -s 清理了当前表单。然后,我收集了words中的过去形式(即过去)、过去形式的不定式形式(即inf1),识别了顺序temp 中过去的形式。我进一步确定了 temp 中过去表格的位置。我最终用不定式形式替换了 sat 形式。我对过去分词重复了相同的过程。

library(tm)
library(rvest)
library(dplyr)
library(splitstackshape)


### Create a database
x <- read_html("http://www.englishpage.com/irregularverbs/irregularverbs.html")

x %>%
html_table(header = TRUE) %>%
bind_rows %>%
rename(Past = `Simple Past`, PP = `Past Participle`) %>%
filter(!Infinitive %in% LETTERS) %>%
cSplit(splitCols = c("Past", "PP"),
       sep = " / ", direction = "long") %>%
filter(complete.cases(.)) %>%
mutate_each(funs(gsub(pattern = "\\s\\(.*\\)$|\\s\\[\\?\\]",
                      replacement = "",
                      x = .))) -> mydic

### Work on the task

words <- c("said", "drawn", "say", "says", "make", "made", "done")

### says to say
temp <- stemDocument(words)

### past forms become present form
### Collect past forms
past <- mydic$Past[which(mydic$Past %in% temp)]

### Collect infinitive forms of past forms
inf1 <- mydic$Infinitive[which(mydic$Past %in% temp)]

### Identify the order of past forms in temp
ind <- match(temp, past)
ind <- ind[is.na(ind) == FALSE]

### Where are the past forms in temp?
position <- which(temp %in% past)

temp[position] <- inf1[ind]

### Check
temp
#[1] "say"   "drawn" "say"   "say"   "make"  "make"  "done" 


### PP forms to infinitive forms (same as past forms)

pp <- mydic$PP[which(mydic$PP %in% temp)]
inf2 <- mydic$Infinitive[which(mydic$PP %in% temp)]
ind <- match(temp, pp)
ind <- ind[is.na(ind) == FALSE]
position <- which(temp %in% pp)
temp[position] <- inf2[ind]

### Check
temp
#[1] "say"  "draw" "say"  "say"  "make" "make" "do" 

关于r - tm 包中的 StemDocment 不适用于过去时词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230641/

相关文章:

r - 如何使用 ROCR 提取平均 ROC 曲线预测?

css - Slidify:如何在 Title Slide 中将 YAML 文本定位到右侧?

r - 按两组比较两个数值变量

python - 我怎样才能解决单词级的困惑?

c++ - 将数组发送到 R 函数 (C++)

tensorflow - 属性错误: 'TFSequenceClassifierOutput' object has no attribute 'argmax'

java - 使用词边界和 POS 将句子拆分为固定大小的 block

R 和 tm 包 : create a term-document matrix with a dictionary of one or two words?

r - 如何在语料库中手动设置文档 ID?

r - 如何在 R 中附加到文档术语矩阵?