Last active
December 4, 2019 07:02
-
-
Save rjurney/b8480d149c08db8466480a2b58ac2016 to your computer and use it in GitHub Desktop.
How do you chain a preprocessor for an LF to occur AFTER SpacyPreprocessor?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spacy = SpacyPreprocessor( | |
text_field='body', | |
doc_field='spacy', | |
memoize=True, | |
language='en_core_web_lg', | |
disable=['vectors'] | |
) | |
@preprocessor(memoize=True, pre=[spacy]) | |
def restore_entity(x): | |
entity = None | |
for ent in x['spacy'].ents: | |
if ent.start == row['entity_start'] \ | |
and ent.end == row['entity_end']: | |
entity = ent | |
if entity is None: | |
raise Exception('Missing entity!') | |
x['entity'] = entity | |
return x | |
@labeling_function(pre=[spacy, restore_entity]) | |
def lf_verb_in_noun(x): | |
"""Return positive if the pattern""" | |
sp = x['spacy'] | |
matches = matcher(sp) | |
found = False | |
for match_id, start, end in matches: | |
if end == x['entity_end']: | |
pass | |
if start == x['start'] - 2: | |
if sp[start].text in ['work', 'written', 'wrote']: | |
if sp[start + 1].text in ['in']: | |
return POSITIVE | |
else: | |
return ABSTAIN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ipython-input-55-bf0c5fb416fc> in restore_entity(x) | |
17 | |
18 entity = None | |
---> 19 for ent in x['spacy'].ents: | |
20 if ent.start == row['entity_start'] \ | |
21 and ent.end == row['entity_end']: | |
KeyError: ('spacy', 'occurred at index 0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment