Как я могу удалить стоп-слово для массива строк в Python? - PullRequest
0 голосов
/ 26 января 2019

У меня проблема, когда пользователь вводит строку, тогда я вызываю стоп-слово, это вызывает ошибку [удалить стоп-слово r ] [1]

Удалить стоп-слово

#read Question
Qustionst_input=[]
for i in range(3):
    Qustionst_input.append(input("Enter Final Qustion"))
Questions=str(Qustionst_input).strip('[]') #convert array string to string

1 Ответ

0 голосов
/ 26 января 2019

Непонятно, чего вы пытаетесь достичь, но этот код может решить вашу проблему.Приведенный ниже код содержит 3 вопроса, которые были набраны, токенизированы и нормализованы, удалены английские стоп-слова и обычные знаки препинания.

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

# Import the string module needed to remove punctuation characters
from string import punctuation

# English stop words to remove from text.
# A stop word is a commonly used word, such
# as “the”, “a”, “an”, “in”
stop_words = set(stopwords.words('english'))

# ASCII characters which are considered punctuation characters.
# These characters will be removed from the text
exclude_punctuation = set(punctuation)

# Combine the stop words and the punctuations to remove
exclude_combined = set.union(stop_words, exclude_punctuation)

question_input = []
for i in range(3):
  question_input.append(input("Please Enter Your Question: "))

# converts the question list into a group of strings that are separated by a comma 
questions = (', '.join(question_input))

# Tokenize and normalized the questions
tokenize_input = word_tokenize(questions.lower().strip())

# Remove the English stop words and punctuations
expunge_stopwords_punctuations = [word for word in tokenize_input if not word in exclude_combined]

print (expunge_stopwords_punctuations)

sys.exit(0)


#####################################################
# INPUT 
# Please Enter Your Question: This is a question.
# Please Enter Your Question: This is another question. 
# Please Enter Your Question: This is the final question.
#####################################################

#####################################################
# OUTPUT 
# ['question', 'another', 'question', 'final', 'question']
#####################################################
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...