2022-01-20 15:19:38 +03:00
|
|
|
from .errors import Errors
|
|
|
|
|
|
|
|
IOB_STRINGS = ("", "I", "O", "B")
|
2017-04-15 13:05:47 +03:00
|
|
|
|
2015-10-10 09:55:55 +03:00
|
|
|
IDS = {
|
2015-10-10 14:10:19 +03:00
|
|
|
"": NULL_ATTR,
|
2015-10-06 16:39:50 +03:00
|
|
|
"IS_ALPHA": IS_ALPHA,
|
|
|
|
"IS_ASCII": IS_ASCII,
|
|
|
|
"IS_DIGIT": IS_DIGIT,
|
|
|
|
"IS_LOWER": IS_LOWER,
|
|
|
|
"IS_PUNCT": IS_PUNCT,
|
|
|
|
"IS_SPACE": IS_SPACE,
|
|
|
|
"IS_TITLE": IS_TITLE,
|
|
|
|
"IS_UPPER": IS_UPPER,
|
|
|
|
"LIKE_URL": LIKE_URL,
|
|
|
|
"LIKE_NUM": LIKE_NUM,
|
|
|
|
"LIKE_EMAIL": LIKE_EMAIL,
|
|
|
|
"IS_STOP": IS_STOP,
|
2016-03-10 15:01:34 +03:00
|
|
|
"IS_BRACKET": IS_BRACKET,
|
|
|
|
"IS_QUOTE": IS_QUOTE,
|
|
|
|
"IS_LEFT_PUNCT": IS_LEFT_PUNCT,
|
|
|
|
"IS_RIGHT_PUNCT": IS_RIGHT_PUNCT,
|
2018-02-11 20:51:32 +03:00
|
|
|
"IS_CURRENCY": IS_CURRENCY,
|
2015-10-06 16:39:50 +03:00
|
|
|
"ID": ID,
|
|
|
|
"ORTH": ORTH,
|
|
|
|
"LOWER": LOWER,
|
|
|
|
"NORM": NORM,
|
|
|
|
"SHAPE": SHAPE,
|
|
|
|
"PREFIX": PREFIX,
|
|
|
|
"SUFFIX": SUFFIX,
|
|
|
|
"LENGTH": LENGTH,
|
|
|
|
"LEMMA": LEMMA,
|
|
|
|
"POS": POS,
|
|
|
|
"TAG": TAG,
|
|
|
|
"DEP": DEP,
|
|
|
|
"ENT_IOB": ENT_IOB,
|
|
|
|
"ENT_TYPE": ENT_TYPE,
|
2020-01-06 16:57:34 +03:00
|
|
|
"ENT_ID": ENT_ID,
|
2019-06-25 16:28:51 +03:00
|
|
|
"ENT_KB_ID": ENT_KB_ID,
|
2015-10-06 16:39:50 +03:00
|
|
|
"HEAD": HEAD,
|
2016-05-05 13:11:57 +03:00
|
|
|
"SENT_START": SENT_START,
|
2015-10-06 16:39:50 +03:00
|
|
|
"SPACY": SPACY,
|
2016-03-10 15:01:34 +03:00
|
|
|
"LANG": LANG,
|
2020-01-29 19:45:46 +03:00
|
|
|
"MORPH": MORPH,
|
2022-01-20 15:19:38 +03:00
|
|
|
"IDX": IDX,
|
2015-10-06 16:39:50 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:34:30 +03:00
|
|
|
|
2022-09-02 10:08:40 +03:00
|
|
|
NAMES = {v: k for k, v in IDS.items()}
|
2017-09-17 20:28:53 +03:00
|
|
|
locals().update(IDS)
|
2016-11-25 13:34:30 +03:00
|
|
|
|
|
|
|
|
2022-08-17 13:13:54 +03:00
|
|
|
def intify_attrs(stringy_attrs, strings_map=None):
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
|
|
|
Normalize a dictionary of attributes, converting them to ints.
|
2017-02-27 00:27:11 +03:00
|
|
|
|
2017-10-27 22:07:59 +03:00
|
|
|
stringy_attrs (dict): Dictionary keyed by attribute string names. Values
|
|
|
|
can be ints or strings.
|
|
|
|
strings_map (StringStore): Defaults to None. If provided, encodes string
|
|
|
|
values into ints.
|
|
|
|
RETURNS (dict): Attributes dictionary with keys and optionally values
|
|
|
|
converted to ints.
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2016-11-25 13:34:30 +03:00
|
|
|
inty_attrs = {}
|
|
|
|
for name, value in stringy_attrs.items():
|
2019-10-03 15:48:45 +03:00
|
|
|
int_key = intify_attr(name)
|
|
|
|
if int_key is not None:
|
2022-01-20 15:19:38 +03:00
|
|
|
if int_key == ENT_IOB:
|
|
|
|
if value in IOB_STRINGS:
|
|
|
|
value = IOB_STRINGS.index(value)
|
|
|
|
elif isinstance(value, str):
|
|
|
|
raise ValueError(Errors.E1025.format(value=value))
|
2021-09-13 18:02:17 +03:00
|
|
|
if strings_map is not None and isinstance(value, str):
|
2022-01-20 15:19:38 +03:00
|
|
|
if hasattr(strings_map, "add"):
|
2019-10-03 15:48:45 +03:00
|
|
|
value = strings_map.add(value)
|
|
|
|
else:
|
|
|
|
value = strings_map[value]
|
|
|
|
inty_attrs[int_key] = value
|
2016-11-25 13:34:30 +03:00
|
|
|
return inty_attrs
|
2019-10-03 15:48:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
def intify_attr(name):
|
|
|
|
"""
|
|
|
|
Normalize an attribute name, converting it to int.
|
|
|
|
|
|
|
|
stringy_attr (string): Attribute string name. Can also be int (will then be left unchanged)
|
|
|
|
RETURNS (int): int representation of the attribute, or None if it couldn't be converted.
|
|
|
|
"""
|
|
|
|
if isinstance(name, int):
|
|
|
|
return name
|
|
|
|
elif name in IDS:
|
|
|
|
return IDS[name]
|
|
|
|
elif name.upper() in IDS:
|
|
|
|
return IDS[name.upper()]
|
|
|
|
return None
|