我花了几天时间研究R中的主题模型,我想知道我是否可以做以下工作:
我希望R基于一个预定义的具有特定术语的术语列表构建主题。我已经使用这个列表来识别文档中的ngram (RWeka),并使用以下代码只计算在我的术语列表中出现的术语:
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中定义这样一个条件?
发布于 2015-02-05 02:28:17
也许是这样的?
tokenizing.phrases <- c("team performance", "user satisfaction") # plus your others you have identified然后加载此函数:
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创建术语文档矩阵:
tdm <- TermDocumentMatrix(corpus, control = list(tokenize = phraseTokenizer)) 然后,当您运行主题模型函数时,它应该与您标识为模型一部分的大图一起工作(尽管根据您所识别的内容列出了更长的列表)。
https://stackoverflow.com/questions/28329006
复制相似问题