-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
util.py
49 lines (34 loc) 路 1.43 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
import logging
import os
from typing import Text
from rasa.nlu import utils
logger = logging.getLogger(__name__)
def transform_entity_synonyms(synonyms, known_synonyms=None):
"""Transforms the entity synonyms into a text->value dictionary"""
entity_synonyms = known_synonyms if known_synonyms else {}
for s in synonyms:
if "value" in s and "synonyms" in s:
for synonym in s["synonyms"]:
entity_synonyms[synonym] = s["value"]
return entity_synonyms
def check_duplicate_synonym(entity_synonyms, text, syn, context_str=""):
if text in entity_synonyms and entity_synonyms[text] != syn:
logger.warning(
"Found inconsistent entity synonyms while {0}, "
"overwriting {1}->{2} "
"with {1}->{3} during merge"
"".format(context_str, text, entity_synonyms[text], syn)
)
def get_file_format(resource_name: Text) -> Text:
from rasa.nlu.training_data import loading
if resource_name is None or not os.path.exists(resource_name):
raise AttributeError("Resource '{}' does not exist.".format(resource_name))
files = utils.list_files(resource_name)
file_formats = list(map(lambda f: loading.guess_format(f), files))
if not file_formats:
return "json"
fformat = file_formats[0]
if fformat == "md" and all(f == fformat for f in file_formats):
return fformat
return "json"