Oct-11-2018, 04:46 PM
I tried to work on this code but it is giving me output only for money and not org and person. Any help would be greatly appreciated. Thanks.
from __future__ import unicode_literals, print_function import plac import spacy TEXTS = [ 'Net income was $9.4 million compared to the prior year of $2.7 million.', 'Revenue exceeded twelve billion dollars, with a loss of $1b.', ] @plac.annotations( model=("Model to load (needs parser and NER)", "positional", None, str)) def main(model='en_core_web_sm'): nlp = spacy.load(model) print("Loaded model '%s'" % model) print("Processing %d texts" % len(TEXTS)) for text in TEXTS: doc = nlp(text) relations = extract_currency_relations(doc) for r1, r2 in relations: print('{:<10}\t{}\t{}'.format(r1.text, r2.ent_type_, r2.text)) def extract_currency_relations(doc): # merge entities and noun chunks into one token spans = list(doc.ents) + list(doc.noun_chunks) for span in spans: span.merge() relations = [] for money in filter(lambda w: w.ent_type_ == 'MONEY', doc): if money.dep_ in ('attr', 'dobj'): subject = [w for w in money.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, money)) elif money.dep_ == 'pobj' and money.head.dep_ == 'prep': relations.append((money.head.head, money)) return relations relations = [] for person in filter(lambda w: w.ent_type_ == 'PERSON', doc): if person.dep_ in ('attr', 'dobj'): subject = [w for w in person.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, person)) elif person.dep_ == 'pobj' and person.head.dep_ == 'prep': relations.append((person.head.head, person)) return relations relations = [] for org in filter(lambda w: w.ent_type_ == 'ORG', doc): if org.dep_ in ('attr', 'dobj'): subject = [w for w in org.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, org)) elif org.dep_ == 'pobj' and org.head.dep_ == 'prep': relations.append((org.head.head, org)) return relations if __name__ == '__main__': plac.call(main) main()