【问题标题】:Corpus build with phrases用短语构建语料库
【发布时间】:2014-07-25 03:59:08
【问题描述】:

我的文件如下:

 doc1 = very good, very bad, you are great
 doc2 = very bad, good restaurent, nice place to visit

我想让我的语料库与, 分开,这样我的最终DocumentTermMatrix 就变成了:

      terms
 docs       very good      very bad        you are great   good restaurent   nice place to visit
  doc1       tf-idf          tf-idf         tf-idf          0                    0
  doc2       0                tf-idf         0                tf-idf             tf-idf

我知道,如何计算单个单词的DocumentTermMatrix,但不知道如何在R中制作语料库separated for each phrase。首选R中的解决方案,但也欢迎Python中的解决方案。

我试过的是:

> library(tm)
> library(RWeka)
> BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 3))
> options(mc.cores=1)
> texts <- c("very good, very bad, you are great","very bad, good restaurent, nice place to visit")
> corpus <- Corpus(VectorSource(texts))
> a <- TermDocumentMatrix(corpus, control = list(tokenize = BigramTokenizer))
> as.matrix(a)

我得到:

                         Docs
  Terms                   1 2
  bad good restaurent   0 1
  bad you are           1 0
  good restaurent nice  0 1
  good very bad         1 0
  nice place to         0 1
  place to visit        0 1
  restaurent nice place 0 1
  very bad good         0 1
  very bad you          1 0
  very good very        1 0
  you are great         1 0

我想要的不是单词的组合,而是我在矩阵中显示的短语。

【问题讨论】:

    标签: r matrix tf-idf corpus phrase


    【解决方案1】:

    对于任何使用 text2vec 的人来说,这是基于自定义词汇的非常方便的解决方案:

    library(text2vec)
    doc1 <- 'very good, very bad, you are great'
    doc2 <- 'very bad, good restaurent, nice place to visit'
    docs <- list(doc1, doc2)
    docs <- sapply(docs, strsplit, split=', ')
    vocab <- vocab_vectorizer(create_vocabulary(unique(unlist(docs))))
    dtm <- create_dtm(itoken(docs), vocab)
    dtm
    

    这将导致:

    2 x 5 sparse Matrix of class "dgCMatrix"
      very good very bad you are great good restaurent nice place to visit
    1         1        1             1               .                   .
    2         .        1             .               1                   1
    

    这种方法允许在加载文件和准备词汇时进行更多自定义。

    【讨论】:

      【解决方案2】:

      这是一种方法,使用qdap + tm包装:

      library(qdap); library(tm); library(qdapTools)
      
      dat <- list2df(list(doc1 = "very good, very bad, you are great",
       doc2 = "very bad, good restaurent, nice place to visit"), "text", "docs")
      
      x <- sub_holder(", ", dat$text)
      
      m <- dtm(wfm(x$unhold(gsub(" ", "~~", x$output)), dat$docs) )
      weightTfIdf(m)
      
      inspect(weightTfIdf(m))
      
      ## A document-term matrix (2 documents, 5 terms)
      ## 
      ## Non-/sparse entries: 4/6
      ## Sparsity           : 60%
      ## Maximal term length: 19 
      ## Weighting          : term frequency - inverse document frequency (normalized) (tf-idf)
      ## 
      ##       Terms
      ## Docs   good restaurent nice place to visit very bad very good you are great
      ##   doc1       0.0000000           0.0000000        0 0.3333333     0.3333333
      ##   doc2       0.3333333           0.3333333        0 0.0000000     0.0000000
      

      您也可以一举返回DocumentTermMatrix,但这可能更难理解:

      x <- sub_holder(", ", dat$text)
      
      apply_as_tm(t(wfm(x$unhold(gsub(" ", "~~", x$output)), dat$docs)), 
          weightTfIdf, to.qdap=FALSE)
      

      【讨论】:

      • 这不是我想要的。使用bigrams @ 987654327,您所展示的内容可以轻松完成。我与你的结果相同,DocumentTermMatrix。我不想将single words 用作terms。我只想要tf-idfphrases which are separated by comma。在您的答案中,有些术语如are , bad我不想要。 span>
      • 查看我的编辑。请添加您从现在开始尝试的内容,因为这是一个非常有价值的信息来弄清楚问题。 span>
      • 你可以回答我这个问题stackoverflow.com/questions/24117862/…吗? span>
      【解决方案3】:

      如果您只是使用 strsplit 以逗号分隔,然后通过与某些字符组合将您的短语变成单个“单词”,该怎么办。例如

      library(tm)
      docs <- c(D1 = "very good, very bad, you are great", 
          D2 = "very bad, good restaurent, nice place to visit")
      
      dd <- Corpus(VectorSource(docs))
      dd <- tm_map(dd, function(x) {
          PlainTextDocument(
             gsub("\\s+","~",strsplit(x,",\\s*")[[1]]), 
             id=ID(x)
           )
      })
      inspect(dd)
      
      # A corpus with 2 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 
      
      # $D1
      # very~good
      # very~bad
      # you~are~great
      # 
      # $D2
      # very~bad
      # good~restaurent
      # nice~place~to~visit
      
      dtm <- DocumentTermMatrix(dd, control = list(weighting = weightTfIdf))
      as.matrix(dtm)
      

      这会产生

      # Docs good~restaurent nice~place~to~visit very~bad very~good you~are~great
      #   D1       0.0000000           0.0000000        0 0.3333333     0.3333333
      #   D2       0.3333333           0.3333333        0 0.0000000     0.0000000
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多