我是从R中的tm包开始的,所以请多多包涵。我创建了相当大的社会主义/共产主义宣传语料库,并想提取新创造的政治用语(多个词,例如“斗争-批评-转变运动”)。

这是一个分为两个步骤的问题,一个与到目前为止的代码有关,另一个与我应该如何进行有关。

步骤1:为此,我想首先确定一些常见的ngram。但是我很早就被困住了。这是我一直在做的事情:

library(tm)
library(RWeka)

a  <-Corpus(DirSource("/mycorpora/1965"), readerControl = list(language="lat")) # that dir is full of txt files
summary(a)
a <- tm_map(a, removeNumbers)
a <- tm_map(a, removePunctuation)
a <- tm_map(a , stripWhitespace)
a <- tm_map(a, tolower)
a <- tm_map(a, removeWords, stopwords("english"))
a <- tm_map(a, stemDocument, language = "english")
# everything works fine so far, so I start playing around with what I have
adtm <-DocumentTermMatrix(a)
adtm <- removeSparseTerms(adtm, 0.75)

inspect(adtm)

findFreqTerms(adtm, lowfreq=10) # find terms with a frequency higher than 10

findAssocs(adtm, "usa",.5) # just looking for some associations
findAssocs(adtm, "china",.5)

# ... and so on, and so forth, all of this works fine

我加载到R中的语料库对我抛出的大多数功能都可以正常工作。从我的语料库创建TDM,查找频繁的单词,联想,创建单词云等方面,我都没有任何问题。但是,当我尝试使用tm FAQ中概述的方法使用identify ngrams时,我显然在使用tdm-constructor时犯了一些错误:
# Trigram

TrigramTokenizer <- function(x) NGramTokenizer(x,
                                Weka_control(min = 3, max = 3))

tdm <- TermDocumentMatrix(a, control = list(tokenize = TrigramTokenizer))

inspect(tdm)

我收到此错误消息:
Error in rep(seq_along(x), sapply(tflist, length)) :
invalid 'times' argument
In addition: Warning message:
In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

有任何想法吗? “a”不是正确的类/对象吗?我很困惑。我认为这里存在一个基本错误,但我没有看到它。 :(

步骤2:然后,当我将语料库与其他语料库进行比较时,我想识别出表示过大的ngram。例如,我可以将我的语料库与大型标准英语语料库进行比较。或者,我创建可以相互比较的子集(例如,苏联与中国共产党的术语)。您对我应该如何做有什么建议?我应该研究任何脚本/功能吗?只是一些想法或指示会很棒。

谢谢你的耐心!

最佳答案

我无法重现您的问题,您使用的是R,tm,RWeka等最新版本吗?

require(tm)
a <- Corpus(DirSource("C:\\Downloads\\Only1965\\Only1965"))
summary(a)
a <- tm_map(a, removeNumbers)
a <- tm_map(a, removePunctuation)
a <- tm_map(a , stripWhitespace)
a <- tm_map(a, tolower)
a <- tm_map(a, removeWords, stopwords("english"))
# a <- tm_map(a, stemDocument, language = "english")
# I also got it to work with stemming, but it takes so long...
adtm <-DocumentTermMatrix(a)
adtm <- removeSparseTerms(adtm, 0.75)

inspect(adtm)

findFreqTerms(adtm, lowfreq=10) # find terms with a frequency higher than 10
findAssocs(adtm, "usa",.5) # just looking for some associations
findAssocs(adtm, "china",.5)

# Trigrams
require(RWeka)
TrigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3))
tdm <- TermDocumentMatrix(a, control = list(tokenize = TrigramTokenizer))
tdm <- removeSparseTerms(tdm, 0.75)
inspect(tdm[1:5,1:5])

这就是我得到的
A term-document matrix (5 terms, 5 documents)

Non-/sparse entries: 11/14
Sparsity           : 56%
Maximal term length: 28
Weighting          : term frequency (tf)

                                   Docs
Terms                               PR1965-01.txt PR1965-02.txt PR1965-03.txt
  †chinese press                              0             0             0
  †renmin ribao                               0             1             1
  — renmin ribao                              2             5             2
  “ chinese people                            0             0             0
  “renmin ribaoâ€\u009d editorial             0             1             0
  etc.

关于第二步,以下是一些有用的开始的指针:

http://quantifyingmemory.blogspot.com/2013/02/mapping-significant-textual-differences.html
http://tedunderwood.com/2012/08/14/where-to-start-with-text-mining/,这是他的代码https://dl.dropboxusercontent.com/u/4713959/Neuchatel/NassrProgram.R

关于r - 在R中查找ngram并比较整个语料库中的ngram,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19615181/

10-12 19:21