spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

[Enhancement]: add scores to Displacy entity markup

Open cceyda opened this issue 10 months ago • 0 comments

Description

This PR adds the ability to pass scores along with entities to displacy with the manual mode. If no score is passed there is no change.

It uses the template below:

# Doing it with a template similar to TPL_KB_LINK so that we don't get empty '()' parenthesis if no score is present
TPL_SCORE = "({score:.2f})"

not sure if should allow modification of template|precision through options?

Example

So far only tested with manual=True mode on jupyter, but if anyone else is willing to complete it feel free~

from spacy import displacy

text="Hello world with scores"
formatted_entities =[  {'start': 0,
                        'end': 5,
                        'label': 'greeting',
                        'score': 0.90},
                        {'start': 5,
                        'end': 11,
                        'label': 'planet',
                        'score': 0.86666},
                        {'start': 17,
                        'end': 23,
                        'label': 'yeay',
                        'score': 0.5},
                        ]
displacy.render([{"text": text, "ents": formatted_entities, "title": None}], style="ent", manual=True,jupyter=True)
image

If a 'score' doesn't exist it isn't shown: image

Checklist

  • [x] I confirm that I have the right to submit this contribution under the project's MIT license.
  • [ ] I ran the tests, and all new and existing tests passed.
  • [ ] My changes don't require a change to the documentation, or if they do, I've added all required information.

cceyda avatar Feb 19 '25 11:02 cceyda