我想要做的是为每个Twitter用户找到她的主题,并根据主题的相似性计算Twitter用户之间的相似性。是否有可能为gensim中的每个用户计算相同的主题,或者我是否必须计算主题词典并对每个用户主题进行聚类?
一般来说,哪种比较两个Twitter用户的最佳方式是基于gensim中的主题模型提取?我的代码如下:
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 approach中启用Qi He和Jianshu Weng的第264页,它说:我们将个人twitterer发布的推文汇总成一个大文档。因此,每个文档对应于twitterer。好吧我很困惑,如果文档将是所有用户推文那么语料库应包含什么?
答案 0 :(得分:1)
According to official document, Latent Dirichlet Allocation, LDA is a transformation from bag-of-words counts into a topic space of lower dimensionality.
You can use LSI on the top of TFIDF, but not LDA. If you use TFIDF on LDA, then it will generate each topic almost the same, you can print and check it.
答案 1 :(得分:0)
Fere Res检查以下建议here。首先,您必须从所有用户计算lda模型,然后使用未知文档的提取向量,此处计算为
vec_bow = dictionary.doc2bow(doc.lower().split())
vec_lda = lda[vec_bow]
如果您打印以下内容:print(vec_lda)
您将获得将看不见的文档分发到lda模型主题。