【问题标题】:Using dictionary to create Bigram in Quanteda在 Quanteda 中使用字典创建 Bigram
【发布时间】:2015-12-26 19:41:35
【问题描述】:

我正在尝试从我的数据文本分析中删除拼写错误。所以我正在使用 quanteda 包的字典功能。它适用于 Unigram。但它为 Bigrams 提供了意想不到的输出。不知道如何处理拼写错误,以免它们潜入我的 Bigrams 和 Trigrams。

ZTestCorp1 <- c("The new law included a capital gains tax, and an inheritance tax.", 
                "New York City has raised a taxes: an income tax and a sales tax.")

ZcObj <- corpus(ZTestCorp1)

mydict <- dictionary(list("the"="the", "new"="new", "law"="law", 
                      "capital"="capital", "gains"="gains", "tax"="tax", 
                      "inheritance"="inheritance", "city"="city")) 

Zdfm1 <- dfm(ZcObj, ngrams=2, concatenator=" ", 
         what = "fastestword", 
         toLower=TRUE, removeNumbers=TRUE,
         removePunct=TRUE, removeSeparators=TRUE,
         removeTwitter=TRUE, stem=FALSE,
         ignoredFeatures=NULL,
         language="english", 
         dictionary=mydict, valuetype="fixed")

wordsFreq1 <- colSums(sort(Zdfm1))

电流输出

> wordsFreq1
    the         new         law     capital       gains         tax inheritance        city 
      0           0           0           0           0           0           0           0 

不使用字典,输出如下:

> wordsFreq
    tax and         the new         new law    law included      included a       a capital 
          2               1               1               1               1               1 
capital gains       gains tax          and an  an inheritance inheritance tax        new york 
          1               1               1               1               1               1 
  york city        city has      has raised        raised a         a taxes        taxes an 
          1               1               1               1               1               1 
  an income      income tax           and a         a sales       sales tax 
          1               1               1               1               1

预期的二元组

The new
new law
law capital
capital gains
gains tax
tax inheritance
inheritance city  

附言我假设在字典匹配后完成标记化。但根据我看到的结果,情况似乎并非如此。

另一方面,我尝试将我的字典对象创建为

mydict <- dictionary(list(mydict=c("the", "new", "law", "capital", "gains", 
                      "tax", "inheritance", "city"))) 

但它没有工作。所以我不得不使用上面我认为效率不高的方法。

更新 根据 Ken 的解决方案添加了输出:

> (myDfm1a <- dfm(ZcObj, verbose = FALSE, ngrams=2, 
+                keptFeatures = c("the", "new", "law", "capital", "gains",  "tax", "inheritance", "city")))
Document-feature matrix of: 2 documents, 14 features.
2 x 14 sparse Matrix of class "dfmSparse" features
docs    the_new new_law law_included a_capital capital_gains gains_tax   tax_and an_inheritance
text1       1       1            1         1             1         1       1               1
text2       0       0            0         0             0         0       1              0
   features
