赤点规避 2022-02-18 14:27 采纳率: 66.7%
浏览 27
已结题

用nltk去停用词如何分行?(语言-python)

想用nltk库进行数据的预处理,发现数据在分词完成时还是能正常按各条数据分行的,但去停用词后全都合成了一行,这个该怎么解决呢?
问题部分代码如下,都是从论坛找的,能正确运行:

(前面部分清理得到数据t)
#分词并删除停用词
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(t)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
print(filtered_sentence)

原始数据为:
能分条

img

去停用词后:
全合成一行

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2022-02-18 15:56
    关注

    可以用for循环写成嵌套列表进行处理,示例如下,获取按行去除停用词的分词结果,并可以直接写入csv或者excel中:

    from nltk.corpus import stopwords
    from nltk.tokenize import word_tokenize
    import pandas as pd
    
    example_sent = """This is a sample sentence,showing off the stop words filtration.\n Hello guys!"""
    
    stop_words = set(stopwords.words('english'))
    
    word_tokens = [word_tokenize(x) for x in example_sent.split('\n')]
    filtered_sentence = []
    for wd in word_tokens:
        cent=[]
        for w in wd:
            if w not in stop_words:
                cent.append(w)            
        filtered_sentence.append(cent)
    print(word_tokens)
    print(filtered_sentence)
    df=pd.DataFrame(filtered_sentence)
    print(df)
    
    

    运行结果:

     0       1         2     3        4     5      6           7     8
    0   This  sample  sentence     ,  showing  stop  words  filtration     .
    1  Hello    guys         !  None     None  None   None        None  None
    
    

    如有帮助和启发,请点采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 已采纳回答 2月25日
  • 创建了问题 2月18日

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本