gpt4 book ai didi

r - R 中的主题建模 : Building topics based on a predefined list of terms

转载 作者:行者123 更新时间:2023-12-02 17:47:16 24 4
gpt4 key购买 nike

我花了几天时间研究 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 中定义这样的条件?

最佳答案

也许是这样的?

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)) 

当您运行主题模型函数时,它应该与您确定为模型一部分的二元模型一起使用(尽管根据您确定的内容,列表会更长)。

关于r - R 中的主题建模 : Building topics based on a predefined list of terms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28329006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com