Skip to content

Commit

Permalink
wikipedia_for_humans
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Dec 5, 2020
1 parent 12d5833 commit bab01db
Show file tree
Hide file tree
Showing 66 changed files with 123 additions and 360 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Wikipedia
## About
Query [Wikipedia](https://www.wikipedia.org) for answers to all your questions. Get just a summary, or ask for more to get in-depth information.

This Skill uses the [Wikimedia API](https://en.wikipedia.org/w/api.php).
This Skill uses the [Wikipedia for humans](https://github.com/HelloChatterbox/wikipedia_for_humans).


## Examples
Expand All @@ -17,8 +17,6 @@ Query [Wikipedia](https://www.wikipedia.org) for answers to all your questions.
* "More information" (followup after an initial summary)
* "Tell me More" (followup after an initial summary)

## Credits
Mycroft AI (@MycroftAI)

## Category
**Information**
Expand Down
184 changes: 39 additions & 145 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2017, Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -11,42 +9,30 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import wikipedia as wiki
import wikipedia_for_humans
from requests.exceptions import ConnectionError
from adapt.intent import IntentBuilder


from mycroft.skills.core import (MycroftSkill, intent_handler,
intent_file_handler)


EXCLUDED_IMAGES = [
'https://upload.wikimedia.org/wikipedia/commons/7/73/Blue_pencil.svg'
]


def wiki_image(pagetext):
""" Fetch first best image from results.
Arguments:
pagetext: wikipedia result page
Returns:
(str) image url or empty string if no image available
"""
images = [i for i in pagetext.images if i not in EXCLUDED_IMAGES]
if len(images) > 0:
return images[0]
else:
return ''


class WikipediaSkill(MycroftSkill):
def __init__(self):
super(WikipediaSkill, self).__init__(name="WikipediaSkill")
self.idx = 0
self.results = []

@intent_handler(IntentBuilder("").require("Wikipedia").
def speak_result(self):
if self.idx + 1 > len(self.results):
self.speak_dialog("thats all")
self.remove_context("wiki_article")
self.idx = 0
else:
self.speak(self.results[self.idx])
self.idx += 1
self.set_context("Wikipedia", "wikipedia")

@intent_handler(IntentBuilder("WikiSearch").require("Wikipedia").
require("ArticleTitle"))
def handle_wiki_query(self, message):
""" Extract what the user asked about and reply with info
Expand All @@ -55,129 +41,37 @@ def handle_wiki_query(self, message):
# Talk to the user, as this can take a little time...
search = message.data.get("ArticleTitle")
self.speak_dialog("searching", {"query": search})

if "lang" in self.settings:
lang = self.settings["lang"]
else:
lang = self.lang.split("-")[0]
try:
self._lookup(search)
except wiki.PageError:
self._lookup(search, auto_suggest=False)
except Exception as e:
self.log.error("Error: {0}".format(e))

@intent_handler(IntentBuilder("").require("More").
require("wiki_article").require("spoken_lines"))
answer = wikipedia_for_humans.summary(search, lang=lang)
if not answer.strip():
self.speak_dialog("no entry found")
return
self.log.debug("Wiki summary: " + answer)
self.idx = 0
self.results = answer.split(". ")
self.speak_result()
self.set_context("wiki_article", search)
except ConnectionError as e:
self.log.error("It seems like lang is invalid!!!")
self.log.error(lang + ".wikipedia.org does not seem to exist")
self.log.info("Override this in skill settings")
# TODO dialog
# TODO Settings meta
raise e # just speak regular error

@intent_handler(IntentBuilder("WikiMore").require("More").
require("wiki_article"))
def handle_tell_more(self, message):
""" Follow up query handler, "tell me more".
If a "spoken_lines" entry exists in the active contexts
this can be triggered.
"""
# Read more of the last article queried
results = self.results
article = message.data.get("wiki_article")
lines_spoken_already = int(message.data.get("spoken_lines"))

summary_read = wiki.summary(article, lines_spoken_already)
try:
summary = wiki.summary(article, lines_spoken_already + 5)
except wiki.PageError:
summary = wiki.summary(article, lines_spoken_already + 5,
auto_suggest=False)

# Remove already-spoken parts and section titles
summary = summary[len(summary_read):]
summary = re.sub(r'\([^)]*\)|/[^/]*/|== [^=]+ ==', '', summary)

if not summary:
self.speak_dialog("thats all")
else:
self.gui.clear()

try:
pagetext = wiki.page(results[0])
except wiki.PageError:
pagetext = wiki.page(results[0], auto_suggest=False)

self.gui['summary'] = summary
self.gui['imgLink'] = wiki_image(pagetext)
self.gui.show_page("WikipediaDelegate.qml", override_idle=60)
self.speak(summary)
self.set_context("wiki_article", article)
self.set_context("spoken_lines", str(lines_spoken_already+5))

@intent_file_handler("Random.intent")
def handle_random_intent(self, message):
""" Get a random wiki page.
Uses the Special:Random page of wikipedia
"""
# Talk to the user, as this can take a little time...
search = wiki.random(pages=1)
self.speak_dialog("searching", {"query": search})
self._lookup(search)

def _lookup(self, search, auto_suggest=True):
""" Performs a wikipedia lookup and replies to the user.
Arguments:
search: phrase to search for
"""
try:
# Use the version of Wikipedia appropriate to the request language
dict = self.translate_namedvalues("wikipedia_lang")
wiki.set_lang(dict["code"])

# First step is to get wiki article titles. This comes back
# as a list. I.e. "beans" returns ['beans',
# 'Beans, Beans the Music Fruit', 'Phaseolus vulgaris',
# 'Baked beans', 'Navy beans']
results = wiki.search(search, 5)
if len(results) == 0:
self.speak_dialog("no entry found")
return

# Now request the summary for the first (best) match. Wikipedia
# writes in inverted-pyramid style, so the first sentence is the
# most important, the second less important, etc. Two sentences
# is all we ever need.
lines = 2
summary = wiki.summary(results[0], lines,
auto_suggest=auto_suggest)

if "==" in summary or len(summary) > 250:
# We hit the end of the article summary or hit a really long
# one. Reduce to first line.
lines = 1
summary = wiki.summary(results[0], lines,
auto_suggest=auto_suggest)

# Now clean up the text and for speaking. Remove words between
# parenthesis and brackets. Wikipedia often includes birthdates
# in the article title, which breaks up the text badly.
summary = re.sub(r'\([^)]*\)|/[^/]*/', '', summary)

# Send to generate displays
self.gui.clear()
pagetext = wiki.page(results[0], auto_suggest=auto_suggest)
self.gui['summary'] = summary
self.gui['imgLink'] = wiki_image(pagetext)
self.gui.show_page("WikipediaDelegate.qml", override_idle=60)

# Remember context and speak results
self.set_context("wiki_article", results[0])
self.set_context("spoken_lines", str(lines))
self.speak(summary)
self.results = results

except wiki.exceptions.DisambiguationError as e:
# Test: "tell me about john"
options = e.options[:5]

option_list = (", ".join(options[:-1]) + " " +
self.translate("or") + " " + options[-1])
choice = self.get_response('disambiguate',
data={"options": option_list})
if choice:
self._lookup(choice, auto_suggest=auto_suggest)
self.speak_result()


def create_skill():
Expand Down
2 changes: 2 additions & 0 deletions dialog/ca-es/disambiguate.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
volíeu dir {{options}}
us agradaria sentir alguna cosa sobre {{options}}
2 changes: 2 additions & 0 deletions dialog/ca-es/no entry found.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
No puc trobar cap article relacionat en la Viquipèdia
Em temo que no hi ha cap article sobre això
1 change: 1 addition & 0 deletions dialog/ca-es/or.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
o
3 changes: 3 additions & 0 deletions dialog/ca-es/searching.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Espereu un moment mentre cerco {{query}}
Permeteu-me cercar sobre {{query}}
Estic cercant a la Viquipèdia sobre {{query}}
2 changes: 2 additions & 0 deletions dialog/ca-es/thats all.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Això és tot el que en sé
Això és tot el que n'he trobat
2 changes: 2 additions & 0 deletions dialog/ca-es/wikipedia_lang.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Codi de llengua de Viquipèdia usat per a fer consultes en la llengua activa
code,ca
2 changes: 2 additions & 0 deletions dialog/gl-es/disambiguate.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quixeches dicir {{options}}
gustaríache oír sobre {{options}}
2 changes: 2 additions & 0 deletions dialog/gl-es/no entry found.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Non consigo atopar un artigo de Wikipedia relacionado
Temo que non vai haber ningún artigo sobre isto
1 change: 1 addition & 0 deletions dialog/gl-es/or.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ou
3 changes: 3 additions & 0 deletions dialog/gl-es/searching.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Espero un momento mentres procuro {{query}}
Déixame procurar {{query}}
Estou buscando na Wikipedia {{query}}
2 changes: 2 additions & 0 deletions dialog/gl-es/thats all.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Isto é todo o que sei
Isto é todo o que conseguín atopar
2 changes: 2 additions & 0 deletions dialog/gl-es/wikipedia_lang.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Código da linguaxe de Wikipedia usado para atender consultas no idioma activo
code,gl
2 changes: 2 additions & 0 deletions dialog/pl-pl/disambiguate.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
czy chodziło Ci o {{options}}
czy chciałbyś usłyszeć o {{options}}
2 changes: 2 additions & 0 deletions dialog/pl-pl/no entry found.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Nie mogę znaleźć powiązanego artykułu na Wikipedii
Boję się, że nie ma żadnego artykułu na ten temat
1 change: 1 addition & 0 deletions dialog/pl-pl/or.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
albo
3 changes: 3 additions & 0 deletions dialog/pl-pl/searching.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Daj mi chwilę kiedy będę szukał {{query}}
Pozwól mi sprawdzić {{query}}
Szukam w Wikipedii o {{query}}
2 changes: 2 additions & 0 deletions dialog/pl-pl/thats all.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To wszystko co wiem
To wszystko co znalazłem
2 changes: 2 additions & 0 deletions dialog/pl-pl/wikipedia_lang.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Kod językowy, który Wikipedia stosuje do obsługi zapytań w wybranym języku
code,pl
2 changes: 2 additions & 0 deletions dialog/pt-pt/disambiguate.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
você quis dizer {{options}}
você gostaria de ouvir sobre {{options}}
2 changes: 2 additions & 0 deletions dialog/pt-pt/no entry found.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Não consigo encontrar um artigo da Wikipedia relacionado
Não encontro nenhum artigo sobre isso
1 change: 1 addition & 0 deletions dialog/pt-pt/or.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ou
3 changes: 3 additions & 0 deletions dialog/pt-pt/searching.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Só um momento enquanto eu procuro {{query}}
Deixe-me procurar {{query}}
Estou checando a Wikipedia por {{query}}
2 changes: 2 additions & 0 deletions dialog/pt-pt/thats all.dialog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Isto é tudo que sei
Isto é tudo que consegui encontrar
2 changes: 2 additions & 0 deletions dialog/pt-pt/wikipedia_lang.value
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Código da linguagem do Wikipedia usado para atender consultas no idioma ativo
code,pt-pt
1 change: 0 additions & 1 deletion regex/da-dk/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/de-de/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/el-gr/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/en-us/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/es-es/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/es-lm/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/fr-fr/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/it-it/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/nl-nl/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/pt-br/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/ro-ro/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/ru-ru/article.title.rx

This file was deleted.

1 change: 0 additions & 1 deletion regex/sv-se/article.title.rx

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wikipedia==1.4.0
wikipedia_for_humans
11 changes: 0 additions & 11 deletions test/behave/steps/wiki.py

This file was deleted.

Loading

0 comments on commit bab01db

Please sign in to comment.