有没有大佬帮我看一下LDA代码问题

有没有大佬帮我看一下LDA代码问题

from gensim import corpora, models

# 主题模型

class TopicModel(object):

# 三个传入参数:处理后的数据集,关键词数量,具体模型(LSI、LDA),主题数量

def __init__(self, doc_list, keyword_num, model='LSI', num_topics=4):

# 使用gensim的接口,将文本转为向量化表示

# 先构建词空间

self.dictionary = corpora.Dictionary(doc_list)

# 使用BOW模型向量化

corpus = [self.dictionary.doc2bow(doc) for doc in doc_list]

# 对每个词,根据tf-idf进行加权,得到加权后的向量表示

self.tfidf_model = models.TfidfModel(corpus)

self.corpus_tfidf = self.tfidf_model[corpus]

self.keyword_num = keyword_num

self.num_topics = num_topics

# 选择加载的模型

if model == 'LSI':

self.model = self.train_lsi()

else:

self.model = self.train_lda()

# 得到数据集的主题-词分布

word_dic = self.word_dictionary(doc_list)

self.wordtopic_dic = self.get_wordtopic(word_dic)

    def train_lsi(self):  

lsi = models.LsiModel(self.corpus_tfidf, id2word=self.dictionary, num_topics=self.num_topics)

return lsi

def train_lda(self):

lda = models.LdaModel(self.corpus_tfidf, id2word=self.dictionary, num_topics=self.num_topics)

return lda

def get_wordtopic(self, word_dic):

wordtopic_dic = {}

for word in word_dic:

single_list = [word]

wordcorpus = self.tfidf_model[self.dictionary.doc2bow(single_list)]

wordtopic = self.model[wordcorpus]

wordtopic_dic[word] = wordtopic

return wordtopic_dic

    # 计算词的分布和文档的分布的相似度,取相似度最高的keyword_num个词作为关键词  

def get_simword(self, word_list):

sentcorpus = self.tfidf_model[self.dictionary.doc2bow(word_list)]

senttopic = self.model[sentcorpus]

# 余弦相似度计算

def calsim(l1, l2):

a, b, c = 0.0, 0.0, 0.0

for t1, t2 in zip(l1, l2):

x1 = t1[1]

x2 = t2[1]

a += x1 * x1

b += x1 * x1

c += x2 * x2

sim = a / math.sqrt(b * c) if not (b * c) == 0.0 else 0.0

return sim

        # 计算输入文本和每个词的主题分布相似度  

sim_dic = {}

for k, v in self.wordtopic_dic.items():

if k not in word_list:

continue

sim = calsim(v, senttopic)

sim_dic[k] = sim

for k, v in sorted(sim_dic.items(), key=functools.cmp_to_key(cmp), reverse=True)[:self.keyword_num]:

print(k + "/ ", end='')

print()

    @staticmethod  

# 词空间构建方法和向量化方法,在没有gensim接口时的一般处理方法

def word_dictionary(doc_list):

dictionary = []

for doc in doc_list:

dictionary.extend(doc)

dictionary = list(set(dictionary))

return dictionary

def doc2bowvec(self, word_list):

vec_list = [1 if word in word_list else 0 for word in self.dictionary]

return vec_list

def topic_extract(word_list, model, pos=False, keyword_num=10):  

doc_list = load_data(pos)

topic_model = TopicModel(doc_list, keyword_num, model=model)

topic_model.get_simword(word_list)

if __name__ == '__main__':

text = '费尔南多·托雷斯(Fernando Jose Torres Sanz),1984年3月20日出生于西班牙马德里,' +

'西班牙足球运动员,司职前锋,效力于日本职业足球甲级联赛鸟栖砂岩足球俱乐部。' +

'托雷斯2001出道于马德里竞技,2007年加盟英超利物浦,2011年转会切尔西,' +

'期间帮助球队夺得了2012年欧洲冠军联赛冠军,其后以租借的形式加盟AC米兰,' +

'2014年12月,托雷斯宣布回归马德里竞技。2018年7月,托雷斯宣布加盟日本鸟栖砂岩足球俱乐部。' +

'2004年欧洲杯,托雷斯首次代表国家队参加国际大赛,2008年和2012年跟随西班牙队两度夺得欧洲杯冠军,' +

'2010年随队夺得世界杯冠军,其个人在2008年荣膺欧洲杯决赛MVP,2012获得欧洲杯金靴奖、2013年获得联合会杯金靴奖。'

    pos = True  

seg_list = seg_to_list(text, pos)

filter_list = word_filter(seg_list, pos)

print('LSI模型结果:')

topic_extract(filter_list, 'LSI', pos)

print('LDA模型结果:')

topic_extract(filter_list, 'LDA', pos)

我在我的电脑上跑总报错

Traceback (most recent call last):

File "E:/jj/pp.py", line 107, in <module>

seg_list = seg_to_list(text, pos)

TypeError: 'int' object is not callable

以上是 有没有大佬帮我看一下LDA代码问题 的全部内容, 来源链接: utcz.com/p/937872.html

回到顶部