├── Description ├── Images ├── block_diagram_qas.png └── screenshot_of_qas.png ├── README.md ├── app.py ├── give_answer.py ├── requirements.txt └── templates ├── README.md ├── answer.html └── index.html /Description/Images: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Description/block_diagram_qas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/upasana05ghosh/Question-Answering-System/c1be473f2af3c507ff19d9e7b3433c1992e489d2/Description/block_diagram_qas.png -------------------------------------------------------------------------------- /Description/screenshot_of_qas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/upasana05ghosh/Question-Answering-System/c1be473f2af3c507ff19d9e7b3433c1992e489d2/Description/screenshot_of_qas.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Question-Answering-System (QAS): 2 | ### Application link: [wikianswers.herokuapp.com](http://wikianswers.herokuapp.com) 3 | A factoid based question answering system 4 | 5 | ![Screenshot of system](https://github.com/Upa005/Question-Answering-System/blob/master/Description/screenshot_of_qas.png) 6 | 7 | QAS is a system that automatically answer questions posed by humans in natural language query. Natural language (e.g. English) is the common way of sharing knowledge. 8 | 9 | ## Characteristics of the factoid based QAS: 10 | The Question Answering System (QAS): 11 | * tries to answer factoid based questions. 12 | * provides concise facts about the question. 13 | For example, "where is Taj Mahal located?", “Who is the father of nation of India?”. 14 | * is a web-based QAS. 15 | * answer open-domain fact based questions. 16 | * uses wikipedia and google search pages to extract concise answers. 17 | 18 | ## Block diagram of the system: 19 | ![Block diagram of system](https://github.com/Upa005/Question-Answering-System/blob/master/Description/block_diagram_qas.png) 20 | 21 | ## Setup Instructions 22 | 23 | * Clone the source 24 | ``` 25 | git clone https://github.com/Upa005/Question-Answering-System.git 26 | ``` 27 | 28 | * Requirements
29 | Python version : 2.7
30 | Operating System: Windows 31 | 32 | 33 | * Python Packages required
34 | 35 | nltk
36 | flask
37 | gunicorn
38 | unidecode
39 | wolframalpha
40 | wikipedia
41 | gevent
42 | flask_bootstrap
43 | flask_appconfig
44 | flask_wtf
45 | wtforms
46 | google
47 | 48 | 49 | 50 | * Generate Wolframalpha API Key (You can use our to test things) 51 | 52 | 1. Visit the [Wolframalpha APIs Console](https://products.wolframalpha.com/api/) and log in with your Wolframalpha account. 53 | 54 | 2. In the API admin page, you will get your API Key. 55 | 56 | 3. Open Question-Answering-System/app.py program. 57 | Search this sentence in the program. (At line number 28) 58 | ``` 59 | app.config['SECRET_KEY']= ## insert your secret key 60 | ``` 61 | Now insert your secret key after '=' sign 62 | ``` 63 | Example: app.config['SECRET_KEY']= '\\ffedg0890489574' 64 | 65 | ``` 66 | 4. Save the file. 67 | 68 | 69 | ## To Run 70 | 1. Install python 2.7 to your system 71 | 2. Install the packages givn in the requirements.txt file 72 | 3. Open command prompt. 73 | 4. Go to the directory in command prompt. 74 | 5. Type in command prompt: 75 | ``` 76 | py -2 app.py 77 | ``` 78 | 6. Now run the browser and type localhost:9191/ 79 | 7. Input your question. (Make sure, you are connected to Internet) 80 | 8. Wait for the answer 81 | 82 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request 2 | from flask_bootstrap import Bootstrap 3 | from flask_appconfig import AppConfig 4 | from flask_wtf import Form, RecaptchaField 5 | from wtforms import TextField, HiddenField, ValidationError, RadioField, BooleanField, SubmitField 6 | from wtforms.validators import Required 7 | import re 8 | import gevent.monkey 9 | from gevent.pywsgi import WSGIServer 10 | gevent.monkey.patch_all() 11 | 12 | from give_answer import answer_question 13 | import unicodedata 14 | import wolframalpha 15 | import wikipedia 16 | 17 | class ExampleForm(Form): 18 | question = TextField('', description='', validators=[Required()]) 19 | submit_button = SubmitField('Go') 20 | 21 | 22 | def create_app(configfile=None): 23 | app = Flask(__name__) 24 | AppConfig(app, configfile) 25 | Bootstrap(app) 26 | 27 | 28 | app.config['SECRET_KEY']= ## insert your secret key 29 | 30 | 31 | @app.route('/', methods=('GET', 'POST')) 32 | def index(): 33 | if request.method == 'POST': 34 | try: 35 | question = request.form['question'] 36 | except KeyError, e: 37 | print 'key eroor' 38 | print 'I got a KeyError - reason "%s"' % str(e) 39 | except: 40 | print 'I got another exception, but I should re-raise' 41 | raise 42 | 43 | 44 | print(question) 45 | answer = answer_question(question) 46 | print 'answer: ',answer 47 | answer=re.sub('([(].*?[)])',"",answer) 48 | 49 | return render_template('answer.html', answer=answer, question=question) 50 | 51 | form = ExampleForm() 52 | return render_template('index.html', form=form) 53 | 54 | 55 | 56 | return app 57 | 58 | # create main callable 59 | app = create_app() 60 | 61 | if __name__ == '__main__': 62 | http_server = WSGIServer(('127.0.0.1', 9191), app) 63 | print("starting server on port 9191") 64 | http_server.serve_forever() 65 | 66 | -------------------------------------------------------------------------------- /give_answer.py: -------------------------------------------------------------------------------- 1 | import unicodedata 2 | import wolframalpha 3 | from nltk import word_tokenize, pos_tag, ne_chunk, conlltags2tree, tree2conlltags 4 | from google import google 5 | import wikipedia 6 | import collections 7 | 8 | #===============================================================#================================================================ 9 | # Determine the type of question i.e location, date, person ,definiton 10 | def classify_question(question): 11 | q = question.lower().split() 12 | if q[0] == 'where': 13 | return 'Location' 14 | elif 'year' in question: 15 | return 'Date' 16 | elif 'country' in question: 17 | return 'Country' 18 | elif q[0] == 'who': 19 | return 'Person' 20 | elif q[0] == 'what': 21 | return 'Definition' 22 | else: 23 | 24 | return 'None' 25 | 26 | #=============================================================== 27 | def google_search(question): 28 | first_page = google.search(question,1) 29 | #print first_page 30 | top_three_result = [] 31 | i = 0 32 | while i<5: 33 | top_three_result.append(first_page[i].description) 34 | i+=1 35 | 36 | first_search = ''.join(top_three_result).encode('ascii','replace') 37 | 38 | ne_tree = (ne_chunk(pos_tag(word_tokenize(first_search)))) 39 | 40 | iob_tagged = tree2conlltags(ne_tree) 41 | 42 | ss = [tuple(map(str,eachTuple)) for eachTuple in iob_tagged] 43 | question_type = classify_question(question) 44 | print 'question_type: ',question_type 45 | if question_type == 'None': 46 | ans = "Oops! I don't know." 47 | else: 48 | google_answer = [] 49 | if question_type == 'Person': 50 | for i in range(len(ss)): 51 | if ss[i][2] == 'B-PERSON'or ss[i][2] == 'I-PERSON': 52 | google_answer.append(ss[i][0]) 53 | elif question_type == 'Country': 54 | print 'country identified' 55 | for i in range(len(ss)): 56 | if ss[i][2] == 'B-GPE'or ss[i][2] == 'I-GPE': 57 | google_answer.append(ss[i][0]) 58 | elif question_type == 'Location': 59 | for i in range(len(ss)): 60 | if ss[i][2] == 'B-LOCATION'or ss[i][2] == 'I-LOCATION': 61 | google_answer.append(ss[i][0]) 62 | elif question_type == 'Date': 63 | for i in range(len(ss)): 64 | if ss[i][2] == 'B-DATE'or ss[i][2] == 'I-DATE': 65 | google_answer.append(ss[i][0]) 66 | print 'google: ',google_answer 67 | if not google_answer: 68 | ans = "Oops, I don't know! " 69 | else: 70 | print 'inside else' 71 | counts = collections.Counter(google_answer) 72 | print 'counts: ',counts 73 | t = counts.most_common(4) 74 | candidate_answer = [ seq[0] for seq in t ] 75 | print candidate_answer 76 | for i in range(len(candidate_answer)): 77 | candidate_answer[i] = 'Candidate Answer '+ str(i+1)+' '+ candidate_answer[i] 78 | candidate_answer = '\n'.join(candidate_answer) 79 | ans = candidate_answer 80 | return ans 81 | ################################################################################## 82 | 83 | def wiki_search(question): 84 | l = question.split(' ') 85 | if len(l) > 2: 86 | ques = " ".join(l[2:]) 87 | try: 88 | print 'inside wiki search' 89 | ans = (wikipedia.summary(question, sentences=1)).encode('ascii', 'ignore') 90 | link = wikipedia.page(ques) 91 | ans = ans + '\n For more information: '+link.url 92 | except: 93 | print 'wiki_search_failed_google' 94 | google_search(question) 95 | return ans 96 | 97 | def answer_question(question): 98 | try: 99 | app_id = '' # add your app id into this 100 | if not app_id: 101 | print 'Add your app id in line no. 110' 102 | client = wolframalpha.Client(app_id) 103 | res = client.query(question) 104 | ans = str(next(res.results).text).replace('.', '.\n') 105 | 106 | if ans == 'None': 107 | print 'ans is none' 108 | q_type = classify_question(question) 109 | if q_type == 'Definition' or q_type == 'Location': 110 | print 'except-wiki' 111 | ans = wiki_search(question) 112 | else: 113 | print 'none-google' 114 | ans = google_search(question) 115 | print 'google answ: ',ans 116 | 117 | return ans 118 | 119 | except: 120 | try: 121 | print 'Exception at first run' 122 | q_type = classify_question(question) 123 | if q_type == 'Definition' or q_type == 'Location': 124 | print 'except-wiki' 125 | ans = wiki_search(question) 126 | else: 127 | print 'except-google' 128 | ans = google_search(question) 129 | print 'google answ: ',ans 130 | return ans 131 | except: 132 | return "Oops! I don't know. Try something else" 133 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | nltk 2 | flask 3 | gunicorn 4 | unidecode 5 | wolframalpha 6 | wikipedia 7 | gevent 8 | flask_bootstrap 9 | flask_appconfig 10 | flask_wtf 11 | wtforms 12 | google -------------------------------------------------------------------------------- /templates/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /templates/answer.html: -------------------------------------------------------------------------------- 1 | {% extends "bootstrap/base.html" %} 2 | {% import "bootstrap/wtf.html" as wtf %} 3 | {% import "bootstrap/fixes.html" as fixes %} 4 | 5 | {% block title %}Inquire{% endblock %} 6 | 7 | {% block content %} 8 |
9 |

Question Answering System

10 |
11 | 12 |
13 | 14 |

Question

15 |
16 | {{ question }} 17 |
18 |

Answer

19 |
20 | 21 |
22 |

23 | {{ answer }} 24 |

25 |
26 | 27 |
28 |
29 | 30 | Have another question 31 |
32 |
33 | {% endblock %} 34 | 35 | {% block head %} 36 | {{super()}} 37 | {{fixes.ie8()}} 38 | {% endblock %} 39 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "bootstrap/base.html" %} 2 | {% import "bootstrap/wtf.html" as wtf %} 3 | {% import "bootstrap/fixes.html" as fixes %} 4 | 5 | {% block title %}QAS{% endblock %} 6 | 7 | {% block content %} 8 |
9 | 10 |

Factoid based Question Answering System

11 |
12 |
13 |
14 |
15 |

What's your Question?

16 |
17 | {{ wtf.quick_form(form, button_map={'submit_button': 'primary'}) }} 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 |

Geography

Science

General

  • Where is Luanda?
  • Why is the sky blue?
  • What instrument does Max Roach play?
  • What is the largest city in Florida?
  • When was Einstein born?
  • Who directed Gone with the Wind?
  • How many people live in Israel?
  • Who invented the telegraph?
  • When was Beethoven born?
  • What is the capital of Greenland?
  • What is TCP/IP?
  • Who wrote the Gift of the Magi?
  • What are tannis?
  • Who invented the telegraph?
  • Who directed The Birds?
  • Which is the longest river in the world?
  • What does obfuscate mean?
  • Who wrote the music to Star Wars?
  • Where is louvre museum located?
  • What is the state flower of Montana?
  • How many people live on earth?
  • 69 |
    70 | 71 | 72 | 73 | 74 | {% endblock %} 75 | 76 | {% block head %} 77 | {{super()}} 78 | {{fixes.ie8()}} 79 | {% endblock %} 80 | 81 | 82 | --------------------------------------------------------------------------------