gpt4 book ai didi

r - Quanteda:如何删除我自己的单词列表

转载 作者:行者123 更新时间:2023-12-02 01:38:20 25 4
gpt4 key购买 nike

由于 Quanteda 中还没有现成的波兰语停用词实现,我想使用我自己的列表。我将其作为以空格分隔的列表形式保存在文本文件中。如果需要,我还可以准备一个以换行符分隔的列表。

如何从我的语料库中删除自定义的长停用词列表?词干后我该如何做到这一点?

我尝试过创建各种格式,转换为字符串向量,例如

stopwordsPL <- as.character(readtext("polish.stopwords.txt",encoding = "UTF-8"))
stopwordsPL <- read.txt("polish.stopwords.txt",encoding = "UTF-8",stringsAsFactors = F))
stopwordsPL <- dictionary(stopwordsPL)

我也尝试过在语法中使用这样的单词向量

myStemMat <-
dfm(
mycorpus,
remove = as.vector(stopwordsPL),
stem = FALSE,
remove_punct = TRUE,
ngrams=c(1,3)
)

dfm_trim(myStemMat, sparsity = stopwordsPL)

myStemMat <- dfm_remove(myStemMat,features = as.data.frame(stopwordsPL))

没有任何效果。我的停用词出现在语料库和分析中。应用自定义停用词的正确方法/语法应该是什么?

最佳答案

假设您的 polish.stopwords.txt 类似于 this那么你应该能够通过这种方式轻松地将它们从你的语料库中删除:

stopwordsPL <- readLines("polish.stopwords.txt", encoding = "UTF-8")

dfm(mycorpus,
remove = stopwordsPL,
stem = FALSE,
remove_punct = TRUE,
ngrams=c(1,3))

使用readtext的解决方案不起作用,因为它将整个文件作为一个文档读取。要获取单个单词,您需要对其进行标记并将标记强制转换为字符。可能 readLines() 更容易。

也不需要从 stopwordsPL 创建字典,因为 remove 应该采用字符向量。另外,恐怕还没有实现波兰词干分析器。

目前(v0.9.9-65)dfm() 中的功能删除并不能消除形成二元组的停用词。要覆盖此设置,请尝试:

# form the tokens, removing punctuation
mytoks <- tokens(mycorpus, remove_punct = TRUE)
# remove the Polish stopwords, leave pads
mytoks <- tokens_remove(mytoks, stopwordsPL, padding = TRUE)
## can't do this next one since no Polish stemmer in
## SnowballC::getStemLanguages()
# mytoks <- tokens_wordstem(mytoks, language = "polish")
# form the ngrams
mytoks <- tokens_ngrams(mytoks, n = c(1, 3))
# construct the dfm
dfm(mytoks)

关于r - Quanteda:如何删除我自己的单词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327556/

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