Skip to content

Commit

Permalink
add search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Singhak committed Mar 4, 2018
1 parent dc0e6c3 commit 8f78558
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Alien a Desktop Assistant
# Alien a Desktop Assistant

### Note
It uses the
Expand Down Expand Up @@ -29,10 +29,11 @@ It uses the
- Say or type `when <festivalname>` Or `festival <festivalname>`
- [x] <b>Search product on Flipkart or Amazon</b>
- Say or type `flipkart <productname>` Or `amazon <productname>`
- [x] <b>Say any thing for search on wikki or google
- Say or type `<anything>`

### Upcoming Feature
- Local search
- Google search
- Open local files
- And many more

Expand Down
Binary file added __pycache__/search.cpython-36.pyc
Binary file not shown.
Binary file added __pycache__/searchQuery.cpython-36.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions alienUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import webbrowser
import speech as s
import weatherForcast as wf
import searchQuery as sq

speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak('Whokum maire aakaa, Alien at your service.')
Expand Down Expand Up @@ -78,6 +79,13 @@ def startSevices(self, event):
if weather is not None:
print(weather)
speak.Speak(weather)
else:
speak.Speak('Searching for ' + query_text)
whois = sq.search(query_text)
print(whois)
speak.Speak(whois)



# Trigger GUI
if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion requirement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ feedparser
BeautifulSoup
wxpython
requests
wikipedia
wikipedia
wolframalpha
google-search
65 changes: 65 additions & 0 deletions searchQuery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import wolframalpha
import wikipedia
import requests
import warnings

warnings.filterwarnings('ignore')
appId = 'APER4E-58XJGHAVAK'
client = wolframalpha.Client(appId)

# method that search wikipedia...
def search_wiki(keyword=''):
# running the query
searchResults = wikipedia.search(keyword)
# If there is no result, print no result
if not searchResults:
return "No result from Wikipedia"
# Search for page... try block
try:
page = wikipedia.page(searchResults[0])
except wikipedia.DisambiguationError as err:
page = wikipedia.page(err.options[0])

wikiTitle = str(page.title.encode('utf-8'))
wikiSummary = str(page.summary)
return "Result from wikipedia:\n"+wikiSummary


def search(text=''):
res = client.query(text)
# Wolfram cannot resolve the question
if res['@success'] == 'false':
return 'Question cannot be resolved'
# Wolfram was able to resolve question
else:
result = ''
# pod[0] is the question
pod0 = res['pod'][0]
# pod[1] may contains the answer
pod1 = res['pod'][1]
# checking if pod1 has primary=true or title=result|definition
if (('definition' in pod1['@title'].lower()) or ('result' in pod1['@title'].lower()) or (pod1.get('@primary','false') == 'true')):
# extracting result from pod1
result = resolveListOrDict(pod1['subpod'])
return "Result from wolframalpha:\n"+result
else:
# extracting wolfram question interpretation from pod0
question = resolveListOrDict(pod0['subpod'])
# removing unnecessary parenthesis
question = removeBrackets(question)
# searching for response from wikipedia
return search_wiki(question)


def removeBrackets(variable):
return variable.split('(')[0]

def resolveListOrDict(variable):
if isinstance(variable, list):
return variable[0]['plaintext']
else:
return variable['plaintext']

#####
# Code credit goes to https://github.com/salisuwy/Python-AI-Assistant/blob/master/main.py
####

0 comments on commit 8f78558

Please sign in to comment.