def __main__(): logging_lvl = logging.INFO args = _get_cl_args() if args.debug == "DEBUG": logging_lvl = logging.DEBUG else: # set level to display nothing logging_lvl = logging.INFO global kb_ID kb_client = KnowledgeBaseClient(False) kb_ID = (kb_client.register())['details'] logging.basicConfig(stream=sys.stderr, level=logging_lvl) tags = { TAG_DRS : {'desc' : 'DRS structure', 'doc' : DESC_DRS}, TAG_ANSWER : {'desc' : 'stupid answer', 'doc' : DESC_ANSWER} } logging.info("\tQA module registered") #TODO register tags nedeed? qa_service = QaService(kb_ID,logging_lvl) t1 = Qa_Thread(qa_service) t1.start() k_service = ConstantFromkB(kb_ID,logging_lvl) t2 = K_Thread(k_service) t2.start()
def __main__(): kb_client = KnowledgeBaseClient(False) kb_ID = (kb_client.register())['details'] tags = [{ TAG_ANSWER : {'desc' : 'Fake by ENLP', 'doc' : 'FAKE by ENLP'}}, { TAG_VISION : {'desc' : 'Fake by ENLP', 'doc' : 'FAKE by ENLP'}}, { TAG_USER_TRANSCRIPT : {'desc' : 'Fake by ENLP', 'doc' : 'FAKE by ENLP'}}] kb_client.registerTags(kb_ID, tags[0]) kb_client.registerTags(kb_ID, tags[1]) kb_client.registerTags(kb_ID, tags[2]) obj_from_vision = { "tag": TAG_VISION, "is_interlocutor" : "True", "time_stamp" : str(datetime.datetime.now()), 'emotion': { 'sadness': 0.34, 'calm': 0.48, 'disgust': 0.54, 'anger': 0.67, 'surprise': 0.77, 'fear': 0.54, 'happiness': 0.23 } } obj_from_erasmus = { "tag": TAG_ANSWER, "text": "Risposta degli erasmus. bla bla bla", "user_query" : "Dove è la lezione del professor attardi?", "time_stamp" : 1, "language" : "it" } obj_from_stt = { "tag": TAG_USER_TRANSCRIPT, "text": "Dove è la lezione del professor attardi", "language": "it" } print("sending Vision data:") res = kb_client.addFact(kb_ID, TAG_VISION, 1, 100, obj_from_vision) print("vision data response:") print(res) time.sleep(1) print("sending STT data:") res = kb_client.addFact(kb_ID, TAG_USER_TRANSCRIPT, 1, 100, obj_from_stt) print("STT data response:") print(res) time.sleep(1) print("sending erasmus data:") res = kb_client.addFact(kb_ID, TAG_ANSWER, 1, 100, obj_from_erasmus) print("erasmus data response:") print(res)
def __main__(): kb_client = KnowledgeBaseClient(False) kb_ID = (kb_client.register())['details'] tags = {TAG_ANSWER: {'desc': 'Fake by ENLP', 'doc': 'FAKE by ENLP'}} kb_client.registerTags(kb_ID, tags) obj_from_erasmus = { "tag": TAG_ANSWER, "text": "Professor Attardi", "user_query": "What time Prof. Gervasi teaches Smart Application?", "time_stamp": 1 } res = kb_client.addFact(kb_ID, TAG_ANSWER, 1, 100, obj_from_erasmus) print(res)
def __main__(): kb_client = KnowledgeBaseClient(False) kb_ID = (kb_client.register())['details'] kb_client.registerTags(kb_ID, { TAG_USER_TRANSCRIPT : {'desc' : 'Fake by ENLP', 'doc' : 'FAKE by ENLP'} }) obj_from_stt = { "tag": TAG_USER_TRANSCRIPT, "timestamp" : 7, "ID": "fake_stt", "text": "We will build a great wall", "language": "en", "valence" :0.5, "arousal" : 0.5 } res = kb_client.addFact(kb_ID, TAG_USER_TRANSCRIPT, 1, 100, obj_from_stt) print(res)
def __main__(): logging_lvl = logging.INFO args = _get_cl_args() if args.debug == "DEBUG": logging_lvl = logging.DEBUG else: # set level to display nothing logging_lvl = logging.INFO kb_client = KnowledgeBaseClient(True) global kb_ID kb_ID = (kb_client.register())['details'] logging.basicConfig(stream=sys.stderr, level=logging_lvl) tags = { TAG_USER_EMOTION : {'desc' : 'Emotion of what user said', 'doc' : DESC_USER_EMOTION}, TAG_ELF_EMOTION : {'desc' : 'Internal emotion of ELF', 'doc' : DESC_ELF_EMOTION}, TAG_COLORED_ANSWER : {'desc' : 'Reply to the user with emotion content in it', 'doc' : DESC_COLORED_ANSWER} } tag_list = [TAG_USER_EMOTION, TAG_ELF_EMOTION, TAG_COLORED_ANSWER] kb_client.registerTags(kb_ID, tags) """ for tag in tag_list: check_tag = kb_client.getTagDetails([tag]) if not check_tag['success']: res = kb_client.registerTags(kb_ID, { tag : tags[tag] } ) if not res['success']: logging.critical(res['details']) return """ logging.info("\tEmotional NLP module registered") ett_service = EttService(kb_ID, logging_lvl) t1 = EttThread(ett_service) t1.start() tte_service = TteService(kb_ID,logging_lvl) t2 = TteThread(tte_service) t2.start() ies_service = IESService(kb_ID, logging_lvl) t3 = IESThread(ies_service) t3.start()
import json from kb import KnowledgeBaseClient k = KnowledgeBaseClient(True) myID = k.register()['details'] registering = k.registerTags(myID, { "RDF": "an rdf triple", "TEST": "test data" }) print(registering) if (registering['success'] == 0): print('registration failed') def callbfun(res): print("callback:") print(res) print(k.subscribe(myID, {"_data": {"prova": "$x"}}, callbfun)) print(k.addFact(myID, "TEST", 1, 50, {"prova": 1})) print(k.addFact(myID, "TEST", 1, 50, {"prova": 2})) print(k.addFact(myID, "TEST", 1, 50, {"prova": 3})) print(k.removeFact(myID, {"_data": {"prova": 2}})) print(k.queryBind({"_data": {"prova": "$x"}})) print(k.addFact(myID, "TEST", 1, 50, {"prova": "callb"}))
class GNLP_Service: def __init__(self): listTag = { 'NLP_ANSWER': { 'desc': 'general_nlp_answer', 'doc': 'nlp_answer_doc' }, 'NLP_ANALYSIS': { 'desc': 'parse_trees_and_entity_rec', 'doc': 'nlp_analysis_doc' } } self.KBC = KnowledgeBaseClient(True) self.ID = (self.KBC.register())['details'] nlp_answer_info = { 'desc': 'Query answer from General NLP', 'doc': 'doc about nlp_answer' } nlp_analysis_info = { 'desc': 'Query analysis from General NLP', 'doc': 'doc about nlp_analysis' } self.KBC.registerTags(self.ID, { 'NLP_ANSWER': nlp_answer_info, 'NLP_ANALYSIS': nlp_analysis_info }) print("Registered to the KB") def analyse(self, *res): ''' Callback that analyse the user query TODO: Handle the different intents and querys the KB for the needed informations ''' print("Analysing...") print(res) obj = res[0]['details'][0]['object']['_data'] question = obj['text'] lang = obj['language'] ts = obj['timestamp'] print(question) #question = question['text'] luis_analysis = NLP_Understand(question, language=lang) spacy_analysis = get_dependency_tree(question, language=lang) self.KBC.addFact( self.ID, "NLP_ANALYSIS", 1, 50, { "tag": "NLP_ANALYSIS", "language": lang, "entities": luis_analysis, "dependencies": spacy_analysis, "user_query": question, "timestamp": ts }) # Logging some infos pp = pprint.PrettyPrinter() pp.pprint(luis_analysis) pp.pprint(spacy_analysis) print(question) self.answer(question, lang, ts) def answer(self, question, lang, ts): ''' Callback that answer the user query ''' print("Answering...") answer = "Non ho barzellette al momento per ora!" self.KBC.addFact( self.ID, "NLP_ANSWER", 1, 50, { "tag": "NLP_ANSWER", "language": lang, "text": answer, "user_query": question, "timestamp": ts }) def start_service(self): TAG_USER_TRANSCRIPT = "AV_IN_TRANSC_EMOTION" TAG_CRW_RAW_INFO = "CRAWLER_DATA_ENTRY" TAG_REASONER_OUTPUT = "REASONING_FRAME" self.KBC.subscribe( self.ID, { "_data": { "tag": TAG_USER_TRANSCRIPT, "text": "$d", "language": "$lang", "timestamp": "$ts" } }, self.analyse) print("Subscribed to the KB")
from kb import KnowledgeBaseClient kb = KnowledgeBaseClient(True) if (sys.argv[1] == "query"): r = kb.query(json.loads(sys.argv[2])) elif (sys.argv[1] == "addfact"): r = kb.addFact(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], json.loads(sys.argv[6])) elif (sys.argv[1] == "addrule"): r = kb.addRule(sys.argv[2], sys.argv[3], sys.argv[4]) elif (sys.argv[1] == "removefact"): r = kb.removeFact(sys.argv[2], json.loads(sys.argv[3])) elif (sys.argv[1] == "removerule"): r = kb.removeRule(sys.argv[2], json.loads(sys.argv[3])) elif (sys.argv[1] == "updatefact"): r = kb.updateFactByID(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], json.loads(sys.argv[7])) elif (sys.argv[1] == "registertags"): r = kb.registerTags(sys.argv[2], json.loads(sys.argv[3])) elif (sys.argv[1] == "tagdetails"): r = kb.getTagDetails(sys.argv[2:]) elif (sys.argv[1] == "getalltags"): r = kb.getAllTags( len(sys.argv) >= 3 and sys.argv[2].lower() in ["true", "yes", "y"]) elif (sys.argv[1] == "register"): r = kb.register() else: r = "invalid argument" print(sys.argv[2:]) print(r)
import sys from interface_tags import PATH_TO_KB_MODULE sys.path.insert(0, PATH_TO_KB_MODULE) from kb import KnowledgeBaseClient client = KnowledgeBaseClient(False) kb_id = (client.register())['details'] rules = [ '{"_meta":{"tag":"TEACHING"}, "teach": "$prof", "room": "$room", "course" : "$course" } <- {"_meta":{"tag":"crawler_course"}, "_data":{"data": {"name" : "$course", "teacher_name": "$prof"}}};{"_meta":{"tag":"crawler_room_event"},"_predicates":[["containsString", ["$course2", "$course"]]], "_data": {"data": {"aula" : "$room", "descrizione" : "$course2"}}}' #'{"_meta":{"tag":"TEACHING"}, "teach": "$prof", "room": "$room", "course" : "$course" } <- {"_meta":{"tag":"crawler_course"}, "_data":{"data": {"name" : "$course", "teacher_name": "$prof"}}};{"_meta":{"tag":"crawler_room_event"}, "_data": {"data": {"aula" : "$room", "descrizione" : "$course"}}}' #'{"tag":"teaching": "$prof", "room": "$room", "course" : "$course" } <- {"data":{"name" : "$course", "teacher_name": "$prof"}};{"data":{"aula" : "$room", "descrizione" : "$course"}}' ] """client.removeRule(kb_id,2) for rule in rules: x = client.addRule(kb_id, "ENLP_EMOTIVE_ANSWER", rule) print(x) """ #print(client.query({"_data" : {"name" : "nlpcourse", "teacher_name": "Giuseppe Attardi"}})) #print(client.query({"_data" : {"aula" : "$X1", "descrizione": "nlpcourse"}})) res = client.query({"_data": {"teach": "GIUSEPPE ATTARDI", "room": "$x"}}) #res = client.query({"_data":{"teach":"$x"}}) print(res) # DO NOT THIS ANYMORE #res = client.query({"_data": "$x"})