如何在text2vec中设置切分中文的itoken?这个例子是针对英语的!目前已有的中文分词软件包有:解霸等,但我想用text2vec做文本聚类和线性回归模型。此外,如何进行文本聚类?
library(text2vec)
library(data.table)
# 数据准备
#首先运用Setkey为数据设置唯一的“主键”,并划分为训练集和测试集。
data("movie_review")
setDT(movie_review)
setkey(movie_review, id)
set.seed(2016L)
all_ids=movie_review$id
train_ids=sample(all_ids, 4000)
test_ids=setdiff(all_ids, train_ids)
train=movie_review[J(train_ids)]
test=movie_review[J(test_ids)]
#文档向量化
#文档向量化是text2vec的主要步骤,创建词表(vocabulary)前需要设置itoken分词迭代 器,然后用create_vocabulary创建词表,形成语料文件,构建DTM矩阵。
prep_fun=tolower
#代表词语划分到什么程度
tok_fun=word_tokenizer
#步骤1.设置分词迭代器
it_train=itoken(train$review, preprocessor=prep_fun, tokenizer=tok_fun,
ids=train$id, progressbar=FALSE)
#步骤2.分词#消除停用词
stop_words=c("i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours")
#分词函数
vocab=create_vocabulary(it_train, stopwords=stop_words)
#对低频词的修建
pruned_vocab=prune_vocabulary(vocab,
term_count_min=10,doc_proportion_max=0.5, doc_proportion_min=0.001)
#词频,低于10个都删掉发布于 2017-05-07 18:28:45
过程与其他语言相同(请参阅http://text2vec.org的详细信息),但您需要提供记号赋予器。我建议看看stringi::stri_split或tokenizers::tokenize_words(使用stringi)。例如:
stringi::stri_split_boundaries("首先运用Setkey为数据设置唯一的“主键, 并划分为训练集和测试集", type="word", skip_word_none=TRUE)
> [1] "首先" "运用" "Setkey" "为" "数据" "设置" "唯一" "的" "主" "键" "并" "划分为" "训练" "集" "和" "测试" "集"https://stackoverflow.com/questions/43777517
复制相似问题