-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wiki.py
47 lines (36 loc) · 1.41 KB
/
Wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#Written by Jake Schultz
#Pathed by Alexander Pateenok
#TODO Add more lang support, limit number of results returned
import re
from urllib2 import Request, urlopen, URLError
import json
WORDS = ["WIKI", "WICKY","ARTICLE"]
PRIORITY = 1
def handle(text, mic, profile):
# method to get the wiki summary
get_wiki(text,mic)
def get_wiki(text,mic):
mic.say("What would you like to learn about?")
# get the user voice input as string
article_title = mic.activeListen()
# make a call to the Wikipedia API
request = Request('https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles='+article_title)
try:
response = urlopen(request)
data = json.load(response)
# Parse the JSON to just get the extract. Always get the first summary.
output = data["query"]["pages"]
final = output[output.keys()[0]]["extract"]
final = re.sub(r'\([^\(]*\)','', final).split('.')[0]
mic.say(final)
except URLError, e:
mic.say("Unable to reach dictionary API.")
def isValid(text):
wiki= bool(re.search(r'\bWiki\b',text, re.IGNORECASE))
# Add 'Wicky' because the STT engine recognizes it quite often
wicky= bool(re.search(r'\bwicky\b',text, re.IGNORECASE))
article= bool(re.search(r'\barticle\b',text, re.IGNORECASE))
if wicky or wiki or article:
return True
else:
return False