├── LICENSE ├── README.md └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Salisu Wada 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-AI-Assistant 2 | Build an AI Assistant Wolfram Alpha and Wikipedia in Python 3 | 4 | Tutorial https://medium.com/@salisuwy/build-an-ai-assistant-with-wolfram-alpha-and-wikipedia-in-python-d9bc8ac838fe 5 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import wolframalpha 2 | import wikipedia 3 | import requests 4 | 5 | appId = 'APER4E-58XJGHAVAK' 6 | client = wolframalpha.Client(appId) 7 | 8 | # method that search wikipedia... 9 | def search_wiki(keyword=''): 10 | # running the query 11 | searchResults = wikipedia.search(keyword) 12 | # If there is no result, print no result 13 | if not searchResults: 14 | print("No result from Wikipedia") 15 | return 16 | # Search for page... try block 17 | try: 18 | page = wikipedia.page(searchResults[0]) 19 | except wikipedia.DisambiguationError, err: 20 | page = wikipedia.page(err.options[0]) 21 | 22 | wikiTitle = str(page.title.encode('utf-8')) 23 | wikiSummary = str(page.summary.encode('utf-8')) 24 | print(wikiSummary) 25 | 26 | 27 | def search(text=''): 28 | res = client.query(text) 29 | # Wolfram cannot resolve the question 30 | if res['@success'] == 'false': 31 | print('Question cannot be resolved') 32 | # Wolfram was able to resolve question 33 | else: 34 | result = '' 35 | # pod[0] is the question 36 | pod0 = res['pod'][0] 37 | # pod[1] may contains the answer 38 | pod1 = res['pod'][1] 39 | # checking if pod1 has primary=true or title=result|definition 40 | if (('definition' in pod1['@title'].lower()) or ('result' in pod1['@title'].lower()) or (pod1.get('@primary','false') == 'true')): 41 | # extracting result from pod1 42 | result = resolveListOrDict(pod1['subpod']) 43 | print(result) 44 | question = resolveListOrDict(pod0['subpod']) 45 | question = removeBrackets(question) 46 | primaryImage(question) 47 | else: 48 | # extracting wolfram question interpretation from pod0 49 | question = resolveListOrDict(pod0['subpod']) 50 | # removing unnecessary parenthesis 51 | question = removeBrackets(question) 52 | # searching for response from wikipedia 53 | search_wiki(question) 54 | primaryImage(question) 55 | 56 | 57 | def removeBrackets(variable): 58 | return variable.split('(')[0] 59 | 60 | def resolveListOrDict(variable): 61 | if isinstance(variable, list): 62 | return variable[0]['plaintext'] 63 | else: 64 | return variable['plaintext'] 65 | 66 | def primaryImage(title=''): 67 | url = 'http://en.wikipedia.org/w/api.php' 68 | data = {'action':'query', 'prop':'pageimages','format':'json','piprop':'original','titles':title} 69 | try: 70 | res = requests.get(url, params=data) 71 | key = res.json()['query']['pages'].keys()[0] 72 | imageUrl = res.json()['query']['pages'][key]['original']['source'] 73 | print(imageUrl) 74 | except Exception, err: 75 | print('Exception while finding image:= '+str(err)) 76 | 77 | 78 | while True: 79 | q = raw_input('Name: ') 80 | search(q) 81 | --------------------------------------------------------------------------------