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)

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

相关文章
|
2月前
|
监控 数据处理 索引
使用Python批量实现文件夹下所有Excel文件的第二张表合并
使用Python和pandas批量合并文件夹中所有Excel文件的第二张表,通过os库遍历文件,pandas的read_excel读取表,concat函数合并数据。主要步骤包括:1) 遍历获取Excel文件,2) 读取第二张表,3) 合并所有表格,最后将结果保存为新的Excel文件。注意文件路径、表格结构一致性及异常处理。可扩展为动态指定合并表、优化性能、日志记录等功能。适合数据处理初学者提升自动化处理技能。
72 1
|
2月前
|
数据采集 Python
爬虫实战-Python爬取百度当天热搜内容
爬虫实战-Python爬取百度当天热搜内容
118 0
|
2月前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
196 0
|
2月前
|
关系型数据库 MySQL 数据库连接
python查询数据库的某个表,将结果导出Excel
python查询数据库的某个表,将结果导出Excel
87 0
|
1天前
|
机器学习/深度学习 缓存 安全
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
Python标准库中的`str`类型有一个`translate()`方法,它用于替换字符串中的字符或字符子集。这通常与`str.maketrans()`方法一起使用,后者创建一个映射表,用于定义哪些字符应该被替换。
4 0
|
1月前
|
存储 SQL 数据挖掘
Python 日期表制作
Python 日期表制作
24 4
|
17天前
|
JSON Serverless 开发工具
函数计算产品使用问题之要使用Python写入时序数据到阿里云表格存储时序表,该怎么办
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
1月前
|
vr&ar 索引 Python
Python基础教程(第3版)中文版 第二章列 表和元组(笔记)
Python基础教程(第3版)中文版 第二章列 表和元组(笔记)
|
2月前
|
程序员 PHP Python
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
2024年Python最全Python基础教程:keys()、values()和 items()方法,百度面试题php
|
2月前
|
索引 Python
使用Python的Pandas库进行数据透视表(pivot table)操作
使用Python Pandas进行数据透视表操作包括:安装Pandas库,导入库,创建或读取数据,如`pd.DataFrame()`或从文件读取;然后使用`pd.pivot_table()`创建透视表,指定数据框、行索引、列索引和值,例如按姓名和科目分组计算平均分;查看结果通过打印数据透视表;最后可使用`to_csv()`等方法保存到文件。这为基础步骤,可按需求调整参数实现更多功能。
95 2