Created
January 19, 2018 04:38
-
-
Save saiury92/50ff4a047b10db62d5d1024ac9004eb5 to your computer and use it in GitHub Desktop.
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
import spacy.en | |
from spacy.symbols import VERB, nsubj, dobj | |
def find_acquisitions(nlp, text, buy_words): | |
doc = nlp(text) | |
for ent in doc.ents: | |
ent.merge(ent.root.tag_, ent.text, ent.label_) | |
buy_words = set(nlp.vocab.strings[w] for w in buy_words) | |
for token in doc: | |
if token.pos == VERB and token.lemma in buy_words: | |
buyer = [w for w in token.lefts if w.dep == nsubj] | |
bought = [w for w in token.rights if w.dep == dobj] | |
if buyer and bought: | |
yield token, buyer[0], bought[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment