add flask backend files for components in KGQnA folder

parent 02d55174
This diff is collapsed.
# import json
# import pandas
import os
class exportToJSON:
"""docstring for exportToJSON."""
def __init__(self):
super(exportToJSON, self).__init__()
def dumpdata(self, pairs):
if os.path.exists(os.path.join(os.getcwd(), 'extra')):
pass
else:
os.makedirs('extra')
my_data = pairs.to_json('extra/database.json', orient='index')
# print(my_data)
class exportToCSV:
"""docstring for exportToJSON."""
def __init__(self):
super(exportToJSON, self).__init__()
def dumpdata(self, pairs):
df = pairs.to_csv(index=False)
# ff = pairs.to_csv('out.zip', index=False, compression=compression_opts)
# print(df)
# df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
# 'mask': ['red', 'purple'],
# 'weapon': ['sai', 'bo staff']})
#
# df.to_csv(index=False)
# 'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n'
#
# Create ‘out.zip’ containing ‘out.csv’
# compression_opts = dict(method='zip',
# archive_name='out.csv')
# df.to_csv('out.zip', index=False,
# compression=compression_opts)
# import re
import pandas as pd
import spacy
from KGQnA._complex import ComplexFunc
from KGQnA._resolvedep import change_nouns
class GetEntity:
"""docstring for GetEntity."""
def __init__(self):
super(GetEntity, self).__init__()
self.complex = ComplexFunc()
self.nlp = spacy.load('en_core_web_sm')
self.change = change_nouns()
def preprocess_text(self, input_file):
text_strip = [text.strip() for text in input_file]
preprocessed_text = [text for text in text_strip if text not in ('', ' ')]
text = " ".join(preprocessed_text)
text = self.change.resolved(text)
text = self.nlp(text)
return text
def get_entity(self, text):
ent_pairs, final_entity_pairs = [],[]
sentences = [one_sentence.text.strip() for one_sentence in text.sents]
for one_sentence in sentences:
final_entity_pairs = []
one_sentence = self.nlp(one_sentence)
dep = [token.dep_ for token in one_sentence]
# print(dep)
# pos = [token.pos_ for token in one_sentence]
# label = [token.label_ for token in one_sentence.ents]
normal_sent_ = self.complex.normal_sent(one_sentence)
if normal_sent_:
for pair in normal_sent_:
ent_pairs.append(pair)
pairs = pd.DataFrame(ent_pairs, columns=['source', 'relation', 'aux_relation', 'target', 'time', 'place'])
number_of_ent_pairs = str(len(ent_pairs))
final_entity_pairs.append(pairs)
if final_entity_pairs:
return final_entity_pairs, number_of_ent_pairs
return None, None
if __name__ == '__main__':
test = GetEntity()
text = test.nlp("Vibhav ate chocolates. Vedant met Vibhav")
entities, numbers = test.get_entity(text)
# print(entities[0])
import matplotlib.pyplot as plt
import networkx as nx
import pandas as pd
from KGQnA._getentitypair import GetEntity
class GraphEnt:
"""docstring for graphEnt."""
def __init__(self):
super(GraphEnt, self).__init__()
self.x = GetEntity()
def createGraph(self, dataEntities):
entity_list = dataEntities.values.tolist()
source, relations, target = [],[],[]
for i in entity_list:
# if i[0] == "" or i[1] == "" or i[3] == "":
# pass
# else:
source.append(i[0])
relations.append(i[1])
# aux_relations = i[2]
target.append(i[3])
# time = i[4]
# place = i[5]
kg_df = pd.DataFrame({'source':source, 'target':target, 'edge':relations})
G=nx.from_pandas_edgelist(kg_df, "source", "target", edge_attr=True, create_using=nx.MultiDiGraph())
plt.figure(figsize=(12,12))
pos = nx.spring_layout(G, k = 2) # k regulates the distance between nodes
nx.draw(G, with_labels=True, node_color='skyblue', node_size=1500, edge_cmap=plt.cm.Blues, pos = pos)
# nx.draw_networkx_edge_labels(G,pos,edge_labels=labels,font_size=30)
plt.show()
if __name__ == '__main__':
test = GraphEnt()
print("Can't Test directly")
import re
import json
import spacy
import inflect
import requests
from KGQnA._getentitypair import GetEntity
from KGQnA._complex import *
class QuestionAnswer:
"""docstring for QuestionAnswer."""
def __init__(self):
super(QuestionAnswer, self).__init__()
self.complex = ComplexFunc()
self.nlp = spacy.load('en_core_web_sm')
self.p = inflect.engine()
def findanswer(self, question, c=None, con=None):
if con is None:
p = self.complex.question_pairs(question)
if p == [] or p is None:
return "Not Applicable"
pair = p[0]
# print(pair[5])
f = open("extra/database.json","r", encoding="utf8")
listData = f.readlines()
relQ = []
loaded = json.loads(listData[0])
relationQ = self.nlp(pair[1])
for i in relationQ:
relationQ = i.lemma_
relQ.append(relationQ)
objectQ = pair[3]
subList = []
timeQ = str(pair[4]).lower()
placeQ = str(pair[5]).lower()
# print(timeQ, placeQ)
relationQ = " ".join(relQ)
# print(relationQ)
if pair[0] in ('who'):
for i in loaded:
relationS = [relation for relation in self.nlp(loaded[str(i)]["relation"])]
relationSSS = " ".join([relation.lemma_ for relation in self.nlp(loaded[str(i)]["relation"])])
relationS = [i.lemma_ for i in relationS]
relationS = relationS[0]
# print(relationSSS)
if relationS == relationQ:
objectS = loaded[str(i)]["target"]
objectS = re.sub('-', ' ', objectS)
objectQ = re.sub('-', ' ', objectQ)
# print(objectQ, objectS)
if self.p.singular_noun(objectS):
objectS = self.p.singular_noun(objectS)
if self.p.singular_noun(objectQ):
objectQ = self.p.singular_noun(objectQ)
if objectS == objectQ:
if str(pair[4]) != "":
timeS = [str(loaded[str(i)]["time"]).lower()]
# print(timeQ, timeS)
if timeQ in timeS:
answer_subj = loaded[str(i)]["source"]
subList.append(answer_subj)
else:
answer_subj = loaded[str(i)]["source"]
subList.append(answer_subj)
elif str(relationSSS) == str(relationQ):
objectS = loaded[str(i)]["target"]
objectS = re.sub('-', ' ', objectS)
if objectS == objectQ:
if str(pair[4]) != "":
timeS = [str(loaded[str(i)]["time"]).lower()]
if timeQ in timeS:
answer_subj = loaded[str(i)]["source"]
subList.append(answer_subj)
else:
answer_subj = loaded[str(i)]["source"]
subList.append(answer_subj)
answer_subj = ",".join(subList)
if answer_subj == "":
return "None"
return answer_subj
elif pair[3] in ['what']:
subjectQ = pair[0]
subList = []
for i in loaded:
subjectS = loaded[str(i)]["source"]
# print(subjectQ, subjectS)
if subjectQ == subjectS:
relationS = [relation for relation in self.nlp(loaded[str(i)]["relation"])]
relationS = [i.lemma_ for i in relationS]
if len(relationS) > 1:
relationS = " ".join(relationS)
else:
relationS = relationS[0]
# print(relationQ, relationS)
if relationQ == relationS:
if str(pair[5]) != "":
placeS = [str(place).lower() for place in self.nlp(loaded[str(i)]["place"])]
# print(placeQ, placeS)
if placeQ in placeS:
if str(pair[4]) != "":
timeS = [str(time).lower() for time in self.nlp(loaded[str(i)]["time"])]
if timeQ in timeS:
answer_subj = loaded[str(i)]["target"]
subList.append(answer_subj)
else:
answer_subj = loaded[str(i)]["target"]
subList.append(answer_subj)
else:
if str(pair[4]) != "":
timeS = [str(time).lower() for time in self.nlp(loaded[str(i)]["time"])]
if timeQ in timeS:
answer_subj = loaded[str(i)]["target"]
subList.append(answer_subj)
else:
answer_subj = loaded[str(i)]["target"]
subList.append(answer_subj)
answer_obj = ",".join(subList)
if answer_obj == "":
return "None"
return answer_obj
elif pair[4] in ['when']:
subjectQ = pair[0]
# print(relationQ, subjectQ)
# print(pair[2])
for i in loaded:
# if i.dep_ in ('obj'):
# print(loaded[str(i)], "HERE we go")
subjectS = loaded[str(i)]["source"]
# print(type(subjectQ), type(subjectS), numberOfPairs)
if subjectQ == subjectS:
relationS = [relation for relation in self.nlp(loaded[str(i)]["relation"])]
# print(relationS)
relationS = [i.lemma_ for i in relationS]
relBuffer = relationS
# print(relationS[0], relationS[1])
# print(relBuffer[1])
if len(relBuffer) < 2:
relationS = relBuffer[0]
else:
if str(relBuffer[1]).lower() == 'to':
relationS = " ".join(relationS)
else:
relationS = relationS[0]
extraIN = relBuffer[1].lower()
# print(relationQ, relationS)
if relationQ == relationS:
if str(pair[5]) != "":
placeS = [str(place).lower() for place in self.nlp(loaded[str(i)]["place"])]
# print(placeQ, placeS)
if placeQ in placeS:
if loaded[str(i)]["time"] != '':
answer_obj = loaded[str(i)]["time"]
# elif extraIN == "in" or extraIN == "on":
# answer_obj = loaded[str(i)]["target"]
return answer_obj
return None
else:
if loaded[str(i)]["time"] != '':
answer_obj = loaded[str(i)]["time"]
return answer_obj
return None
elif pair[5] in ['where']:
subjectQ = pair[0]
for i in loaded:
subjectS = loaded[str(i)]["source"]
if subjectQ == subjectS:
relationS = [relation for relation in self.nlp(loaded[str(i)]["relation"])]
relationS = [i.lemma_ for i in relationS]
relationS = relationS[0]
if relationQ == relationS:
if str(pair[4]) != "":
timeS = [str(time).lower() for time in self.nlp(loaded[str(i)]["time"])]
if timeQ in timeS:
answer_obj = loaded[str(i)]["place"]
if answer_obj in (" ",""):
if int(i)<int(len(loaded)-1):
pass
return None
return answer_obj
return None
answer_obj = loaded[str(i)]["place"]
if answer_obj in (" ",""):
if int(i)<int(len(loaded)-1):
pass
return None
return answer_obj
else:
output = self.complex.nlp_(question=question, context=con)
return output
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment