我想创建一个包含字符 n 元语法的术语文档矩阵。例如,采用以下句子:
“在本文中,我们专注于一种不同但简单的文本表示。”
字符 4-gram 为:|In_t|、|n_th|、|_thi|、|this|、|his__|、|is_p|、|s_pa|、|_pap|、|pape|、|aper|、等等
我已经使用 R/Weka 包来处理“词袋”n-gram,但我在调整标记器(例如下面的标记器)来处理字符时遇到了困难:
BigramTokenizer <- function(x){
NGramTokenizer(x, Weka_control(min = 2, max = 2))}
tdm_bigram <- TermDocumentMatrix(corpus,
control = list(
tokenize = BigramTokenizer, wordLengths=c(2,Inf)))
关于如何使用 R/Weka 或其他包创建字符 n-gram 有什么想法吗?
最佳答案
我发现quanteda
非常有用:
library(tm)
library(quanteda)
txts <- c("In this paper.", "In this lines this.")
tokens <- tokenize(gsub("\\s", "_", txts), "character", ngrams=4L, conc="")
dfm <- dfm(tokens)
tdm <- as.TermDocumentMatrix(t(dfm), weighting=weightTf)
as.matrix(tdm)
# Docs
# Terms text1 text2
# In_t 1 1
# n_th 1 1
# _thi 1 2
# this 1 2
# his_ 1 1
# is_p 1 0
# s_pa 1 0
# _pap 1 0
# pape 1 0
# aper 1 0
# per. 1 0
# is_l 0 1
# s_li 0 1
# _lin 0 1
# line 0 1
# ines 0 1
# nes_ 0 1
# es_t 0 1
# s_th 0 1
# his. 0 1
关于r - R 中的 "Bag of characters"n 元语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581065/