From b0990f7ea1435c35c5091457ad2ee4e946eac304 Mon Sep 17 00:00:00 2001 From: Mathieu B Date: Fri, 30 Jul 2021 21:13:06 +0200 Subject: [PATCH] Wikipedia skill now working (added translate module aswell) --- jarvis/skills/research/wikipedia/__init__.py | 9 ++- jarvis/skills/research/wikipedia/wikipedia.py | 56 +++++++++++++++++++ requirements.txt | 4 +- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 jarvis/skills/research/wikipedia/wikipedia.py diff --git a/jarvis/skills/research/wikipedia/__init__.py b/jarvis/skills/research/wikipedia/__init__.py index 9c40026..fd1832c 100644 --- a/jarvis/skills/research/wikipedia/__init__.py +++ b/jarvis/skills/research/wikipedia/__init__.py @@ -1,5 +1,6 @@ from jarvis.skills import Skill, SkillRegistering from jarvis.skills.decorators import intent_file_handler +from jarvis.skills.research.wikipedia import wikipedia class WikipediaSkill(Skill, metaclass=SkillRegistering): @@ -8,8 +9,12 @@ class WikipediaSkill(Skill, metaclass=SkillRegistering): @intent_file_handler("search.wikipedia.intent", "WikipediaQueryIntent") def handle_wikipedia_query_intent(self, data): - print(data) - print("Handle Wikipedia Query Intent Method") + if 'query' in data: + # TODO : say somethink like "i'm searching..." + print("[INTENT RESULT] : " + wikipedia.page_summary(query=data['query'])) + else: + # TODO: fallback to duckduckgo or wolfram alpha + pass def create_skill(): diff --git a/jarvis/skills/research/wikipedia/wikipedia.py b/jarvis/skills/research/wikipedia/wikipedia.py new file mode 100644 index 0000000..43866b5 --- /dev/null +++ b/jarvis/skills/research/wikipedia/wikipedia.py @@ -0,0 +1,56 @@ +import re + +import wikipedia as wiki +import wikipedia.exceptions +from translate import Translator + +from jarvis.utils import languages_utils, fake_emails_generator + + +def page_summary(query, auto_suggest=True): + """Request the summary for the result. + 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. + Arguments: + wiki result (str): Wikipedia match name + auto_suggest (bool): True if auto suggest was used to get this + result. + """ + + print(query) + lines = 2 + wiki.set_lang(languages_utils.get_language_only_country()) + + # First try in local language + try: + summary = wiki.summary(title=query, sentences=lines, auto_suggest=auto_suggest) + except wikipedia.exceptions.WikipediaException: + try: + # Second try if nothing is found in the local language, try in english and translate the answer + summary = summary_english_translated(query) + except wikipedia.exceptions.WikipediaException: + return "Found nothing" + + 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. + summary = summary.split('. ')[0] + + # replace inc. by incorporation (for pronunciation) + summary = summary.replace("Inc.", "Incorporation") + + # Clean text to make it more speakable + return re.sub(r'\([^)]*\)|/[^/]*/', '', summary) + + +def summary_english_translated(query): + wiki.set_lang("en") + summary = wiki.page(title=query, auto_suggest=False).summary + summary = re.sub(r'\([^)]*\)|/[^/]*/', '', str(summary)) + summary = summary.split('. ')[0] + summary.split('. ')[1] + + translator = Translator(to_lang=languages_utils.get_language_only_country(), from_lang="en", + email=fake_emails_generator.generate_random_email()) + summary = translator.translate(summary) + return summary diff --git a/requirements.txt b/requirements.txt index 7f2b258..5da5c38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,6 @@ adapt-parser~=0.5.1 padatious~=0.4.8 spacy~=3.1.1 lingua-franca~=0.4.2 -spotipy~=2.18.0 \ No newline at end of file +spotipy~=2.18.0 +translate~=3.6.1 +wikipedia~=1.4.0 \ No newline at end of file