码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • NLP---文本前期预处理的几个步骤


    1、读取文本

    text1 ="""
    Football is a family of team sports that involve, to varying degrees, kicking a ball to score a goal. 
    Unqualified, the word football is understood to refer to whichever form of football is the most popular 
    in the regional context in which the word appears. Sports commonly called football in certain places 
    include association football (known as soccer in some countries); gridiron football (specifically American 
    football or Canadian football); Australian rules football; rugby football (either rugby league or rugby union); 
    and Gaelic football. These different variations of football are known as football codes.
    """
    print("原文:\n", text1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、去除换行符

    text = text1.replace("\n", "")
    print("去除原文中的换行符:\n", text)
    
    • 1
    • 2

    3、分句

    import nltk
    sents = nltk.sent_tokenize(text)
    print("将文本进行分句:\n", sents)
    
    • 1
    • 2
    • 3

    4、分词

    import string
    punctuation_tokens = []
    for sent in sents:
        for word in nltk.word_tokenize(sent):
            punctuation_tokens.append(word)
    print("将每个句子进行分词:\n", punctuation_tokens)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5、过滤标点符号

    tokens = []
    for word in punctuation_tokens:
        if word not in string.punctuation:
            tokens.append(word)
    print("将分词结果去除标点符号:\n", tokens)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6、过滤停用词

    from nltk.corpus import stopwords
    fltered = [w for w in tokens if w not in stopwords.words("english")]
    print("过滤完停用词之后:\n", fltered)
    
    • 1
    • 2
    • 3

    7、剩下有用的单词进行计数

    from collections import Counter
    count = Counter(fltered)
    print("对最终清洗好的单词进行计数:\n", count)
    
    • 1
    • 2
    • 3
  • 相关阅读:
    剑指offer 46. 把数组排成最小的数
    linux环境验证c++程序库间调用
    Linux0.12内核源码解读(2)-Bootsect.S
    HiveQL性能调优-概览
    SpringBoot-AOP学习案例
    Kotlin File writeText appendText appendBytes readBytes readText
    java毕业设计毕业论文管理系统Mybatis+系统+数据库+调试部署
    Spire.Office for Java 7.10.4 ~Spire.Office 旗舰版
    Linux环境redis外网连接配置
    详解欧拉计划第211题:约数平方和
  • 原文地址:https://blog.csdn.net/qq_45893319/article/details/134388277
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号