import jieba
import re
import json
import tqdm
import gensim
def load_data(self):
with open('0.5-.csv') as file:
data = file.read()
data = json.loads(data)
return data
def fenci_data(self):
text = []
print(".....")
data = self.load_data()
with open('stoplist.txt') as file:
stop_word_list = file.read()
for weibo_item in tqdm(data):
tmp = []
sentence=''.join(re.findall(r'[\u4e00-\u9fa5]+',weibo_item['weibo_cont']))
for word in jieba.lcut(sentence):
if word not in stop_word_list:
tmp.append(word)
text.append(tmp)
return text
def weibo_lda(self):
text = self.fenci_data()
dictionary = Dictionary(text)
corpus = [dictionary.doc2bow(tmp) for tmp in text]
return dictionary, corpus
def choose_topic(self):
dictionary, corpus = self.weibo_lda()
texts = self.fenci_data()
for i in range(1,16):
print('目前的topic个数:{}'.format(i))
print('目前的数据量:{}'.format(len(texts)))
temp = 'lda_{}_{}'.format(i,len(texts))
tmp = gensim.models.ldamodel.LdaModel(corpus, num_topics=i, id2word=dictionary, passes=20)
file_path = './{}.model'.format(temp)
tmp.save('LDA.csv')
print('------------------')

请问LDA的这段代码,为什么不执行也不报错
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- CSDN专家-HGJ 2021-03-31 10:34关注
代码只是给出了几个函数,并没有定义类及实例化对象和对函数的调用,当然不会有结果输出。
将这些函数写类中:class wordParse:
在最后用,output=wordParse(),output.choose_topic()调用即可。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用