• NLTK进行文本清洗并进行词性标注


    import pandas as pd
    import re
    import nltk
    from nltk import word_tokenize, pos_tag
    from nltk.corpus import stopwords

    # 下载NLTK的停用词和词性标注所需的资源
    nltk.download('stopwords')
    nltk.download('punkt')
    nltk.download('averaged_perceptron_tagger')

    # 读取停用词表
    with open('en_stopwords.txt', 'r') as f:
        stopwords_set = set(f.read().splitlines())

    # 读取Excel文件
    df = pd.read_excel('原始新闻合并.xlsx', usecols=['content'])

    # 定义文本清洗和词性标注函数
    def clean_and_tag(text):
        # 检查text是否为字符串,如果不是则转换为字符串
        if isinstance(text, str):
            # 去除HTML标签
            cleaned_text = re.sub(r'<.*?>', '', text)
            # 去除多余空格和换行符
            cleaned_text = re.sub(r'\s+', ' ', cleaned_text)
            # 分词
            words = word_tokenize(cleaned_text)
            # 去除停用词
            filtered_words = [word.lower() for word in words if word.lower() not in stopwords_set]
            # 词性标注
            tagged_words = pos_tag(filtered_words)
            # 将词汇和词性标签拼接为字符串
            tagged_text = ' '.join([f"{word}/{tag}" for word, tag in tagged_words])
            return tagged_text
        else:
            # 如果text不是字符串,返回空字符串或者其他你认为合适的默认值
            return ''

    # 对每条新闻内容进行文本清洗和词性标注
    df['cleaned_content'] = df['content'].apply(clean_and_tag)

    # 将清洗后的文本保存到Excel文件
    df['cleaned_content'].to_excel('清洗后的文本.xlsx', index=False)

    # 输出清洗后的文本内容
    print("清洗后的文本已保存到 '清洗后的文本.xlsx' 文件。")
     

  • 相关阅读:
    Python与数据分析--Matplotlib-1
    Qt Creator 的 Shadow build(影子构建)
    nginx 配置错误目录遍历漏洞
    Vue3中动态渲染菜单栏(TS)
    Golang【Web 入门】 07 路由 - http.ServeMux
    野火FPGA入门(7):IP核调用
    Mysql中事件和定时任务
    压电换能器的工作原理和应用(功率放大器)
    4. C++内存管理
    【JVM】运行时数据区 - 虚拟机栈(Java栈)
  • 原文地址:https://blog.csdn.net/weston95/article/details/134083104