dfrankow
dfrankow

Reputation: 21459

How to display spaCy named entities in plain text?

spaCy visualizers render named entities into HTML or SVG. Is there any easy way to get quick debug output in plaintext that looks good? I'm using spaCy 3.7.2 and python 3.11.

Upvotes: 0

Views: 110

Answers (2)

dfrankow
dfrankow

Reputation: 21459

I ended up making a simple utility function:

def char_span_string(span: Span, extra: str = "") -> str:
    return (
            (span.start_char * " ")
            + ((span.end_char - span.start_char) * "-")
            + " "
            + span.label_
            + " "
            + str(span.start_char)
            + ".."
            + str(span.end_char)
            + extra
    )

for ent in doc.ents:
    print(char_span_string(ent))

Example output:

3 tablespoons minced scallions
  ----------- UNIT 2..13
                     --------- INGREDIENT 21..30

Upvotes: 0

حمزة نبيل
حمزة نبيل

Reputation: 1931

You can iterate on entities and display text and label:

import spacy
from spacy import displacy

text = "When Sebastian Thrun started working on self-driving cars at Google in 2007, few people outside of the company took him seriously."

nlp = spacy.load("en_core_web_sm")
doc = nlp(text)


for ent in doc.ents:
    print(f"{ent.text}  => {ent.label_}")

output :

Sebastian Thrun  => PERSON
Google  => ORG
2007  => DATE

For more informations see EntityRecognizer documentation

Upvotes: 0

Related Questions