编辑:我在这里发现了一个有趣的问题。此链接显示 gensim 在训练和推理步骤中都使用随机性。所以这里建议的是设置一个固定的种子,以便每次都能得到相同的结果。但是,为什么我对每个主题都得到相同的概率?
我想做的是为每个推特用户找到她的主题,并根据主题的相似性计算推特用户之间的相似度。是否有可能为 gensim 中的每个用户计算相同的主题,还是我必须计算一个主题字典并聚类每个用户主题?
一般来说,基于 gensim 中的主题模型提取比较两个 twitter 用户的最佳方法是什么?我的代码如下:
def preprocess(id): #Returns user word list (or list of user tweet)
user_list = user_corpus(id, 'user_'+str(id)+'.txt')
documents = []
for line in open('user_'+str(id)+'.txt'):
documents.append(line)
#remove stop words
lines = [line.rstrip() for line in open('stoplist.txt')]
stoplist= set(lines)
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
# remove words that appear only once
all_tokens = sum(texts, [])
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) < 3)
texts = [[word for word in text if word not in tokens_once]
for text in texts]
words = []
for text in texts:
for word in text:
words.append(word)
return words
words1 = preprocess(14937173)
words2 = preprocess(15386966)
#Load the trained model
lda = ldamodel.LdaModel.load('tmp/fashion1.lda')
dictionary = corpora.Dictionary.load('tmp/fashion1.dict') #Load the trained dict
corpus = [dictionary.doc2bow(words1)]
tfidf = models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
corpus_lda = lda[corpus_tfidf]
list1 = []
for item in corpus_lda:
list1.append(item)
print lda.show_topic(0)
corpus2 = [dictionary.doc2bow(words2)]
tfidf2 = models.TfidfModel(corpus2)
corpus_tfidf2 = tfidf2[corpus2]
corpus_lda2 = lda[corpus_tfidf2]
list2 = []
for it in corpus_lda2:
list2.append(it)
print corpus_lda.show_topic(0)
返回用户语料库的主题概率(当使用用户词列表作为语料库时):
[(0, 0.10000000000000002), (1, 0.10000000000000002), (2, 0.10000000000000002),
(3, 0.10000000000000002), (4, 0.10000000000000002), (5, 0.10000000000000002),
(6, 0.10000000000000002), (7, 0.10000000000000002), (8, 0.10000000000000002),
(9, 0.10000000000000002)]
在我使用用户推文列表的情况下,我会返回每条推文的计算主题。
问题 2:以下是否有意义:使用之前计算的 LDA 模型,用几个 twitter 用户训练 LDA 模型并为每个用户(每个用户语料库)计算主题?
在提供的示例中,list[0]
返回具有相等概率 0.1 的主题分布。基本上,每一行文本对应一条不同的推文。如果我用它计算语料库,corpus = [dictionary.doc2bow(text) for text in texts]
它将分别给我每条推文的概率。另一方面,如果我corpus = [dictionary.doc2bow(words)]
像示例一样使用,我将只使用所有用户词作为语料库。在第二种情况下, gensim 返回所有主题的相同概率。因此,对于两个用户,我都得到了相同的主题分布。
用户文本语料库应该是单词列表还是句子列表(推文列表)?
关于twitterRank 方法中 Qi He 和 Jianshu Weng 的实现在第 264 页中说:我们将个人 twitterer 发布的推文聚合成一个大文档。因此,每个文档对应一个twitterer。好的,我很困惑,如果文档将是所有用户推文,那么语料库应该包含什么?