Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBot/wiki.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
34 lines (32 sloc)
1.6 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import textwrap #package that allows to choose lenght of the lines that are printed -> font:https://stackoverflow.com/questions/16430200/a-good-way-to-make-long-strings-wrap-to-newline-in-python | |
import wikipedia #package that allows search in wikipedia database to get information of certain topic -> font:https://pypi.org/project/wikipedia/ | |
""" defines the key word and searches in wikipedia for the meaning """ | |
def wikiSearch(sentence): | |
totalSize = len(sentence) | |
word = "" | |
for i in range(totalSize): | |
if "what is" in sentence: | |
if i <= 7: | |
continue | |
else: | |
word = word + sentence[i] | |
if "wiki" in sentence: | |
if i <= 4: | |
continue | |
else: | |
word = word + sentence[i] | |
if "who is" in sentence: | |
if i <= 6: | |
continue | |
else: | |
word = word + sentence[i] | |
try: | |
meaning = (wikipedia.summary(str(word), sentences=2)) #gets the first 2 sentences of wikipedia when searching for meaning of word | |
fullMeaning = (wikipedia.summary(str(word), sentences=20)) #gets the first 20 sentences of wikipedia when searching for meaning of word | |
print(textwrap.fill(meaning, 80)) #formats the lenght of the lines that are printed | |
answer = input("\nDo you want to know more about this?\nYes or No: ") | |
answer = answer.lower() | |
if "yes" in answer or "y" in answer: | |
print(textwrap.fill(fullMeaning, 120)) | |
except: | |
print("Sorry, I do not understand") |