def tag_phrase(phrases, utterances): final_utterances = [] for iter in utterances: utterance = get(iter, "utterance") case_converted_utterance = get(iter, "case_converted_utterance") mapping = get(iter, "mapping") mapping = json.loads(mapping) tokens = get(mapping, "tokens") text = get(mapping, "text") tags = get(mapping, "tags") intent = get(mapping, "intent") phrase = phrase_checker(utterance, phrases) final_tags = tags for phr in phrase: final_tags.append(check(tags, phr)) final_tags = remove_duplicates(final_tags) final_tags.sort(key=lambda k: len(k['entity']), reverse=True) mappings = { "tokens": tokens, "text": text, "tags": final_tags, "intent": intent } data = { "utterance": utterance, "case_converted_utterance": case_converted_utterance, "mapping": json.dumps(mappings) } final_utterances.append(data) return final_utterances
def get_entities_for_ds(self, serviceid, text, datasources_map): predefined_tags = datasources_map["predefined_entities"] if 'predefined_entities' in datasources_map else [] pattern_response = [] phrase_response = [] pattern_entities = datasources_map[ "patterns"] if 'patterns' in datasources_map else [] phrase_entities = datasources_map[ "phrases"] if 'phrases' in datasources_map else [] if len(pattern_entities) != 0: pattern_response = regex_checker(text, pattern_entities) if len(phrase_entities) != 0: phrase_response = phrase_checker(text, phrase_entities) return pattern_response, phrase_response, predefined_tags
def test_phrase_checker_with_data2(mocker): phrases = phrase_checker('my fav color is black and charcoal black ', [{ "phrase": ["black", "sky blue", "pink"], "entity": "colors" }]) assert phrases == [{ 'end': 5, 'entity': 'black', 'start': 4, 'tag': 'colors' }, { 'end': 8, 'entity': 'black', 'start': 7, 'tag': 'colors' }]
def test_phrase_checker_with_data1(mocker): phrases = phrase_checker('my fav color is blue and i hate pink', [{ "phrase": ["blue", "yellow", "pink"], "entity": "colors" }]) assert phrases == [{ 'end': 5, 'entity': 'blue', 'start': 4, 'tag': 'colors' }, { 'end': 9, 'entity': 'pink', 'start': 8, 'tag': 'colors' }]
def test_phrase_checker_with_null(mocker): phrase = phrase_checker('', []) assert phrase == []