LDA模型 python代码样例

# -*- coding: utf-8 -*-
import re
import warnings
import jieba
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from gensim import corpora
from gensim.models.coherencemodel import CoherenceModel
from gensim.models.ldamodel import LdaModel

warnings.filterwarnings('ignore')  # To ignore all warnings that arise here to enhance clarity
PATH = "data_lda.csv"

# 停用词
stop_words_path = "stop_words.txt"
stop_words = []

# 保留词文本路径
# 文本格式: 词语 词频(可省略) 词性(可省略)
# 一个词一行
reserved_words_path = "reserved_words.txt"

with open(stop_words_path, encoding='utf-8') as f:
    for line in f.readlines():
        stop_words.append(line.strip())
f.close()


# 数据清洗, 可以根据自己的需求进行重载
def processing(text):
    text = re.sub("【.+?】", "", text)  # 去除 【xx】 (里面的内容通常都不是用户自己写的)
    text = re.sub("\n", "", text)
    text = re.sub(r'[\W]', "", text)  # 去除标点符号
    text = re.sub(r'[\d]', "", text)  # 去除数字
    return text


# 对句子进行中文分词
def seg_depart(sentence
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值