docs    inheritance_tax new_york york_city city_has income_tax sales_tax
text1               1        0         0        0          0         0
text2               0        1         1        1          1         1

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    2017 年 12 月 21 日更新了 quanteda 的较新版本

    很高兴看到您正在使用该软件包!我认为你正在努力解决两个问题。首先是如何在形成 ngram 之前应用特征选择。二是一般如何定义特征选择(使用quanteda)。

    第一期:如何在形成 ngram 之前应用特征选择。在这里,您已经定义了一个字典来执行此操作。 (正如我将在下面展示的,这不是必需的。)您想删除所有不在选择列表中的术语,然后形成二元组。 quanteda 默认情况下不这样做,因为它不是“bigram”的标准形式,其中单词不是根据邻接严格定义的某个窗口来搭配的。例如,在您的预期结果中,law capital 不是一对相邻的术语,这是二元组的通常定义。

    但是,我们可以通过更“手动”构建文档特征矩阵来覆盖此行为。

    首先,标记文本。

    # tokenize the original
    toks <- tokens(ZcObj, removePunct = TRUE, removeNumbers = TRUE) %>%
      tokens_tolower()
    toks
    ## tokens object from 2 documents.
    ## text1 :
    ##  [1] "the"         "new"         "law"         "included"    "a"           "capital"     "gains"       "tax"         "and"         "an"          "inheritance" "tax"        
    ## 
    ## text2 :
    ##  [1] "new"    "york"   "city"   "has"    "raised" "a"      "taxes"  "an"     "income" "tax"    "and"    "a"      "sales"  "tax"  
    

    现在我们将您的字典 mydict 应用到使用 tokens_select() 的标记化文本:

    (toksDict <- tokens_select(toks, mydict, selection = "keep"))
    ## tokens object from 2 documents.
    ## text1 :
    ##  [1] "the"         "new"         "law"         "capital"     "gains"       "tax"         "inheritance" "tax"        
    ## 
    ## text2 :
    ##  [1] "new"  "city" "tax"  "tax" 
    

    从这组选定的标记中,我们现在可以形成二元组(或者我们可以将toksDict 直接提供给dfm()):

    (toks2 <- tokens_ngrams(toksDict, n = 2, concatenator = " "))
    ## tokens object from 2 documents.
    ## text1 :
    ##  [1] "the new"         "new law"         "law capital"     "capital gains"   "gains tax"       "tax inheritance" "inheritance tax"
    ## 
    ## text2 :
    ##  [1] "new city" "city tax" "tax tax" 
    
    # now create the dfm
    (myDfm2 <- dfm(toks2))
    ## Document-feature matrix of: 2 documents, 10 features.
    ## 2 x 10 sparse Matrix of class "dfm"
    ##        features
    ## docs    the new new law law capital capital gains gains tax tax inheritance inheritance tax new city city tax tax tax
    ##   text1       1       1           1             1         1               1               1        0        0       0
    ##   text2       0       0           0             0         0               0               0        1        1       1
    topfeatures(myDfm2)
    #     the new         new law     law capital   capital gains       gains tax tax inheritance inheritance tax        new city        city tax         tax tax 
    #           1               1               1               1               1               1               1               1               1               1 
    

    功能列表现在非常接近您想要的。

    第二个问题是您的字典方法似乎效率低下的原因。这是因为您正在创建一个字典来执行特征选择,但并未真正将其用作字典 - 换句话说,每个键等于其自己的键作为值的字典并不是真正的字典。只需给它一个选择标记的字符向量,它就可以正常工作,例如:

    (myDfm1 <- dfm(ZcObj, verbose = FALSE, 
                   keptFeatures = c("the", "new", "law", "capital", "gains", "tax", "inheritance", "city")))
    ## Document-feature matrix of: 2 documents, 8 features.
    ## 2 x 8 sparse Matrix of class "dfm"
    ##        features
    ## docs    the new law capital gains tax inheritance city
    ##   text1   1   1   1       1     1   2           1    0
    ##   text2   0   1   0       0     0   2           0    1
    

    【讨论】:

    • 感谢您慷慨而详细的解释。我收到此错误。有任何想法吗?? ` > (toksDict
    • 可能是因为 selectFeatures() 的方法仅在最新 (GitHub) 版本的 quanteda 中进行了扩展,并且您使用的是 CRAN 版本。尝试按照github.com/kbenoit/quanteda 从 GitHub 安装,截至今天的版本是 0.9.1-7。 (将在 2016 年 1 月更新 CRAN 版本。)
    • 谢谢@Ken。这很棒!我将安装最新的 cran 包。事实上,我喜欢您提供的第二种解决方案,因为它考虑了停用词。这对我来说很重要,因为我正在做一个单词预测项目。但是,我很好奇它是如何吸引到 york 的。我以为约克不是停用词。当我使用 ngrams=2 选项时,我得到了这个。
    • 谢谢!不知道你说的“约克”是什么意思。它不是 dfm 中的标记,因为您选择仅保留字典值中的单词。很高兴继续通过私信或 GitHub 问题提供帮助。
    • 我设法使用斯塔福德字典创建了我的 Unigrams。但是,当我尝试创建 Bigram 时,它一直在等待。我在 20 小时前启动了它,它仍在运行。索引在不到 10 分钟内完成。它显示了 674、468 个文档和 5,168,973 个特征类型。我正在使用 Windows 10 12 GB 内存的戴尔 inspiron 15 笔记本电脑上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多