[Python] How to correctly identify entity types for tokens using spaCy using python?

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm using spaCy to extract and identify entity types (like ORG, GPE, DATE, etc.) from a text description. However, I am noticing some incorrect results, and I'm unsure how to fix this.

    Here is the code I am using:

    import spacy

    nlp = spacy.load("en_core_web_sm")

    def getPayeeName(description):
    description = description.replace("-", " ").replace("/", " ").strip()
    doc = nlp(description)

    for token in doc:
    print(f"Token: {token.text}, Entity: {token.ent_type_ if token.ent_type_ else 'None'}")

    # Example input
    description = "UPI DR 400874707203 BENGALORE 08 JAN 2024 14:38:56 MEDICAL LTD HDFC 50200"
    getPayeeName(description)


    Token: UPI, Entity: ORG

    Token: DR, Entity: ORG

    Token: 400874707203, Entity: None

    Token: BENGALORE, Entity: None

    Token: 08, Entity: DATE

    Token: JAN, Entity: DATE

    Token: 2024, Entity: DATE

    Token: 14:38:56, Entity: None

    Token: MEDICAL, Entity: ORG

    Token: LTD, Entity: ORG

    Token: HDFC, Entity: ORG

    Token: 50200, Entity: ORG


    • 50200 is identified as ORG, but it is just a number.


    • BENGALORE is a city, but it is not recognized as a GPE or location (returns None).


    • UPI and DR are acronyms/abbreviations, but they are incorrectly identified as ORG.

    I want the entity recognition to be more accurate and reliable. How can I fix these issues? Are there additional spaCy configurations, custom rules, or pre-trained models I should use to improve the entity recognition?

    Note: I tried ChatGPT as well, but still this issue is not solved.

    Continue reading...

Compartilhe esta Página