我自己会问chatGPT 2021-03-31 09:35 采纳率: 42.9%
浏览 61
已结题

请问LDA的这段代码,为什么不执行也不报错

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('------------------')
  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-03-31 10:34
    关注

    代码只是给出了几个函数,并没有定义类及实例化对象和对函数的调用,当然不会有结果输出。

    将这些函数写类中:class wordParse:

    在最后用,output=wordParse(),output.choose_topic()调用即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月16日
  • 已采纳回答 12月8日

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?