首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中的主题建模:基于预先定义的术语列表构建主题

R中的主题建模:基于预先定义的术语列表构建主题
EN

Stack Overflow用户
提问于 2015-02-04 18:44:45
回答 1查看 733关注 0票数 4

我花了几天时间研究R中的主题模型,我想知道我是否可以做以下工作:

我希望R基于一个预定义的具有特定术语的术语列表构建主题。我已经使用这个列表来识别文档中的ngram (RWeka),并使用以下代码只计算在我的术语列表中出现的术语:

代码语言:javascript
复制
terms=read.delim("TermList.csv", header=F, stringsAsFactor=F)

biTok=function(x) NGramTokenizer(x, Weka_control(min=1, max=4))

tdm=TermDocumentMatrix(data.corpus, control=list(tokenizer=biTok))

现在,我想再次使用这个列表来搜索文档中的主题,只基于我的术语列表中的术语。

在下面这句话中:“这些安排使团队绩效更高,用户满意度更好”,我希望在主题中使用“团队绩效”和“用户满意度”这两个复合术语,而不是将“团队”、“绩效”、“用户”和“满意”作为单一术语来处理,并在它们之上构建主题。这就是我需要使用预定义列表的原因。

是否有可能在R中定义这样一个条件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-05 02:28:17

也许是这样的?

代码语言:javascript
复制
tokenizing.phrases <- c("team performance", "user satisfaction") # plus your others you have identified

然后加载此函数:

代码语言:javascript
复制
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 we don't have to worry about multiple occurences 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)
  }

  # get rid of any extraneous empty strings, which can happen if a phrase occurs just before a punctuation
  out[out != ""]
}

然后使用预定义的tokeninzing.phrases创建术语文档矩阵:

代码语言:javascript
复制
tdm <- TermDocumentMatrix(corpus, control = list(tokenize = phraseTokenizer)) 

然后,当您运行主题模型函数时,它应该与您标识为模型一部分的大图一起工作(尽管根据您所识别的内容列出了更长的列表)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28329006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档