2017-04-15 10:05:47 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
2015-10-10 06:55:55 +00:00
|
|
|
IDS = {
|
2015-10-10 11:10:19 +00:00
|
|
|
"": NULL_ATTR,
|
2015-10-06 13:39:50 +00: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,
|
|
|
|
"IS_OOV": IS_OOV,
|
2016-03-10 12:01:34 +00:00
|
|
|
"IS_BRACKET": IS_BRACKET,
|
|
|
|
"IS_QUOTE": IS_QUOTE,
|
|
|
|
"IS_LEFT_PUNCT": IS_LEFT_PUNCT,
|
|
|
|
"IS_RIGHT_PUNCT": IS_RIGHT_PUNCT,
|
2018-02-11 17:51:32 +00:00
|
|
|
"IS_CURRENCY": IS_CURRENCY,
|
2015-10-06 13:39:50 +00:00
|
|
|
"FLAG19": FLAG19,
|
|
|
|
"FLAG20": FLAG20,
|
|
|
|
"FLAG21": FLAG21,
|
|
|
|
"FLAG22": FLAG22,
|
|
|
|
"FLAG23": FLAG23,
|
|
|
|
"FLAG24": FLAG24,
|
|
|
|
"FLAG25": FLAG25,
|
|
|
|
"FLAG26": FLAG26,
|
|
|
|
"FLAG27": FLAG27,
|
|
|
|
"FLAG28": FLAG28,
|
|
|
|
"FLAG29": FLAG29,
|
|
|
|
"FLAG30": FLAG30,
|
|
|
|
"FLAG31": FLAG31,
|
|
|
|
"FLAG32": FLAG32,
|
|
|
|
"FLAG33": FLAG33,
|
|
|
|
"FLAG34": FLAG34,
|
|
|
|
"FLAG35": FLAG35,
|
|
|
|
"FLAG36": FLAG36,
|
|
|
|
"FLAG37": FLAG37,
|
|
|
|
"FLAG38": FLAG38,
|
|
|
|
"FLAG39": FLAG39,
|
|
|
|
"FLAG40": FLAG40,
|
|
|
|
"FLAG41": FLAG41,
|
|
|
|
"FLAG42": FLAG42,
|
|
|
|
"FLAG43": FLAG43,
|
|
|
|
"FLAG44": FLAG44,
|
|
|
|
"FLAG45": FLAG45,
|
|
|
|
"FLAG46": FLAG46,
|
|
|
|
"FLAG47": FLAG47,
|
|
|
|
"FLAG48": FLAG48,
|
|
|
|
"FLAG49": FLAG49,
|
|
|
|
"FLAG50": FLAG50,
|
|
|
|
"FLAG51": FLAG51,
|
|
|
|
"FLAG52": FLAG52,
|
|
|
|
"FLAG53": FLAG53,
|
|
|
|
"FLAG54": FLAG54,
|
|
|
|
"FLAG55": FLAG55,
|
|
|
|
"FLAG56": FLAG56,
|
|
|
|
"FLAG57": FLAG57,
|
|
|
|
"FLAG58": FLAG58,
|
|
|
|
"FLAG59": FLAG59,
|
|
|
|
"FLAG60": FLAG60,
|
|
|
|
"FLAG61": FLAG61,
|
|
|
|
"FLAG62": FLAG62,
|
|
|
|
"FLAG63": FLAG63,
|
|
|
|
|
|
|
|
"ID": ID,
|
|
|
|
"ORTH": ORTH,
|
|
|
|
"LOWER": LOWER,
|
|
|
|
"NORM": NORM,
|
|
|
|
"SHAPE": SHAPE,
|
|
|
|
"PREFIX": PREFIX,
|
|
|
|
"SUFFIX": SUFFIX,
|
|
|
|
|
|
|
|
"LENGTH": LENGTH,
|
|
|
|
"CLUSTER": CLUSTER,
|
|
|
|
"LEMMA": LEMMA,
|
|
|
|
"POS": POS,
|
|
|
|
"TAG": TAG,
|
|
|
|
"DEP": DEP,
|
|
|
|
"ENT_IOB": ENT_IOB,
|
|
|
|
"ENT_TYPE": ENT_TYPE,
|
|
|
|
"HEAD": HEAD,
|
2016-05-05 10:11:57 +00:00
|
|
|
"SENT_START": SENT_START,
|
2015-10-06 13:39:50 +00:00
|
|
|
"SPACY": SPACY,
|
|
|
|
"PROB": PROB,
|
2016-03-10 12:01:34 +00:00
|
|
|
"LANG": LANG,
|
2015-10-06 13:39:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 10:34:30 +00:00
|
|
|
|
2015-10-06 13:39:50 +00:00
|
|
|
# ATTR IDs, in order of the symbol
|
2015-10-10 06:55:55 +00:00
|
|
|
NAMES = [key for key, value in sorted(IDS.items(), key=lambda item: item[1])]
|
2017-09-17 17:28:53 +00:00
|
|
|
locals().update(IDS)
|
2016-11-25 10:34:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def intify_attrs(stringy_attrs, strings_map=None, _do_deprecated=False):
|
2017-04-15 09:59:21 +00:00
|
|
|
"""
|
|
|
|
Normalize a dictionary of attributes, converting them to ints.
|
2017-02-26 21:27:11 +00:00
|
|
|
|
2017-10-27 19:07:59 +00: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 09:59:21 +00:00
|
|
|
"""
|
2016-11-25 10:34:30 +00:00
|
|
|
inty_attrs = {}
|
|
|
|
if _do_deprecated:
|
|
|
|
if 'F' in stringy_attrs:
|
|
|
|
stringy_attrs["ORTH"] = stringy_attrs.pop("F")
|
|
|
|
if 'L' in stringy_attrs:
|
|
|
|
stringy_attrs["LEMMA"] = stringy_attrs.pop("L")
|
|
|
|
if 'pos' in stringy_attrs:
|
|
|
|
stringy_attrs["TAG"] = stringy_attrs.pop("pos")
|
2016-11-25 15:17:32 +00:00
|
|
|
if 'morph' in stringy_attrs:
|
|
|
|
morphs = stringy_attrs.pop('morph')
|
2016-11-25 15:57:18 +00:00
|
|
|
if 'number' in stringy_attrs:
|
|
|
|
stringy_attrs.pop('number')
|
|
|
|
if 'tenspect' in stringy_attrs:
|
|
|
|
stringy_attrs.pop('tenspect')
|
2016-12-18 15:50:26 +00:00
|
|
|
morph_keys = [
|
|
|
|
'PunctType', 'PunctSide', 'Other', 'Degree', 'AdvType', 'Number',
|
|
|
|
'VerbForm', 'PronType', 'Aspect', 'Tense', 'PartType', 'Poss',
|
|
|
|
'Hyph', 'ConjType', 'NumType', 'Foreign', 'VerbType', 'NounType',
|
2017-03-15 14:26:16 +00:00
|
|
|
'Gender', 'Mood', 'Negative', 'Tense', 'Voice', 'Abbr',
|
|
|
|
'Derivation', 'Echo', 'Foreign', 'NameType', 'NounType', 'NumForm',
|
|
|
|
'NumValue', 'PartType', 'Polite', 'StyleVariant',
|
|
|
|
'PronType', 'AdjType', 'Person', 'Variant', 'AdpType',
|
2017-02-26 21:27:11 +00:00
|
|
|
'Reflex', 'Negative', 'Mood', 'Aspect', 'Case',
|
2018-02-18 14:46:12 +00:00
|
|
|
'Polarity', 'PrepCase', 'Animacy' # U20
|
2017-02-26 21:27:11 +00:00
|
|
|
]
|
2016-12-18 15:50:26 +00:00
|
|
|
for key in morph_keys:
|
|
|
|
if key in stringy_attrs:
|
|
|
|
stringy_attrs.pop(key)
|
2016-12-18 16:33:46 +00:00
|
|
|
elif key.lower() in stringy_attrs:
|
|
|
|
stringy_attrs.pop(key.lower())
|
|
|
|
elif key.upper() in stringy_attrs:
|
|
|
|
stringy_attrs.pop(key.upper())
|
2016-11-25 10:34:30 +00:00
|
|
|
for name, value in stringy_attrs.items():
|
|
|
|
if isinstance(name, int):
|
|
|
|
int_key = name
|
|
|
|
else:
|
|
|
|
int_key = IDS[name.upper()]
|
|
|
|
if strings_map is not None and isinstance(value, basestring):
|
2017-05-28 13:10:22 +00:00
|
|
|
if hasattr(strings_map, 'add'):
|
|
|
|
value = strings_map.add(value)
|
|
|
|
else:
|
|
|
|
value = strings_map[value]
|
2016-11-25 10:34:30 +00:00
|
|
|
inty_attrs[int_key] = value
|
|
|
|
return inty_attrs
|