简体   繁体   English

删除停用词和标点符号时出现错误

[英]Error is coming while removing stop words and punctuation

def transform_text(text):
    text = text.lower()
    text = nltk.word_tokenize(text)
    
    y = []
    for i in text:
        if i.isalnum():
            y.append(i)
    
    text = y[:]
    y.clear()
    
    for i in text:
        if i not in stopwords.words('english') and i not in string.punctuation:
            y.append(i)
            
    text = y[:]
    y.clear()
    
    for i in text:
        y.append(ps.stem(i))
    
            
    return " ".join(y)

gives

<ipython-input-47-c84ab809613a> in <module>
----> 1 transform_text("I'm gonna be home soon and i don't want to talk about this stuff anymore tonight, k? I've cried enough today.")

<ipython-input-46-fed03b80da62> in transform_text(text)
     12 
     13     for i in text:
---> 14         if i not in stopwords.words('english') and i not in string.punctuation:
     15             y.append(i)
     16 

NameError: name 'stopwords' is not defined

You need to include the following at the top of your module:您需要在模块顶部包含以下内容:

from nltk.corpus import stopwords

NameError: name 'stopwords' is not defined means exactly that - you haven't imported or defined what stopwords is yet. NameError: name 'stopwords' is not defined确切的意思是——你还没有导入或定义什么是stopwords

Read the docs for more details.阅读文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM