python基于百度,哈工大等停用表进行的中文分词

python 复制代码
import os
import pandas as pd
import jieba


# 加载停用词
def load_stopwords(filenames):
    stopwords = set()
    for filename in filenames:
        with open(filename, 'r', encoding='utf-8') as f:
            for line in f:
                stopwords.add(line.strip())
    return stopwords


# 中文分词并去除停用词
def segment_and_remove_stopwords(text, stopwords):
    words = jieba.cut(text)
    filtered_words = [word for word in words if word not in stopwords and len(word) > 1]
    return ' '.join(filtered_words)


# 处理评论数据
def process_comments(df, comment_column, stopwords):
    df['connected_words'] = df[comment_column].apply(lambda x: segment_and_remove_stopwords(x, stopwords))
    return df


# 主函数
def main(input_file_path, output_file_path, comment_column, stopwords_files=[]):
    # 加载停用词
    stopwords = load_stopwords(stopwords_files)

    # 读取CSV文件
    df = pd.read_csv(input_file_path, encoding='utf-8')

    # 处理评论数据
    processed_df = process_comments(df, comment_column, stopwords)

    # 保存处理后的数据到新的CSV文件
    processed_df.to_csv(output_file_path, index=False, encoding='utf-8-sig')

    print(f"数据预处理完成,已保存到 {output_file_path}")



if __name__ == '__main__':
    input_file_path = r"D:\pycharm\爬虫案列\24.汽车之家\_0_10.csv"  # 你的CSV文件路径
    output_file_path = 'comments_processed.csv'  # 输出文件的路径
    comment_column = '空间'  # 假设评论数据在'comment'列中

    # 停用词文件列表,确保这些文件在你的工作目录中
    stopwords_files = [
        r"stopwords-master\baidu_stopwords.txt",
        r"stopwords-master\cn_stopwords.txt",
        r"stopwords-master\hit_stopwords.txt",
        r"stopwords-master\scu_stopwords.txt",
        # ... 其他停用词文件
    ]

    # 确保所有停用词文件都存在
    for filename in stopwords_files:
        if not os.path.exists(filename):
            print(f"Stopwords file {filename} not found.")
            exit(1)

            # 调用主函数处理评论数据
    main(input_file_path, output_file_path, comment_column, stopwords_files)

停用词表可以去看一下博主的上传的资源 , 可以免费获取的

相关推荐
qq_458842156 分钟前
基于SaaS平台的iHRM管理系统测试学习
python·学习·postman
apocelipes12 分钟前
随机数漫谈
linux·数据结构·python·算法·golang·linux编程
交换喜悲36 分钟前
深度学习之半监督学习:一文梳理目标检测中的半监督学习策略
论文阅读·人工智能·python·学习·目标检测·计算机视觉·目标跟踪
Learning改变世界44 分钟前
Shell代码解读
python·shell
那个那个鱼1 小时前
C#面:现有一个整数number,请写一个方法判断这个整数是否是2的N次方
开发语言·算法·c#·.net
爱看书的小沐1 小时前
【小沐学AI】Python实现语音识别(faster-whisper-webui)
人工智能·python·ai·nlp·whisper·语音识别·fast-whisper
啊取名真困难1 小时前
TG群发机器人:高效自动化消息分发指南
python·机器人·自动化
专注VB编程开发20年2 小时前
基于C#在WPF中使用斑马打印机进行打印
hadoop·c#·wpf·斑马打印机·zpl打印机
yangshuquan2 小时前
闲聊 .NET Standard
c#·.net core·基础知识·.net standard
疯狂学习GIS2 小时前
核对不同文件夹所含内容的差异并提取缺失内容:Python代码
python·学术工作效率