使用tm包进行文本挖掘-词根提取

11

我在使用R中的tm包做一些文本挖掘工作。一切都很顺利。但是,在提取词干后,出现了一个问题(http://en.wikipedia.org/wiki/Stemming)。显然,有一些单词具有相同的词干,但重要的是它们不应该被“合并”(因为这些单词有不同的含义)。

例如,看下面的4个文本。在这里,您不能将“lecturer”或“lecture”(“association”和“associate”)互换使用。然而,在第4步中却这样做了。

是否有任何优雅的解决方案可以手动实现某些情况/单词(例如保持“讲师”和“演讲”为两个不同的词)?

texts <- c("i am member of the XYZ association",
"apply for our open associate position", 
"xyz memorial lecture takes place on wednesday", 
"vote for the most popular lecturer")

# Step 1: Create corpus
corpus <- Corpus(DataframeSource(data.frame(texts)))

# Step 2: Keep a copy of corpus to use later as a dictionary for stem completion
corpus.copy <- corpus

# Step 3: Stem words in the corpus
corpus.temp <- tm_map(corpus, stemDocument, language = "english")  

inspect(corpus.temp)

# Step 4: Complete the stems to their original form
corpus.final <- tm_map(corpus.temp, stemCompletion, dictionary = corpus.copy)  

inspect(corpus.final)

3
这就是词干提取的目的。你这样做是为了找到词根。如果想保留差异,那就不要进行词干提取。 - Tyler Rinker
1
我知道。但是难道没有一种优雅的方法可以在某些情况下将其改回来吗? - majom
2个回答

13

我不完全确定您想要什么,也不太明白tm_map的工作方式。如果我理解正确,以下方法可以实现。根据我的理解,您想提供一个单词列表,这些单词不应被词干处理。我主要使用qdap包,因为我很懒并且喜欢它的mgsub函数。

请注意,我对使用mgsubtm_map感到沮丧,因为它们经常报错,所以我改用了lapply

texts <- c("i am member of the XYZ association",
    "apply for our open associate position", 
    "xyz memorial lecture takes place on wednesday", 
    "vote for the most popular lecturer")

library(tm)
# Step 1: Create corpus
corpus.copy <- corpus <- Corpus(DataframeSource(data.frame(texts)))

library(qdap)
# Step 2: list to retain and indentifier keys
retain <- c("lecturer", "lecture")
replace <- paste(seq_len(length(retain)), "SPECIAL_WORD", sep="_")

# Step 3: sub the words you want to retain with identifier keys
corpus[seq_len(length(corpus))] <- lapply(corpus, mgsub, pattern=retain, replacement=replace)

# Step 4: Stem it
corpus.temp <- tm_map(corpus, stemDocument, language = "english")  

# Step 5: reverse -> sub the identifier keys with the words you want to retain
corpus.temp[seq_len(length(corpus.temp))] <- lapply(corpus.temp, mgsub, pattern=replace, replacement=retain)

inspect(corpus)       #inspect the pieces for the folks playing along at home
inspect(corpus.copy)
inspect(corpus.temp)

# Step 6: complete the stem
corpus.final <- tm_map(corpus.temp, stemCompletion, dictionary = corpus.copy)  
inspect(corpus.final)

基本上,它的工作原理是:

  1. 用提供的“NO STEM”单词替换唯一标识符键(使用mgsub
  2. 然后使用stemDocument对其进行词干处理
  3. 接下来将其反转并使用“NO STEM”单词替换标识符键(使用mgsub
  4. 最后完成词干处理(stemCompletion

以下是输出结果:

## >     inspect(corpus.final)
## A corpus with 4 text documents
## 
## The metadata consists of 2 tag-value pairs and a data frame
## Available tags are:
##   create_date creator 
## Available variables in the data frame are:
##   MetaID 
## 
## $`1`
## i am member of the XYZ associate
## 
## $`2`
##  for our open associate position
## 
## $`3`
## xyz memorial lecture takes place on wednesday
## 
## $`4`
## vote for the most popular lecturer

0

您还可以使用以下软件包来进行词干提取:https://cran.r-project.org/web/packages/SnowballC/SnowballC.pdf

您只需要使用函数wordStem,将要进行词干提取的单词向量和您所处理的语言作为参数传递即可。要了解确切的语言字符串,您可以参考方法getStemLanguages,它将返回所有可能的选项。

此致敬礼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接