Как я могу удалить слова "они" и "мы" из стоп-слова nltk.corpus? - PullRequest
0 голосов
/ 24 октября 2019

Я знаю, что могу обновить набор стоп-слов, добавив к нему, но как я могу удалить из него некоторые стоп-слова, которые мне нужны в моем анализе, есть ли способ сделать это с помощью python?

from nltk.corpus import stopwords
stop_words = stopwords.words('english')
print("stop_words :",stop_words)
stop_words_none = stop_words.remove("they")
print("stop_words without they: ",stop_words_none)

но вывод:

stop_words ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
stop_words without they: None

1 Ответ

0 голосов
/ 26 октября 2019

Список в python является изменяемым объектом, как указано здесь :

Изменяемый объект может быть изменен после его создания, а неизменный объект - нет. Объекты встроенных типов, такие как (int, float, bool, str, tuple, unicode), являются неизменяемыми. Объекты встроенных типов, таких как (list, set, dict), являются изменяемыми.

Метод удаления списка () из python не создает новый список, он изменяет список, заданный в качестве аргумента, см. здесь :

Метод списка Python remove () ищет указанный элемент в списке и удаляет первый соответствующий элемент.

Возвращаемое значение : Этот метод списка Python не возвращает никакого значения, но удаляет данный объект из списка.

Следующий код показывает, что слово «они» действительно удалено из списка:

from nltk.corpus import stopwords
stop_words = stopwords.words('english')

print('they' in stop_words)
#True
stop_words.remove("they")
print('they' in stop_words)
#False
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...