在 R 中使用 N-Grams 创建文档术语矩阵
Create Document Term Matrix with N-Grams in R
我正在使用 "tm" 包在 R 中创建 DocumentTermMatrix。它适用于一个 - gram 但我正在尝试使用 tm 包创建 N-Grams 的 DocumenttermMatrix(现在 N = 3)和tokenize_ngrams 来自 "tokenizers" 包的函数。
但是我无法创建它。
我搜索了可能的解决方案,但没有得到太多帮助。
出于隐私原因,我无法共享数据。
这是我尝试过的,
library(tm)
library(tokenizers)
data 是一个大约有 4.5k 行和 2 列的数据框,即 "doc_id" 和 "text"
data_corpus = Corpus(DataframeSource(data))
n-gram 标记化的自定义函数:
ngram_tokenizer = function(x){
temp = tokenize_ngrams(x, n_min = 1, n = 3, stopwords = FALSE, ngram_delim = "_")
return(temp)
}
DTM 创建控制列表:
1 克
control_list_unigram = list(tokenize = "words",
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
用于 N-gram 标记化
control_list_ngram = list(tokenize = ngram_tokenizer,
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
dtm_unigram = DocumentTermMatrix(data_corpus, control_list_unigram)
dtm_ngram = DocumentTermMatrix(data_cropus, control_list_ngram)
dim(dtm_unigram)
dim(dtm_ngram)
两个 dtm 的维度相同。
请指正!
不幸的是,tm 有一些令人讨厌的怪癖,而且并不总是很清楚。首先,标记化似乎不适用于创建 Corpus
的语料库。为此,您需要使用 VCorpus
。
因此将 data_corpus = Corpus(DataframeSource(data))
行更改为 data_corpus = VCorpus(DataframeSource(data))
。
这是一个已解决的问题。现在语料库将用于标记化,但现在您 运行 会遇到 tokenize_ngrams
的问题。您将收到以下错误:
Input must be a character vector of any length or a list of character
vectors, each of which has a length of 1.
当你运行这一行时:dtm_ngram = DocumentTermMatrix(data_cropus, control_list_ngram)
要解决这个问题,并且不依赖于 tokenizer 包,您可以使用以下函数来标记数据。
NLP_tokenizer <- function(x) {
unlist(lapply(ngrams(words(x), 1:3), paste, collapse = "_"), use.names = FALSE)
}
这使用了 NLP 包中的 ngrams
函数,该函数在您加载 tm 包时加载。 1:3 告诉它从 1 到 3 个单词创建 ngram。所以你的 control_list_ngram 应该是这样的:
control_list_ngram = list(tokenize = NLP_tokenizer,
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
我个人会使用 quanteda 包来完成所有这些工作。但现在这应该对你有帮助。
我正在使用 "tm" 包在 R 中创建 DocumentTermMatrix。它适用于一个 - gram 但我正在尝试使用 tm 包创建 N-Grams 的 DocumenttermMatrix(现在 N = 3)和tokenize_ngrams 来自 "tokenizers" 包的函数。 但是我无法创建它。
我搜索了可能的解决方案,但没有得到太多帮助。 出于隐私原因,我无法共享数据。 这是我尝试过的,
library(tm)
library(tokenizers)
data 是一个大约有 4.5k 行和 2 列的数据框,即 "doc_id" 和 "text"
data_corpus = Corpus(DataframeSource(data))
n-gram 标记化的自定义函数:
ngram_tokenizer = function(x){
temp = tokenize_ngrams(x, n_min = 1, n = 3, stopwords = FALSE, ngram_delim = "_")
return(temp)
}
DTM 创建控制列表:
1 克
control_list_unigram = list(tokenize = "words",
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
用于 N-gram 标记化
control_list_ngram = list(tokenize = ngram_tokenizer,
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
dtm_unigram = DocumentTermMatrix(data_corpus, control_list_unigram)
dtm_ngram = DocumentTermMatrix(data_cropus, control_list_ngram)
dim(dtm_unigram)
dim(dtm_ngram)
两个 dtm 的维度相同。
请指正!
不幸的是,tm 有一些令人讨厌的怪癖,而且并不总是很清楚。首先,标记化似乎不适用于创建 Corpus
的语料库。为此,您需要使用 VCorpus
。
因此将 data_corpus = Corpus(DataframeSource(data))
行更改为 data_corpus = VCorpus(DataframeSource(data))
。
这是一个已解决的问题。现在语料库将用于标记化,但现在您 运行 会遇到 tokenize_ngrams
的问题。您将收到以下错误:
Input must be a character vector of any length or a list of character
vectors, each of which has a length of 1.
当你运行这一行时:dtm_ngram = DocumentTermMatrix(data_cropus, control_list_ngram)
要解决这个问题,并且不依赖于 tokenizer 包,您可以使用以下函数来标记数据。
NLP_tokenizer <- function(x) {
unlist(lapply(ngrams(words(x), 1:3), paste, collapse = "_"), use.names = FALSE)
}
这使用了 NLP 包中的 ngrams
函数,该函数在您加载 tm 包时加载。 1:3 告诉它从 1 到 3 个单词创建 ngram。所以你的 control_list_ngram 应该是这样的:
control_list_ngram = list(tokenize = NLP_tokenizer,
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
我个人会使用 quanteda 包来完成所有这些工作。但现在这应该对你有帮助。