gpt4 book ai didi

r - 使用 Quanteda 一步一步创建 dfm

转载 作者:行者123 更新时间:2023-12-02 03:34:18 24 4
gpt4 key购买 nike

我想分析一个大的(n=500,000)文档语料库。我使用 quanteda 期望 will be fastertm 中的 tm_map() 更好。我想逐步进行,而不是使用 dfm() 的自动化方式。我这样做是有原因的:在一种情况下,我不想在删除停用词之前进行标记,因为这会导致许多无用的二元组,在另一种情况下,我必须使用特定于语言的程序预处理文本。

我希望实现这个序列:
1)删除标点符号和数字
2)删除停用词(即在标记化之前,以避免无用的标记)
3)使用一元组和二元组进行标记
4)创建dfm

我的尝试:

> library(quanteda)
> packageVersion("quanteda")
[1] ‘0.9.8’
> text <- ie2010Corpus$documents$texts
> text.corpus <- quanteda:::corpus(text, docnames=rownames(ie2010Corpus$documents))

> class(text.corpus)
[1] "corpus" "list"

> stopw <- c("a","the", "all", "some")
> TextNoStop <- removeFeatures(text.corpus, features = stopw)
# Error in UseMethod("selectFeatures") :
# no applicable method for 'selectFeatures' applied to an object of class "c('corpus', 'list')"

# This is how I would theoretically continue:
> token <- tokenize(TextNoStop, removePunct=TRUE, removeNumbers=TRUE)
> token2 <- ngrams(token,c(1,2))

奖励问题如何删除 quanteda 中的稀疏标记? (即相当于 tm 中的 removeSparseTerms()

<小时/>

更新根据@Ken的回答,以下是逐步使用 quanteda 进行操作的代码:

library(quanteda)
packageVersion("quanteda")
[1] ‘0.9.8’

1) 删除自定义标点符号和数字。例如。注意ie2010语料库中的“\n”

text.corpus <- ie2010Corpus
texts(text.corpus)[1] # Use texts() to extrapolate text
# 2010_BUDGET_01_Brian_Lenihan_FF
# "When I presented the supplementary budget to this House last April, I said we
# could work our way through this period of severe economic distress. Today, I
# can report that notwithstanding the difficulties of the past eight months, we
# are now on the road to economic recovery.\nIt is

texts(text.corpus)[1] <- gsub("\\s"," ",text.corpus[1]) # remove all spaces (incl \n, \t, \r...)
texts(text.corpus)[1]
2010_BUDGET_01_Brian_Lenihan_FF
# "When I presented the supplementary budget to this House last April, I said we
# could work our way through this period of severe economic distress. Today, I
# can report that notwithstanding the difficulties of the past eight months, we
# are now on the road to economic recovery. It is of e

关于人们可能更喜欢预处理的原因的进一步说明。我目前的语料库是意大利语,这种语言的冠词与带有撇号的单词相关。因此,直接的 dfm() 可能会导致不精确的标记化。例如:

broken.tokens <- dfm(corpus(c("L'abile presidente Renzi. Un'abile mossa di Berlusconi"), removePunct=TRUE))

将为同一个单词生成两个单独的标记(“un'abile”和“l'abile”),因此需要在此处使用 gsub() 执行额外步骤。

2) 在 quanteda 中,不可能在标记化之前直接删除文本中的停用词。在我之前的示例中,必须删除“l”和“un”,以免产生误导性的二元组。这可以在 tm 中使用 tm_map(..., removeWords) 进行处理。

3) 代币化

token <- tokenize(text.corpus[1], removePunct=TRUE, removeNumbers=TRUE, ngrams = 1:2)

4)创建 dfm:

dfm <- dfm(token)

5)删除稀疏特征

dfm <- trim(dfm, minCount = 5)

最佳答案

我们设计了dfm()它不是一个“黑匣子”,而更像是一把瑞士军刀,它结合了典型用户在将文本转换为文档和功能矩阵时想要应用的许多选项。然而,如果您希望进行更精细的控制,所有这些选项也可以通过较低级别的处理命令来使用。

然而,quanteda 的设计原则之一是文本只能通过标记化过程成为“特征”。如果您希望排除一组标记化功能,则必须首先对文本进行标记化,否则无法排除它们。与 R 的其他文本包(例如 tm)不同,这些步骤是从语料库“下游”应用的,因此语料库仍然是一组未处理的文本,将对其应用操作(但本身不会是一组经过转换的文本)。这样做的目的是为了保持通用性,同时也是为了提高文本分析的可重复性和透明度。

回答您的问题:

  1. 但是,您可以使用texts(myCorpus) <-来覆盖我们鼓励的行为。函数,其中分配给文本的内容将覆盖现有文本。因此,您可以使用正则表达式来删除标点符号和数字 - 例如 stringi 命令并使用标点符号和数字的 Unicode 类来识别模式。

  2. 我建议您在删除停用词之前进行分词。停止“单词”是标记,因此在对文本进行标记之前无法从文本中删除它们。甚至应用正则表达式来替换""涉及在正则表达式中指定某种形式的单词边界 - 同样,这就是标记化。

  3. 标记为一元组和二元组:

    token (myCorpus, ngrams = 1:2)

  4. 要创建 dfm,只需调用 dfm(myTokens) 。 (您也可以在此阶段对 ngram 应用步骤 3。

奖励 1:n=2 搭配产生与二元组相同的列表,只是格式不同。你还有别的打算吗? (也许是单独的问题?)

奖励 2:参见 dfm_trim(x, sparsity = )removeSparseTerms()对于大多数人来说,选择是相当令人困惑的,但这包括来自tm的移民。请参阅this post以获得完整的解释。

顺便说一句:使用texts()而不是ie2010Corpus$documents$texts -- 我们很快将重写语料库的对象结构,因此当存在提取器函数时,您不应该以这种方式访问​​其内部。 (此外,这一步是不必要的 - 这里您只是重新创建了语料库。)

2018-01 更新:

语料库对象的新名称是 data_corpus_irishbudget2010 ,搭配评分函数为textstat_collocations() .

关于r - 使用 Quanteda 一步一步创建 dfm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com