├── .gitignore
├── requirements.txt
├── LICENSE.md
├── Readme.md
├── knowledge.pl
└── main.py
/.gitignore:
--------------------------------------------------------------------------------
1 | venv/
2 | .vscode/
3 | build/
4 | dist/
5 | __pycache__/
6 | **.spec
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | altgraph==0.17
2 | pyinstaller==5.13.1
3 | pyinstaller-hooks-contrib==2020.10
4 | pyswip==0.2.10
5 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Kamrul Hasan
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 |
2 |
3 |
4 |
5 |
6 | ## Knowledge Based QA System with Python and Prolog  
7 | A knowledge-based system (KBS) is a form of artificial intelligence (AI) that aims to capture the knowledge of human experts to support decision-making. Examples of knowledge-based systems include expert systems, which are so called because of their reliance on human expertise.
8 |
9 | This project is a simple implementation of a knowledge-based system. It is a simple ChatBot. There is a prolog file that defines the knowledge base. The questions are handled with python. The key idea is mapping different questions for the same answer. The simplest way is finding keywords in the question. Using the keywords, a query is performed on the prolog knowledge base. Then the query result is shown with some prefix and suffix as the answer.
10 |
11 | These types of chatbots can handle frequently asked questions in a system. For example, this program can be an extension of a university website that can handle general queries about the university.
12 |
13 | ### Build 
14 | ---
15 | Install prolog
16 | > `` $ sudo apt install swi-prolog ``
17 |
18 | To build and run the program, open terminal and change directory to the program directory. Then execute the following commands.
19 | > ``$ python -m vevn venv``
20 | > ``$ souce venv/bin/activate``
21 | > ``$ pip install -r requirements.txt``
22 | > ``$ python main.py``
23 |
24 | This program can be built and run on windows too. First install ``prolog``, ``pip``. Then run the commands in the commands in the ``cmd`` terminal to build and run the program.
25 |
26 | To create executable, run
27 | > ``$ pyinstaller main.py``
28 |
29 | Run the executable
30 | > ``./dist/main/main``
31 |
32 |
33 |
34 | ### LICENSE [](https://opensource.org/licenses/MIT)
35 | ***
36 | Copyright © 2020, [Kamrul Hasan](https://github.com/kmtusher97).
37 | Released under the [MIT License](LICENSE). [See more](https://github.com/kmtusher97/Knowledge-Based-QA-System-with-Python-and-Prolog/blob/master/LICENSE.md)
38 |
39 |
40 |
--------------------------------------------------------------------------------
/knowledge.pl:
--------------------------------------------------------------------------------
1 | name('jahangirnagar university').
2 | breif_description('Jahangirnagar University is a public university in Bangladesh, based in Savar Upazila, Dhaka. It is one of the top and only fully residential universities in Bangladesh. There are 34 departments, 4 institutes under six faculties.').
3 | location('jahangirnagar university stands on the west side of the Asian Highway, popularly known as the Dhaka-Aricha Road, and is 32 kilometres away from the capital').
4 | established('jahangirnagar university', '1970').
5 | first_vice_chancellor('Mafizuddin Ahmed (PhD in chemistry, Penn State)').
6 | current_vice_chancellor('Professor Doctor Farzan Islam').
7 | history('The university was established in 1970 by the Jahangirnagar Muslim University Ordinance of the government of Pakistan. Its first vice-chancellor, Mafizuddin Ahmed (PhD in chemistry, Penn State) took up office on 24 September 1970. The first group of students, a total of 150, were enrolled in four departments: Economics, Geography, Mathematics, and Statistics. Its formal inauguration was delayed until 12 January 1971, when the university was launched by Rear Admiral S. M. Ahsan, the chancellor.').
8 | area('697.56 acres which is 2.8 square kilometres').
9 | number_of_faculties('6').
10 | number_of_departments('34').
11 | number_of_institutes('4').
12 | faculty('faculty of mathematical and physical science').
13 | faculty('faculty of biological science').
14 | faculty('faculty of social science').
15 | faculty('faculty of arts and humanities').
16 | faculty('faculty of business studies').
17 | faculty('faculty of law').
18 |
19 | faculties('faculty of mathematical and physical science, faculty of biological science, faculty of social science, faculty of arts and humanities, faculty of business studies, faculty of law').
20 |
21 | departments('department of computer science and engineering, department of mathematics, department of physics, department of environmental science, department of chemistry, department of statistics, department of geological science, department of botany, department of zoology, department of biochemistry and molecular biology, department of microbiology, department of pharmacy, department of public health and informatics, department of biotechnology and genetic engineering, department of anthropology, department of economics, department of government and politics,\n\tdepartment of geography and environment, department of public administration, department of urban and regional planning, department of archaeology, department of bangla, department of drama and dramatics, department of english, department of fine arts, department of history, department of international relations, department of journalism and media studies, department of philosophy, department of accounting and information systems, department of finance and banking, department of marketing, department of management studies, department of accounting and information systems, department of finance and banking, department of marketing, department of management studies').
22 |
23 | departments_under_faculty('faculty of mathematical and physical science', 'department of computer science and engineering').
24 | departments_under_faculty('faculty of mathematical and physical science', 'department of mathematics').
25 | departments_under_faculty('faculty of mathematical and physical science', 'department of physics').
26 | departments_under_faculty('faculty of mathematical and physical science', 'department of environmental science').
27 | departments_under_faculty('faculty of mathematical and physical science', 'department of chemistry').
28 | departments_under_faculty('faculty of mathematical and physical science', 'department of statistics').
29 | departments_under_faculty('faculty of mathematical and physical science', 'department of geological science').
30 |
31 | departments_under_faculty('faculty of biological science', 'department of botany').
32 | departments_under_faculty('faculty of biological science', 'department of zoology').
33 | departments_under_faculty('faculty of biological science', 'department of biochemistry and molecular biology').
34 | departments_under_faculty('faculty of biological science', 'department of microbiology').
35 | departments_under_faculty('faculty of biological science', 'department of pharmacy').
36 | departments_under_faculty('faculty of biological science', 'department of public health and informatics').
37 | departments_under_faculty('faculty of biological science', 'department of biotechnology and genetic engineering').
38 |
39 | departments_under_faculty('faculty of social science', 'department of anthropology').
40 | departments_under_faculty('faculty of social science', 'department of economics').
41 | departments_under_faculty('faculty of social science', 'department of government and politics').
42 | departments_under_faculty('faculty of social science', 'department of geography and environment').
43 | departments_under_faculty('faculty of social science', 'department of public administration').
44 | departments_under_faculty('faculty of social science', 'department of urban and regional planning').
45 |
46 | departments_under_faculty('faculty of arts and humanities', 'department of archaeology').
47 | departments_under_faculty('faculty of arts and humanities', 'department of bangla').
48 | departments_under_faculty('faculty of arts and humanities', 'department of drama and dramatics').
49 | departments_under_faculty('faculty of arts and humanities', 'department of english').
50 | departments_under_faculty('faculty of arts and humanities', 'department of fine arts').
51 | departments_under_faculty('faculty of arts and humanities', 'department of history').
52 | departments_under_faculty('faculty of arts and humanities', 'department of international relations').
53 | departments_under_faculty('faculty of arts and humanities', 'department of journalism and media studies').
54 | departments_under_faculty('faculty of arts and humanities', 'department of philosophy').
55 |
56 | departments_under_faculty('faculty of business studies', 'department of accounting and information systems').
57 | departments_under_faculty('faculty of business studies', 'department of finance and banking').
58 | departments_under_faculty('faculty of business studies', 'department of marketing').
59 | departments_under_faculty('faculty of business studies', 'department of management studies').
60 |
61 | departments_under_faculty('faculty of law', 'department of law and justice').
62 |
63 | department('department of computer science and engineering').
64 |
65 | about_department_of_computer_science_and_engineering('the department of computer science and engineering of jahangirnagar university is one of the leading cse department in bangladesh. since its inception in 1991, the Department is playing a vital role in developing skilled ict professional and researchers. the department selects very good quality national and international students in each academic year.\n\tprofessor doctor mohammad imdadul islam the present chairman of this department').
66 |
67 | chairman_of_cse('department of computer science and engineering', 'professor doctor mohammad imdadul islam').
68 | developers('shamim imtiaz and kamrul hasan tusher').
69 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70 | introduction(X, Y) :-
71 | name(X),
72 | breif_description(Y).
73 | history(X, Y) :-
74 | name(X),
75 | history(Y).
76 | location(X, Y) :-
77 | name(X),
78 | location(Y).
79 | area(X, Y) :-
80 | name(X),
81 | area(Y).
82 | first_vice_chancellor(X, Y) :-
83 | name(X),
84 | first_vice_chancellor(Y).
85 | vice_chancellor(X, Y) :-
86 | name(X),
87 | current_vice_chancellor(Y).
88 | number_of_faculties(X, Y) :-
89 | name(X),
90 | number_of_faculties(Y).
91 | number_of_departments(X, Y) :-
92 | name(X),
93 | number_of_departments(Y).
94 | number_of_institutes(X, Y) :-
95 | name(X),
96 | number_of_institutes(Y).
97 |
98 | faculties(X, Y) :-
99 | name(X),
100 | faculties(Y).
101 | departments(X, Y) :-
102 | name(X),
103 | departments(Y).
104 | departments_under_faculty(X, Y, Z) :-
105 | name(X),
106 | faculty(Y),
107 | departments_under_faculty(Y, Z).
108 | about_department_of_computer_science_and_engineering(X, Y) :-
109 | name(X),
110 | about_department_of_computer_science_and_engineering(Y).
111 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | from pyswip import Prolog
2 | import time
3 | import json
4 |
5 |
6 | def prolog_query(query_string):
7 | prolog = Prolog()
8 | prolog.consult("knowledge.pl")
9 | results = []
10 | for res in prolog.query(query_string):
11 | results.append(res)
12 |
13 | return results
14 |
15 |
16 | def ask_question(query_string):
17 | answers = prolog_query(query_string)
18 | return answers
19 |
20 |
21 | def make_json(data):
22 | json_str = ""
23 | for c in data:
24 | if c == "'":
25 | json_str += '"'
26 | continue
27 | json_str += c
28 | return json_str
29 |
30 |
31 | def say_answers(prefix, suffix, question_i, answers_i):
32 | for ansi in answers_i:
33 | ansi = make_json(str(ansi))
34 | obj = json.loads(str(ansi))
35 | print(obj[question_i])
36 | text = prefix + " " + obj[question_i] + " " + suffix
37 | print(">>>> ", text)
38 |
39 |
40 | print(
41 | "Hi, I'm here to tell you about jahangirnagar university. \
42 | what do you want to know about jahangirnagar university?"
43 | )
44 | flg = True
45 | while flg:
46 | # Q/A
47 | print("\n\n")
48 | asked_question = str(input("what is in your mind: ")).lower()
49 |
50 | if (
51 | "name of the university" in asked_question
52 | or "university name" in asked_question
53 | ):
54 | # Q: what is the name of the university?
55 | question = "UniversityName"
56 | query = "name(" + question + ")."
57 | answers = ask_question(query)
58 | say_answers("The name of the university ", "", question, answers)
59 |
60 | elif (
61 | "introduction" in asked_question
62 | or "about ju" in asked_question
63 | or "about jahangirnagar university" in asked_question
64 | ):
65 | # Q: what is jahangirnagar university?
66 | question = "Introduction"
67 | query = "introduction('jahangirnagar university', " + question + ")."
68 | answers = ask_question(query)
69 | say_answers("", "", question, answers)
70 |
71 | elif (
72 | "history of ju" in asked_question
73 | or "history of jahangirnagar university" in asked_question
74 | ):
75 | # Q: history of jahangirnagar university.
76 | question = "History"
77 | query = "history('jahangirnagar university', " + question + ")."
78 | answers = ask_question(query)
79 | say_answers("Brief history: ", "", question, answers)
80 |
81 | elif (
82 | "location of jahangirnagar university" in asked_question
83 | or "situated" in asked_question
84 | ):
85 | # Q: where is jahangirnagar university?
86 | question = "Loction"
87 | query = "location('jahangirnagar university', " + question + ")."
88 | answers = ask_question(query)
89 | say_answers("", "", question, answers)
90 |
91 | elif "area of jahangirnagar university" in asked_question:
92 | # Q: where is jahangirnagar university?
93 | question = "Area"
94 | query = "area('jahangirnagar university', " + question + ")."
95 | answers = ask_question(query)
96 | say_answers(
97 | "total area of jahangirnagar university is about ", "", question, answers
98 | )
99 |
100 | elif (
101 | "current" in asked_question
102 | or "present" in asked_question
103 | or "now" in asked_question
104 | ) and ("vice chancellor" in asked_question or "vc" in asked_question):
105 | # Q: who is the current vice_chancellor of jahangirnagar university?
106 | question = "Vice_chancellor"
107 | query = "vice_chancellor('jahangirnagar university', " + question + ")."
108 | answers = ask_question(query)
109 | say_answers(
110 | "The current vice chancellor of jahangirnagar university is ",
111 | "",
112 | question,
113 | answers,
114 | )
115 |
116 | elif (
117 | "number of faculties" in asked_question
118 | or "how many faculties" in asked_question
119 | and asked_question.find("faculty of") == -1
120 | ):
121 | # Q how many faculties are in jahangirnagr university
122 | question = "Number_of_faculties"
123 | query = "number_of_faculties('jahangirnagar university', " + question + ")."
124 | answers = ask_question(query)
125 | say_answers(
126 | "There are ", "faculties in jahangirnagar university", question, answers
127 | )
128 |
129 | elif (
130 | "number of departments" in asked_question
131 | or "how many departments" in asked_question
132 | ):
133 | # Q how many departments are in jahangirnagr university
134 | question = "Number_of_departments"
135 | query = "number_of_departments('jahangirnagar university', " + question + ")."
136 | answers = ask_question(query)
137 | say_answers(
138 | "There are ", "departments in jahangirnagar university", question, answers
139 | )
140 |
141 | elif (
142 | "number of institutes" in asked_question
143 | or "how many institutes" in asked_question
144 | ):
145 | # Q how many institutes are in jahangirnagr university
146 | question = "Number_of_institutes"
147 | query = "number_of_institutes('jahangirnagar university', " + question + ")."
148 | answers = ask_question(query)
149 | say_answers(
150 | "There are ", "institutes in jahangirnagar university", question, answers
151 | )
152 |
153 | elif (
154 | "names of the faculties" in asked_question
155 | or "what are the faculties" in asked_question
156 | ):
157 | # Q what are the faculties in jahangirnagar university
158 | question = "Facultiy"
159 | query = "faculties('jahangirnagar university', " + question + ")."
160 | answers = ask_question(query)
161 | say_answers(
162 | "there are 6 faculties are in jahangirnagar university, they are, ",
163 | "",
164 | question,
165 | answers,
166 | )
167 |
168 | elif (
169 | "names of the departments" in asked_question
170 | or "what are the departments" in asked_question
171 | ) and "under the faculty of" in asked_question:
172 | # Q what are the names departments in faculty of X?
173 | faculties = [
174 | "faculty of mathematical and physical science",
175 | "faculty of biological science",
176 | "faculty of social science",
177 | "faculty of arts and humanities",
178 | "faculty of business studies",
179 | "faculty of law",
180 | ]
181 | id = -1
182 | for i in range(6):
183 | if faculties[i] in asked_question:
184 | id = i
185 | break
186 | if id != -1:
187 | print(faculties[id])
188 | question = "Departments"
189 | query = (
190 | "departments_under_faculty('jahangirnagar university', '"
191 | + faculties[id]
192 | + "',"
193 | + question
194 | + ")."
195 | )
196 | answers = ask_question(query)
197 | print(">>>>> ", "the departments under " + faculties[id] + " are, ")
198 | say_answers("", "", question, answers)
199 |
200 | else:
201 | print(">>>>> ", "sorry, there is no such faculty.")
202 |
203 | elif (
204 | "names of the departments" in asked_question
205 | or "what are the departments" in asked_question
206 | ):
207 | # Q what are the departments in jahangirnagar university
208 | question = "Departments"
209 | query = "departments('jahangirnagar university', " + question + ")."
210 | answers = ask_question(query)
211 | say_answers(
212 | "there are 34 departments in jahangirnagar university, they are, ",
213 | "",
214 | question,
215 | answers,
216 | )
217 |
218 | elif (
219 | "about department of cse" in asked_question
220 | or "about cse" in asked_question
221 | or "about computer science and engineering" in asked_question
222 | or "about department of computer science and engineering" in asked_question
223 | ):
224 | # Q what you know about dept of CSE jahangirnagar university
225 | question = "Cse"
226 | query = (
227 | "about_department_of_computer_science_and_engineering(\
228 | 'jahangirnagar university', "
229 | + question
230 | + ")."
231 | )
232 | answers = ask_question(query)
233 | say_answers("", "", question, answers)
234 |
235 | elif (
236 | "chairman of department of cse" in asked_question
237 | or "chairman of cse" in asked_question
238 | or "chairman of computer science and engineering" in asked_question
239 | or "chairman of department of computer science and engineering"
240 | in asked_question
241 | ):
242 | # Q who is the chairman of dept of CSE JU?
243 | question = "Chairman"
244 | query = (
245 | "chairman_of_cse('department of computer science and engineering', "
246 | + question
247 | + ")."
248 | )
249 | answers = ask_question(query)
250 | say_answers(
251 | "",
252 | "is the chairman of department of computer science and engineering",
253 | question,
254 | answers,
255 | )
256 |
257 | elif (
258 | "who are the developers of this project" in asked_question
259 | or "who developed" in asked_question
260 | or "who created" in asked_question
261 | ):
262 | # Q who developed this program?
263 | question = "Developers"
264 | query = "developers(" + question + ")."
265 | answers = ask_question(query)
266 | print(
267 | ">>>>> ",
268 | "This project is supervised by professor doctor mohammad shorif uddin.",
269 | )
270 | say_answers("the developers are", "", question, answers)
271 |
272 | elif "stop" in asked_question or "exit" in asked_question:
273 | print(">>>>> ", "thank you, hope you have enjoyed the session")
274 | break
275 |
276 | else:
277 | if asked_question != "-----------------":
278 | confirmation = str(
279 | input(
280 | "Sorry, this is out of my knowledge. whould you like to continue? "
281 | )
282 | ).lower()
283 |
284 | if "no" in confirmation or "nope" in confirmation or "stop" in confirmation:
285 | print(">>>>> ", "thank you, hope you have enjoyed the session")
286 | break
287 | else:
288 | continue
289 |
290 | time.sleep(2)
291 |
--------------------------------------------------------------------------------