Удалить пользовательские игнорируемые слова - PullRequest
1 голос
/ 08 июля 2020

Я пытаюсь удалить стоп-слова на этапе предварительной обработки NLP. Я использую функцию remove_stopwords() из gensim, но также хотел бы добавить свои собственные стоп-слова

# under this method, these custom stopwords still show up after processing
custom_stops = ["stopword1", "stopword2"]
data_text['text'].apply(lambda x: [item for item in x if item not in custom_stops])
# remove stopwords with gensim
data_text['filtered_text'] = data_text['text'].apply(lambda x: remove_stopwords(x.lower()))
# split the sentences into a list
data_text['filtered_text'] = data_text['filtered_text'].apply(lambda x: str.split(x))

Ответы [ 2 ]

0 голосов
/ 08 июля 2020

Мне удалось заставить его работать со следующим:

custom_stops = ["stopword1", "stopword2"]
# remove stopwords with gensim
data_text['filtered_text'] = data_text['text'].apply(lambda x: remove_stopwords(x.lower()))
# split the sentence
data_text['filtered_text'] = data_text['filtered_text'].apply(lambda x: str.split(x))
# remove the custom stopwords
data_text['filtered_text'] = data_text['filtered_text'].apply(lambda x: [item for item in x if item.lower() not in custom_stops])
0 голосов
/ 08 июля 2020

После того, как программа удалила все нестандартные стоп-слова из строки, вот что вы можете сделать, чтобы удалить пользовательские:

custom_stops = ["stopword1", "stopword2"]

s = 'I am very stopword1 and also very stopword2!'

for c in custom_stops:
    s = s.replace(c,'').replace('  ',' ')

print(s)

Вывод:

I am very and also very !
...