Skip to content

Instantly share code, notes, and snippets.

@rjurney rjurney/pre.py
Last active Dec 4, 2019

Embed
What would you like to do?
How do you chain a preprocessor for an LF to occur AFTER SpacyPreprocessor?
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
<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
You can’t perform that action at this time.