Удаление стоп-слов из NLTK

Я пытаюсь прочитать один текстовый файл (foo1.txt), удалить все заданные nltk стоп-слова и записать в другой файл (foo2.txt). Код выглядит следующим образом: Требуется импорт: из nltk.corpus импорт стоп-слов

def stop_words_removal(): 
    with open("foo1.txt") as f:
            reading_file_line = f.readlines() #entire content, return  list 
            #print reading_file_line #list
            reading_file_info = [item.rstrip('\n') for item in reading_file_line]
            #print reading_file_info #List and strip \n
            #print ' '.join(reading_file_info)
            '''-----------------------------------------'''
            #Filtering & converting to lower letter
            for i in reading_file_info:
                words_filtered = [e.lower() for e in i.split() if len(e) >= 4]                
                print words_filtered

            '''-----------------------------------------'''
            '''removing the strop words from the file'''
            word_list = words_filtered[:] 
            #print word_list
            for word in words_filtered:
                        if word in nltk.corpus.stopwords.words('english'): 
                            print word
                            print word_list.remove(word)

            '''-----------------------------------------'''
            '''write the output in a file'''
            z = ' '.join(words_filtered)
            out_file = open("foo2.txt", "w")
            out_file.write(z)
            out_file.close()  

Проблема в том, что 2-я часть кода "удаление слов из файла" не работает. Любое предложение будет с благодарностью. Благодарю.

Example Input File: 
'I a Love this car there', 'positive',
'This a view is amazing there', 'positive',
'He is my best friend there', 'negative'

Example Output:
['love', "car',", "'positive',"]
['view', "amazing',", "'positive',"]
['best', "friend',", "'negative'"]

Я пытался, как это предложено в этой ссылке, но ни один из них не работает

1 ответ

Решение

Вот что я бы сделал внутри вашей функции:

with open('input.txt','r') as inFile, open('output.txt','w') as outFile:
    for line in inFile:
        print(''.join([word for word in line.lower().translate(None, string.punctuation).split() 
              if len(word) >=4 and word not in stopwords.words('english')]), file=outFile)

Не забудьте добавить:

from __future__ import print_function                   

если вы на Python 2.x.

Другие вопросы по тегам