【问题标题】:tm Bigrams workaround still producing unigramstm Bigrams 解决方法仍然产生 unigrams
【发布时间】:2019-01-18 06:54:49
【问题描述】:

我正在尝试使用 tm 的 DocumentTermMatrix 函数来生成一个带有二元组而不是一元组的矩阵。我尝试在我的函数中使用herehere 列出的示例(这里是三个示例):

make_dtm = function(main_df, stem=F){
  tokenize_ngrams = function(x, n=2) return(rownames(as.data.frame(unclass(textcnt(x,method="string",n=n)))))
  decisions = Corpus(VectorSource(main_df$CaseTranscriptText))
  decisions.dtm = DocumentTermMatrix(decisions, control = list(tokenize=tokenize_ngrams,
                                                           stopwords=T,
                                                           tolower=T,
                                                           removeNumbers=T,
                                                           removePunctuation=T,
                                                           stemming = stem))
  return(decisions.dtm)
}

make_dtm = function(main_df, stem=F){
  BigramTokenizer = function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
  decisions = Corpus(VectorSource(main_df$CaseTranscriptText))
  decisions.dtm = DocumentTermMatrix(decisions, control = list(tokenize=BigramTokenizer,
                                                           stopwords=T,
                                                           tolower=T,
                                                           removeNumbers=T,
                                                           removePunctuation=T,
                                                           stemming = stem))
  return(decisions.dtm)
}

make_dtm = function(main_df, stem=F){
  BigramTokenizer = function(x) unlist(lapply(ngrams(words(x), 2), paste, collapse = " "), use.names = FALSE)
  decisions = Corpus(VectorSource(main_df$CaseTranscriptText))
  decisions.dtm = DocumentTermMatrix(decisions, control = list(tokenize=BigramTokenizer,
                                                           stopwords=T,
                                                           tolower=T,
                                                           removeNumbers=T,
                                                           removePunctuation=T,
                                                           stemming = stem))
  return(decisions.dtm)
}

然而,不幸的是,这三个版本的函数中的每一个都产生完全相同的输出:带有一元组而不是二元组的 DTM(为简单起见包括图像):

为方便起见,以下是我正在处理的数据的子集:

x = data.frame("CaseName" = c("Attorney General's Reference (No.23 of 2011)", "Attorney General's Reference (No.31 of 2016)", "Joseph Hill & Co Solicitors, Re"),
               "CaseID"= c("[2011]EWCACrim1496", "[2016]EWCACrim1386", "[2013]EWCACrim775"),
               "CaseTranscriptText" = c("sanchez 2011 02187 6 appeal criminal division 8 2011 2011 ewca crim 14962011 wl 844075 wales wednesday 8 2011 attorney general reference 23 2011 36 criminal act 1988 representation qc general qc appeared behalf attorney general", 
                                        "attorney general reference 31 2016 201601021 2 appeal criminal division 20 2016 2016 ewca crim 13862016 wl 05335394 dbe honour qc sitting cacd wednesday 20 th 2016 reference attorney general 36 criminal act 1988 representation",
                                        "matter wasted costs against company solicitors 201205544 5 appeal criminal division 21 2013 2013 ewca crim 7752013 wl 2110641 date 21 05 2013 appeal honour pawlak 20111354 hearing date 13 th 2013 representation toole respondent qc appellants"))

【问题讨论】:

    标签: r tm n-gram


    【解决方案1】:

    您的代码存在一些问题。我只关注您创建的最后一个函数,因为我不使用 tau 或 Rweka 包。

    1 使用分词器需要指定tokenizer = ...,而不是tokenize = ...

    2 而不是Corpus 你需要VCorpus

    3 在你的函数make_dtm 中调整后,我对结果不满意。并非控制选项中指定的所有内容都得到正确处理。我创建了第二个函数make_dtm_adjusted,所以你可以看到两者之间的区别。

    # OP's function adjusted to make it work
    make_dtm = function(main_df, stem=F){
      BigramTokenizer = function(x) unlist(lapply(ngrams(words(x), 2), paste, collapse = " "), use.names = FALSE)
      decisions = VCorpus(VectorSource(main_df$CaseTranscriptText))
      decisions.dtm = DocumentTermMatrix(decisions, control = list(tokenizer=BigramTokenizer,
                                                               stopwords=T,
                                                               tolower=T,
                                                               removeNumbers=T,
                                                               removePunctuation=T,
                                                               stemming = stem))
      return(decisions.dtm)
    }
    
    # improved function
    make_dtm_adjusted = function(main_df, stem=F){
      BigramTokenizer = function(x) unlist(lapply(ngrams(words(x), 2), paste, collapse = " "), use.names = FALSE)
      decisions = VCorpus(VectorSource(main_df$CaseTranscriptText))
    
      decisions <- tm_map(decisions, content_transformer(tolower))
      decisions <- tm_map(decisions, removeNumbers)
      decisions <- tm_map(decisions, removePunctuation)
      # specifying your own stopword list is better as you can use stopwords("smart")
      # or your own list
      decisions <- tm_map(decisions, removeWords, stopwords("english")) 
      decisions <- tm_map(decisions, stripWhitespace)
    
      decisions.dtm = DocumentTermMatrix(decisions, control = list(stemming = stem,
                                                                   tokenizer=BigramTokenizer))
      return(decisions.dtm)
    }
    

    【讨论】:

    • 能否详细说明 VCorpus 和 Corpus 之间的区别?我使用 Corpus 已经有一段时间了,没有遇到任何问题
    • 抱歉回复晚了,但this question 的底部答案回答得很好。
    猜你喜欢
    • 2012-08-28
    • 2011-04-04
    • 2022-11-22
    • 2023-03-12
    • 1970-01-01
    • 2017-03-31
    • 2012-09-01
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多