DFM之前的搭配和复合

问题描述

我想使用文本列来查找短语,所以我尝试使用并置选项:

library(quanteda)

dataset1 <- data.frame( anumber = c(1,2,3),text = c("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,when an unkNown printer took a galley of type and scrambled it to make a type specimen book.","It has survived not only five centuries,but also the leap into electronic typesetting,remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum","Contrary to popular belief,Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC,making it over 2000 years old. Richard Mcclintock,a Latin professor at Hampden-Sydney College in Virginia,looked up one of the more obscure Latin words,consectetur,from a Lorem Ipsum passage,and going through the cites of the word in classical literature,discovered the undoubtable source."))

    cols <- textstat_collocations(dataset1 $text,size = 2:3,min_count = 30)

在将化合物用于其频率之后,请尝试以下操作:

inputforDfm <- tokens_compound(cols)

tokens_compound.default(cols)中的错误: tokens_compound()仅适用于令牌对象。

但是需要令牌吗?如何制作并插入dfm中:

myDfm <- dataset1 %>%
corpus() %>%
tokens(remove_punct = TRUE,remove_numbers = TRUE,remove_symbols = TRUE) %>%
dfm()

解决方法

您需要标记文本,因为标记化合物需要标记对象作为其第一个参数。

library(quanteda)
## Package version: 2.1.1

在这里,我将其更改为min_count = 2,因为在此示例中,否则您将不返回任何搭配,因为在文本中不会出现30次以上!

cols <- textstat_collocations(dataset1$text,size = 2:3,min_count = 2)

复合后,现在我们可以看到令牌中的复合物:

toks <- tokens(dataset1$text) %>%
  tokens_compound(cols)

print(toks)
## Tokens consisting of 3 documents.
## text1 :
##  [1] "Lorem_Ipsum_is" "simply"         "dummy_text"     "of_the"        
##  [5] "printing"       "and"            "typesetting"    "industry"      
##  [9] "."              "Lorem_Ipsum"    "has"            "been"          
## [ ... and 28 more ]
## 
## text2 :
##  [1] "It_has"    "survived"  "not"       "only"      "five"      "centuries"
##  [7] ","         "but"       "also"      "the"       "leap"      "into"     
## [ ... and 37 more ]
## 
## text3 :
##  [1] "Contrary"       "to"             "popular"        "belief"        
##  [5] ","              "Lorem_Ipsum_is" "not"            "simply"        
##  [9] "random"         "text"           "."              "It_has"        
## [ ... and 63 more ]

现在以通常的方式创建dfm,我们只需选择以下化合物即可查看这些化合物:

dfm(toks) %>%
  dfm_select(pattern = "*_*")
## Document-feature matrix of: 3 documents,5 features (33.3% sparse).
##        features
## docs    lorem_ipsum_is dummy_text of_the lorem_ipsum it_has
##   text1              1          2      1           1      0
##   text2              0          0      0           2      1
##   text3              1          0      2           1      1

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...