You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In our newest project we are using a wrapped version of replacy to support list variables in match_dicts, like so
importjsonimportosfromtypingimportListfromreplacyimportReplaceMatcherfromreplacy.dbimportload_jsonasload_replacy_files_from_directoryhere=os.path.abspath(os.path.dirname(__file__))
classModifiedReplaceMatcher:
def__init__(self):
rd_path=os.path.join(here, "resources/match_dicts")
proto_match_dict=load_replacy_files_from_directory(rd_path)
vocab_refs=self._load_vocab_refs("resources/variables/vocab_refs.json")
self.rmatch_dict=self._refine_match_dict(proto_match_dict, vocab_refs)
def_load_vocab_refs(self, vocab_refs_path: str):
file_path=os.path.join(here, vocab_refs_path)
withopen(file_path, "r", encoding="utf-8") asf:
returnjson.load(f)
def_remove_square_brackets_from_list_of_strings(self, l: List[str]) ->str:
""" look at me, I'm metaprogramming turns ["a", "b", "c"] into '"a", "b", "c"' """list_str='"'list_str+='", "'.join(l)
list_str+='"'returnlist_strdef_refine_match_dict(self, match_dict: dict, vocab_refs: dict) ->dict:
""" Replace $REF:something by vocab list from vocab_refs.json file This should probably be replaCy functionality And we could add functionality if we did fancier parsing """r_matcher_str=json.dumps(match_dict)
forref_id, ref_listinvocab_refs.items():
# this is a sinref_list_str=self._remove_square_brackets_from_list_of_strings(ref_list)
target=f'"$REF:{ref_id}"'r_matcher_str=r_matcher_str.replace(target, ref_list_str)
# end sinreturnjson.loads(r_matcher_str)
defget_matcher(self, nlp, kenlm_path):
returnReplaceMatcher(nlp, match_dict=self.rmatch_dict, lm_path=kenlm_path)
Where resources/variables/vocab_refs.json would have an entry like
{
"variable-name": [
"hello",
"hi",
"yo"
]
}
This allows for a match dict syntax like:
[
{"LOWER":{"IN": ["$REF:variable-name"]}}
]
which is convenient for frequently-used lists of words. Lists are easier than dicts though.
The text was updated successfully, but these errors were encountered:
sam-writer
changed the title
Support list and dict variables in match_dicts
Support list variables in match_dicts
Sep 22, 2020
In our newest project we are using a wrapped version of replacy to support list variables in match_dicts, like so
Where
resources/variables/vocab_refs.json
would have an entry likeThis allows for a match dict syntax like:
which is convenient for frequently-used lists of words. Lists are easier than dicts though.
The text was updated successfully, but these errors were encountered: