I am a beginner in python trying to make a chatbot for a project in college. We just started this year and our professor asked us to do one, so I am trying to brainstorm ways that I can implement into my project. I learned about the Wikipedia module and I would like to use it to answer basic "what is X" and "who is Y" questions. I was planning to do so by telling the user to input a question, then splitting the question by word and storing each word in a list. Then I wanted the program to compare those words to words in a "keyword list" where if certain words matched the bot would print out a response.
I wanted to make it so the bot knew that after the word "what"(or "who") in the question there might exist a word that is important to the question, and I wanted it to put that word through the Wikipedia summary method.
I was trying something like this:
def storeInput():
question = input("Ask me a question! No typos please tho.")
words = question.split()
return words
def not_understand():
print("Unfortunately I couldn't understand what you meant to say... could you repeat again? Be careful"
" with wording.")
def answer_w_questions():
key=["what", "who"]
for word in words:
if word in key:
print(wikipedia.summary(#important word))
else:
not_understand()
However as you can see I don't know how I can make the bot pick the correct word to use on the Wikipedia summary. Is there any way I can make it detect the proper word to search, or is my current of thought not efficient for the program?
CodePudding user response:
the bot knew that after the word "what"(or "who") in the question there might exist a word that is important to the question
There are several ways to do this.
You might also be interested in having a look at ntlk or spacy for tokenization / stemming
1. String splitting
The simplest one is to split the string:
>>> question = "Who is the president?"
>>> question.lower().split("who")
['', ' is the president?']
As you can see, split returns what was before "who" and what comes after it. So question.lower.split(key)[1]
gives you everything after the keyword.
2. Regex
You could also look at regular expressions (regex): https://docs.python.org/3/library/re.html
3. List index
>>> question = "Who is the president?"
>>> key = "who"
>>> words = question.lower().split()
>>> key_index = words.index(key)
>>> words
['who', 'is', 'the', 'president?']
>>> key_index # starts with 0
0
CodePudding user response:
Rather than for word in words
, consider using the built-in enumerate
(doc) function to make your iterator. This will allow you to reference both the word
(to see if it is a key
) but also its position within the list. By knowing its position, you can also reference words in positions relative to it.