def ask_wikipedia(self, definition):
'''
Ask Wikipedia for the definition.
:param definition:
:return:
'''
# TODO: this method should run in a separate process, asynchronously
is_exact = False
out = []
if not wikipedia:
return is_exact, out
page_titles = wikipedia.search(definition)
page = None
if page_titles:
for page_title in page_titles:
if page_title.lower() == definition:
try:
page = wikipedia.page(page_title)
is_exact = True
except DisambiguationError as ex:
out.append(Phrase().text('This can refer to a many things, such as {0}'.format(self.join_for_more(ex.options, limit=None))))
return is_exact, out
if not page and 'disambiguation' not in page_titles[0]:
try:
page = wikipedia.page(page_titles[0])
except Exception as ex:
out.append(Phrase().text(str(ex)))
if page and not out:
out.append(Phrase().text(page.content.split('==')[0]
.split('\n')[0]
.encode('utf-8', 'ignore')).pause(1))
return is_exact, out
评论列表
文章目录