简体   繁体   中英

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.

Read the docs for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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