From 4c2bbc3ccc2c6830846764376a52edb307ef592e Mon Sep 17 00:00:00 2001 From: ines Date: Sat, 3 Jun 2017 19:44:47 +0200 Subject: [PATCH] Add add_lookups util function --- spacy/util.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spacy/util.py b/spacy/util.py index 55f2a49bb..469123479 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -299,6 +299,22 @@ def compile_infix_regex(entries): return re.compile(expression) +def add_lookups(default_func, *lookups): + """Extend an attribute function with special cases. If a word is in the + lookups, the value is returned. Otherwise the previous function is used. + + default_func (callable): The default function to execute. + *lookups (dict): Lookup dictionary mapping string to attribute value. + RETURNS (callable): Lexical attribute getter. + """ + def get_attr(string): + for lookup in lookups: + if string in lookup: + return lookup[string] + return default_func(string) + return get_attr + + def update_exc(base_exceptions, *addition_dicts): """Update and validate tokenizer exceptions. Will overwrite exceptions.