Skip to content

Instantly share code, notes, and snippets.

@saiury92
Created January 19, 2018 04:38
Show Gist options
  • Save saiury92/50ff4a047b10db62d5d1024ac9004eb5 to your computer and use it in GitHub Desktop.
Save saiury92/50ff4a047b10db62d5d1024ac9004eb5 to your computer and use it in GitHub Desktop.
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