-
Notifications
You must be signed in to change notification settings - Fork 3
/
Duckduckgo.py
89 lines (77 loc) · 2.37 KB
/
Duckduckgo.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8-*-
import duckduckgo
import re
WORDS = ["DUCKDUCKGO", "SEARCH", "GOOGLE"]
#Word determine as useless in a search
useless_search_word = ["a", "and", "the"]
def getResults(question, mic):
result=duckduckgo.query(question)
flag=True
switch=False
count=1
count_topics=0
#Look at the best (hopefully) answer first
mic.say(duckduckgo.get_zci(question))
mic.say('Were you talking about this ?')
yesno=mic.activeListen()
if re.search('no',yesno):
if result.type=='exclusive':
mic.say(result.answer.text)
elif result.type!='nothing':
while flag:
try:
if(switch==False):
mic.say(result.related[count].text)
else:
mic.say(result.related[count].topics[count_topics].text)
mic.say('Were you talking about this ?')
yesno=mic.activeListen()
if re.search('no',yesno) and count<len(result.related)-1:
if switch==False:
count=count+1
else:
if(count_topics==len(result.related[count].topics)-1):
count_topics=0
count=count+1
else:
count_topics=count_topics+1
elif re.search('yes', yesno):
flag=False
mic.say('Glad I could help you')
else:
flag=False
mic.say("OK I'll stop")
#Switch to topics
except Exception:
switch=True
else :
#get the best results
mic.say("Sorry, sometime, I'm no good...")
else:
mic.say('Glad I could help you')
def handle(text, mic, profile):
"""
Search on duckduckgo for information
Arguments:
text -- user-input, typically transcribed speech
mic -- used to interact with the user (for both input and output)
profile -- contains information related to the user (e.g., phone
number)
"""
if re.search('what is',text):
#print re.split('what is', text)
#get last element
question=re.split('what is', text)[-1]
question=" ".join("" if s in useless_search_word else s for s in question.split())
else:
mic.say('What would like me to search ?')
question=mic.activeListen()
question=" ".join("" if s in useless_search_word else s for s in question.split())
getResults(question, mic)
def isValid(text):
"""
Returns True if the input is related to the meaning of life.
Arguments:
text -- user-input, typically transcribed speech
"""
return bool(re.search(r'\bduckduckgo|search|google\b', text, re.IGNORECASE))