NLTK中的小写停用词并将停用词存储在列表中

时间:2012-09-06 00:29:33

标签: python nltk stop-words

亲爱的朋友们,我想知道如何在名为tokens的列表中小写NLTK中的停用词

import nltk
from nltk.corpus import stopwords
tokens= ['TOWING', 'VESSEL', 'XXXX', 'XXXX', 'XXXX', 'WAS', 'FACING', 'UP', 'TO', 'BARGES', 'IN', 'MON', 'VIEW', 'MININGS', 'FLEET', 'WHEN', 'WIRE', 'CABLE', 'AT', 'THE', 'CELL', 'PARTED', 'STRIKING', 'XXX', 'XXX', 'IN', 'THE', 'LEFT', 'FOREARM', 'LUNDY', 'WAS', 'TAKEN', 'TO', 'THE', 'HOSPITAL', 'VIA', 'AMBULANCE', 'AND', 'DIAGNOSED', 'WITH', 'XXX']

stop_list = ['all', 'just', 'being', 'over', 'both', 'through', 'yourselves', 'its', 'before', 'herself', 'had', 'should', 'to', 'only', 'under', 'ours', 'has', 'do', 'them', 'his', 'very', 'they',
         'not', 'during', 'now', 'him', 'nor', 'did', 'this', 'she', 'each', 'further', 'where', 'few', 'because', 'doing', 'some', 'are', 'our', 'ourselves', 'out', 'what', 'for', 'while', 'does', 'above', 'between', 't',
         'be', 'we', 'who', 'were', 'here', 'hers', 'by', 'on', 'about', 'of', 'against', 's', 'or', 'own', 'into', 'yourself', 'down', 'your', 'from', 'her', 'their', 'there', 'been', 'whom', 'too', 'themselves', 'was',
         'until', 'more', 'himself', 'that', 'but', 'don', 'with', 'than', 'those', 'he', 'me', 'myself', 'these', 'up', 'will', 'below', 'can', 'theirs', 'my', 'and', 'then', 'is', 'am', 'it', 'an', 'as', 'itself', 'at',
         'have', 'in', 'any', 'if', 'again', 'no', 'when', 'same', 'how', 'other', 'which', 'you', 'after', 'most', 'such', 'why', 'a', 'off', 'i', 'yours', 'so', 'the', 'having', 'once']

english_stops = set(stopwords.words('english'))
for coid in range(0,len(english_stops)):
for coidrec in range(0,len(tokens)):
    if tokens[coidrec].isupper():
        if tokens[coidrec].lower == stop_list[coid]:
            tokens[i].lower
        else:
            pass
tokens
['TOWING', 'VESSEL', 'XXXX', 'XXXX', 'XXXX', 'WAS', 'FACING', 'UP', 'TO', 'BARGES', 'IN', 'MON', 'VIEW', 'MININGS', 'FLEET', 'WHEN', 'WIRE', 'CABLE', 'AT', 'THE', 'CELL', 'PARTED', 'STRIKING', 'XXX', 'XXX', 'IN', 'THE', 'LEFT', 'FOREARM', 'LUNDY', 'WAS', 'TAKEN', 'TO', 'THE', 'HOSPITAL', 'VIA', 'AMBULANCE', 'AND', 'DIAGNOSED', 'WITH', 'XXX']

1 个答案:

答案 0 :(得分:2)

试试这个:

tokens = [token.lower() for token in tokens]