使用短语而不是单个单词在R中进行主题建模

使用短语而不是单个单词在R中进行主题建模,第1张

概述我正在尝试做一些主题建模,但想要使用存在而不是单个单词短语 即 library(topicmodels)library(tm)my.docs = c('the sky is blue, hot sun', 'flowers,hot sun', 'black cats, bees, rats and mice')my.corpus = Corpus(VectorSource(my.docs) 我正在尝试做一些主题建模,但想要使用存在而不是单个单词的短语

library(topicmodels)library(tm)my.docs = c('the sky is blue,hot sun','flowers,'black cats,bees,rats and mice')my.corpus = Corpus(VectorSource(my.docs))my.dtm = documentTermMatrix(my.corpus)inspect(my.dtm)

当我检查我的dtm时,它将所有单词分开,但我想要所有的短语,即每个应该有一个列:
天是蓝的
烈日
花卉
黑猫
蜜蜂
大鼠和小鼠

如何使文档术语矩阵识别短语和单词?它们是逗号分隔的

解决方案需要高效,因为我想在大量数据上运行它

解决方法 您可以尝试使用自定义标记生成器的方法.您将所需的多字词定义为短语(我不知道执行该步骤的算法代码):

tokenizing.phrases <- c("sky is blue","hot sun","black cats")

请注意,没有完成任何干扰,所以如果你想要“黑猫”和“黑猫”,那么你需要输入两种变体.案例被忽略.

然后你需要创建一个函数:

phrasetokenizer <- function(x) {      require(stringr)      x <- as.character(x) # extract the plain text from the tm Textdocument object      x <- str_trim(x)      if (is.na(x)) return("")      #warning(paste("doing:",x))      phrase.hits <- str_detect(x,ignore.case(tokenizing.phrases))      if (any(phrase.hits)) {        # only split once on the first hit,so you don't have to worry about multiple occurrences of the same phrase        split.phrase <- tokenizing.phrases[which(phrase.hits)[1]]         # warning(paste("split phrase:",split.phrase))        temp <- unList(str_split(x,ignore.case(split.phrase),2))        out <- c(phrasetokenizer(temp[1]),split.phrase,phrasetokenizer(temp[2]))       } else {        out <- MC_tokenizer(x)      } out[out != ""]}

然后,您可以正常进行创建术语文档矩阵,但这次您可以通过控制参数在语料库中包含标记化短语.

tdm <- TermdocumentMatrix(corpus,control = List(tokenize = phrasetokenizer))
总结

以上是内存溢出为你收集整理的使用短语而不是单个单词在R中进行主题建模全部内容,希望文章能够帮你解决使用短语而不是单个单词在R中进行主题建模所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1077920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存