Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python text error
#4
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()
Reply


Messages In This Thread
Python text error - by Prashant93 - Oct-10-2018, 08:27 PM
RE: Python text error - by ichabod801 - Oct-10-2018, 08:34 PM
RE: Python text error - by Prashant93 - Oct-10-2018, 09:33 PM
RE: Python text error - by Prashant93 - Oct-11-2018, 04:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  cx_Oracle.DatabaseError: Error while trying to retrieve text from error ORA-01804 rajeshparadker 0 8,712 Nov-12-2020, 07:34 PM
Last Post: rajeshparadker
  Getting Python error: AttributeError: 'tuple' object has no attribute 'Text' dageci 2 14,373 Jul-26-2018, 08:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020