def test_should_persist_parser(self): # Given parser = BuiltinEntityParser.build("en") # When with temp_dir() as tmpdir: persisted_path = str(tmpdir / "persisted_builtin_parser") parser.persist(persisted_path) loaded_parser = BuiltinEntityParser.from_path(persisted_path) res = loaded_parser.parse("Raise the temperature to 9 degrees", None) # Then expected_result = [{ "value": "9 degrees", "entity": { "kind": "Temperature", "unit": "degree", "value": 9.0 }, "range": { "start": 25, "end": 34 }, "entity_kind": "snips/temperature" }] self.assertListEqual(expected_result, res)
def test_should_persist_parser_with_gazetteer_entities(self): # Given parser = BuiltinEntityParser.from_path(BUILTIN_PARSER_PATH) # When with temp_dir() as tmpdir: persisted_path = str(tmpdir / "persisted_builtin_parser") parser.persist(persisted_path) loaded_parser = BuiltinEntityParser.from_path(persisted_path) res = loaded_parser.parse("I want to listen to the stones", None) # Then expected_result = [{ "value": "the stones", "entity": { "kind": "MusicArtist", "value": "The Rolling Stones" }, "range": { "start": 20, "end": 30 }, "entity_kind": "snips/musicArtist" }] self.assertListEqual(expected_result, res)
def test_should_parse_with_gazetteer_entity(self): # Given gazetteer_parser_path = ROOT_DIR / "data" / "tests" / \ "builtin_gazetteer_parser" parser = BuiltinEntityParser.build("en", gazetteer_parser_path) scope = ["snips/musicArtist"] # When res = parser.parse("I want to listen to the stones please!", scope) # Then expected_result = [{ "entity": { "kind": "MusicArtist", "value": "The Rolling Stones" }, "entity_kind": "snips/musicArtist", "range": { "end": 30, "start": 20 }, "value": "the stones" }] self.assertListEqual(expected_result, res)
def test_should_not_accept_bytes_in_scope(self): # Given scope = [b"snips/number", b"snips/datetime"] parser = BuiltinEntityParser.build("en") # When/Then with self.assertRaises(TypeError): parser.parse("Raise to sixty", scope)
def test_should_not_accept_bytes_in_text(self): # Given parser = BuiltinEntityParser.build("en") bytes_text = b"Raise to sixty" # When/Then with self.assertRaises(TypeError): parser.parse(bytes_text)
def test_should_parse_in_all_languages(self): # Given all_languages = get_all_languages() text = "1234" # When / Then for language in all_languages: parser = BuiltinEntityParser.build(language) parser.parse(text)
def parse_snips(self, text, lang): from snips_nlu_parsers import BuiltinEntityParser if lang in self.parsers: parser = self.parsers[lang] else: parser = BuiltinEntityParser.build(language=lang) self.parsers[lang] = parser parsing = parser.parse(text) return parsing
def test_should_fail_to_extend_non_extensible_parser(self): # Given parser = BuiltinEntityParser.build("en") # When / Then with self.assertRaises(ValueError) as cm: entity_values = [ { "raw_value": "my first custom artist", "resolved_value": "my first resolved custom artist" } ] parser.extend_gazetteer_entity("snips/musicArtist", entity_values) self.assertTrue("No gazetteer parser found for entity 'MusicArtist'" in str(cm.exception))
def _build_builtin_parser(language, gazetteer_entities): with temp_dir() as serialization_dir: gazetteer_entity_parser = None if gazetteer_entities: gazetteer_entity_parser = _build_gazetteer_parser( serialization_dir, gazetteer_entities, language) metadata = { "language": language.upper(), "gazetteer_parser": gazetteer_entity_parser } metadata_path = serialization_dir / "metadata.json" with metadata_path.open("w", encoding="utf-8") as f: f.write(json_string(metadata)) parser = _BuiltinEntityParser.from_path(serialization_dir) return BuiltinEntityParser(parser)
def test_should_parse_with_extended_gazetteer_entity(self): # Given gazetteer_parser_path = ROOT_DIR / "data" / "tests" / \ "builtin_gazetteer_parser" parser = BuiltinEntityParser.build("en", gazetteer_parser_path) entity_values = [ { "raw_value": "my first custom artist", "resolved_value": "my first resolved custom artist" }, { "raw_value": "my second custom artist", "resolved_value": "my second resolved custom artist" }, ] parser.extend_gazetteer_entity("snips/musicArtist", entity_values) # When res = parser.parse( "I want to listen to my first custom artist please and then the " "rolling stones", scope=["snips/musicArtist"]) # Then expected_result = [ { "entity": { "kind": "MusicArtist", "value": "my first resolved custom artist" }, "alternatives": [], "entity_kind": "snips/musicArtist", "range": {"end": 42, "start": 20}, "value": "my first custom artist", }, { "entity": { "kind": "MusicArtist", "value": "The Rolling Stones" }, "alternatives": [], "entity_kind": "snips/musicArtist", "range": {"end": 77, "start": 59}, "value": "the rolling stones" } ] self.assertListEqual(expected_result, res)
def snips_provider(self, cnt, lang, ctx, key): from snips_nlu_parsers import BuiltinEntityParser if lang not in ('de', 'en', 'es', 'fr', 'it', 'pt', 'ja', 'ko'): return False result = False if lang in self.parsers: parser = self.parsers[lang] else: parser = BuiltinEntityParser.build(language='pt_pt' if lang=='pt' else lang) self.parsers[lang] = parser parsing = parser.parse(cnt) dims = [d['entity_kind'] for d in parsing] print(cnt, '->', 'dims', dims, 'to fits in', self.dims) if self.fits(dims): result = True ctx.add_result(self.name(), self.provider, key, parsing) return result
def test_should_load_parser_with_gazetteer_entities_from_path(self): # Given parser = BuiltinEntityParser.from_path(BUILTIN_PARSER_PATH) # When res = parser.parse("I want to listen to the stones", None) # Then expected_result = [{ "value": "the stones", "entity": { "kind": "MusicArtist", "value": "The Rolling Stones" }, "range": { "start": 20, "end": 30 }, "entity_kind": "snips/musicArtist" }] self.assertListEqual(expected_result, res)
def test_should_parse_without_scope(self): # Given parser = BuiltinEntityParser.build("en") # When res = parser.parse("Raise to sixty two degrees celsius") # Then expected_result = [{ "entity": { "kind": "Temperature", "unit": "celsius", "value": 62.0 }, "entity_kind": "snips/temperature", "range": { "end": 34, "start": 9 }, "value": "sixty two degrees celsius" }] self.assertListEqual(expected_result, res)
def test_should_load_parser_from_path(self): # Given parser = BuiltinEntityParser.from_path( BUILTIN_PARSER_NO_GAZETTEER_PATH) # When res = parser.parse("Raise the temperature to 9 degrees", None) # Then expected_result = [{ "value": "9 degrees", "entity": { "kind": "Temperature", "unit": "degree", "value": 9.0 }, "range": { "start": 25, "end": 34 }, "entity_kind": "snips/temperature" }] self.assertListEqual(expected_result, res)
def test_should_parse_with_scope(self): # Given parser = BuiltinEntityParser.build("en") scope = ["snips/duration", "snips/temperature"] # When res = parser.parse("Raise to sixty two", scope) # Then expected_result = [{ "entity": { "kind": "Temperature", "unit": None, "value": 62.0 }, "entity_kind": "snips/temperature", "range": { "end": 18, "start": 9 }, "value": "sixty two" }] self.assertListEqual(expected_result, res)
def from_path(cls, path): from snips_nlu_parsers import (BuiltinEntityParser as _BuiltinEntityParser) parser = _BuiltinEntityParser.from_path(path) return cls(parser)
def response(utterances, userID='123', show_details=False): print(utterances) context = {} seed = 42 engine = snips_nlu_engine parser = BuiltinEntityParser.build(language="en") user_input = json.loads(utterances) print(user_input) samp = {} for key, value in user_input.items(): if key != 'text': samp.update({key: value}) parsing = engine.parse(user_input['text']) doc = json.loads(json.dumps(parsing)) print(doc) intent = doc['intent'] print(intent) if not doc['slots']: resp = 'Sorry,I do not get you, Please say it again!' full_response = { 'Date': None, 'Time': None, 'Doctor Name': None, 'text': str(resp) } return json.dumps(full_response) name = "" for res in doc['slots']: if res['entity'] == 'doc': name = res['rawValue'] name1 = process.extractOne(name, list_doc) print(name1) if name1[1] < 68: print(name1[0]) full_response = { 'text': 'Sorry,the doctor by this name does not exist,So please say the correct doctor name' } samp.update(full_response) print(samp) return json.dumps(samp) else: Doctors_Name = name1[0] intent = doc['intent'] Intent = intent['intentName'] if (Intent == 'doctor_day_time'): u1 = json.loads(utterances) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) count = len(date) if count == 1: date_time = date[0]['entity']['value'] d = dateutil.parser.parse(date_time).date() d1 = dateutil.parser.parse(date_time).time() else: date_time = date[0]['entity']['value'] date_time1 = date[1]['entity']['value'] if date[1]['entity']['grain'] == 'Hour': d = dateutil.parser.parse(date_time).date() d1 = dateutil.parser.parse(date_time1).time() else: d = dateutil.parser.parse(date_time1).date() d1 = dateutil.parser.parse(date_time).time() full_response = { 'Date': str(d), 'Time': str(d1), 'Doctor Name': str(Doctors_Name) } dictionary = { 'Date': str(d), 'Time': str(d1), 'Doctor Name': str(Doctors_Name) } elif (Intent == 'doctor_day'): u1 = json.loads(utterances) print(u1['text']) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) date_time = date[0]['entity']['value'] d = dateutil.parser.parse(date_time).date() full_response = { 'Date': str(d), 'Time': None, 'Doctor Name': str(Doctors_Name) } dictionary = {'Date': str(d), 'Doctor Name': str(Doctors_Name)} elif (Intent == 'doctor'): full_response = { 'Date': None, 'Time': None, 'Doctor Name': str(Doctors_Name) } dictionary = {'Doctor Name': str(Doctors_Name)} elif (Intent == 'time'): u1 = json.loads(utterances) print(u1['text']) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) date_time = date[0]['entity']['value'] d1 = dateutil.parser.parse(date_time).time() full_response = {'Date': None, 'Time': str(d1), 'Doctor Name': None} dictionary = {'Time': str(d1)} elif (Intent == 'day'): u1 = json.loads(utterances) print(u1['text']) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) date_time = date[0]['entity']['value'] d = dateutil.parser.parse(date_time).date() full_response = {'Date': str(d), 'Time': None, 'Doctor Name': None} dictionary = {'Date': str(d)} elif (Intent == 'day_time'): u1 = json.loads(utterances) print(u1['text']) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) date_time = date[0]['entity']['value'] d = dateutil.parser.parse(date_time).date() d1 = dateutil.parser.parse(date_time).time() full_response = {'Date': str(d), 'Time': str(d1), 'Doctor Name': None} dictionary = {'Date': str(d), 'Time': str(d1)} elif (Intent == 'doctor_time'): u1 = json.loads(utterances) print(u1['text']) parsing1 = parser.parse(u1['text']) date = json.loads(json.dumps(parsing1)) date_time = date[0]['entity']['value'] d1 = dateutil.parser.parse(date_time).time() full_response = { 'Date': None, 'Time': str(d1), 'Doctor Name': str(Doctors_Name) } dictionary = {'Time': str(d1), 'Doctor Name': str(Doctors_Name)} else: full_response = {'Date': None, 'Time': None, 'Doctor Name': None} dictionary = {'Date': None, 'Time': None, 'Doctor Name': None} with open('./proj_reply_new.json') as json_data: intents = json.load(json_data) if intent: while intent: for i in intents['intents']: print(i, Intent) if i['tag'] == Intent: if 'context_set' in i: if show_details: print('context:', i['contexit_set']) context[userID] = i['context_set'] if not 'context_filter' in i or \ (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]): if show_details: print('tag:', i['tag']) full_response.update( {"text": str(random.choice(i['responses']))}) return json.dumps(full_response) Intent.pop(0) print('******************************') print(dictionary) new_dict = json.dumps(dictionary) f = open("data.txt", "a") resaa = f.write(new_dict) f.close() file = open("data.txt", "r") for line in file.readlines(): #print (line) resa = line.replace('}{', ', ') print(resa) ret = json.loads(resa) # print(ret) # print(type(ret)) resa1 = {'Date': None, 'Time': None, 'Doctor Name': None} #print(type(resa1)) resultt = merge(resa1, ret) pprint(resultt, width=40) count = 0 for c in resultt.values(): if c is not None: count = count + 1 print(count) if count == 3: dr = resultt["Doctor Name"] dt = resultt["Date"] di = resultt["Time"] full_response = { 'Date': str(dt), 'Time': str(di), 'Doctor Name': str(dr), 'text': 'Your appointment booking is successfull with {} for {} at {}'. format(dr, dt, di) } bod = ("Your appointment booking is successfull with {} for {} at {}". format(dr, dt, di)) message = client.messages \ .create( body= str(bod), from_ ='+12024102004', to ='+916265454670' ) file.close() os.remove("data.txt") return json.dumps(full_response) #os.remove("data.txt") elif count == 2: if (resultt['Doctor Name'] != None and resultt['Date'] != None): Intent = "doctor_day" full_response = resultt elif (resultt['Date'] != None and resultt['Time'] != None): Intent = "day_time" full_response = resultt elif (resultt['Time'] != None and resultt['Doctor Name'] != None): Intent = "doctor_time" full_response = resultt elif count == 1: if (resultt['Doctor Name'] != None): Intent = "doctor" full_response = resultt elif (resultt['Time'] != None): Intent = "time" full_response = resultt elif (resultt['Date'] != None): Intent = "day" full_response = resultt with open('./proj_reply_new.json') as json_data: intents = json.load(json_data) if intent: while intent: for i in intents['intents']: print(i, Intent) if i['tag'] == Intent: if 'context_set' in i: if show_details: print('context:', i['contexit_set']) context[userID] = i['context_set'] if not 'context_filter' in i or \ (userID in context and 'context_filter' in i and i['context_filter'] == context[userID]): if show_details: print('tag:', i['tag']) full_response.update( {"text": str(random.choice(i['responses']))}) return json.dumps(full_response) Intent.pop(0)
def from_path(cls, path): parser = _BuiltinEntityParser.from_path(path) return cls(parser)
def test_should_not_accept_bytes_as_language(self): with self.assertRaises(TypeError): BuiltinEntityParser.build(b"en")