mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 01:48:04 +03:00 
			
		
		
		
	Adjust label casing in displaCy NER visualizer (resolves #4866)
- Accept any case for label names in ents and colors option, even if actual predicted label uses different casing - Don't text-transform: uppercase visually, if it's important to users that the label is represented as-is in the UI
This commit is contained in:
		
							parent
							
								
									c356e62908
								
							
						
					
					
						commit
						e60442d83a
					
				| 
						 | 
				
			
			@ -252,8 +252,10 @@ class EntityRenderer:
 | 
			
		|||
            colors.update(user_color)
 | 
			
		||||
        colors.update(options.get("colors", {}))
 | 
			
		||||
        self.default_color = DEFAULT_ENTITY_COLOR
 | 
			
		||||
        self.colors = colors
 | 
			
		||||
        self.colors = {label.upper(): color for label, color in colors.items()}
 | 
			
		||||
        self.ents = options.get("ents", None)
 | 
			
		||||
        if self.ents is not None:
 | 
			
		||||
            self.ents = [ent.upper() for ent in self.ents]
 | 
			
		||||
        self.direction = DEFAULT_DIR
 | 
			
		||||
        self.lang = DEFAULT_LANG
 | 
			
		||||
        template = options.get("template")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,14 +51,14 @@ TPL_ENTS = """
 | 
			
		|||
TPL_ENT = """
 | 
			
		||||
<mark class="entity" style="background: {bg}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;">
 | 
			
		||||
    {text}
 | 
			
		||||
    <span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; text-transform: uppercase; vertical-align: middle; margin-left: 0.5rem">{label}</span>
 | 
			
		||||
    <span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem">{label}</span>
 | 
			
		||||
</mark>
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
TPL_ENT_RTL = """
 | 
			
		||||
<mark class="entity" style="background: {bg}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em">
 | 
			
		||||
    {text}
 | 
			
		||||
    <span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; text-transform: uppercase; vertical-align: middle; margin-right: 0.5rem">{label}</span>
 | 
			
		||||
    <span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-right: 0.5rem">{label}</span>
 | 
			
		||||
</mark>
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
import pytest
 | 
			
		||||
from spacy import displacy
 | 
			
		||||
from spacy.displacy.render import DependencyRenderer
 | 
			
		||||
from spacy.displacy.render import DependencyRenderer, EntityRenderer
 | 
			
		||||
from spacy.tokens import Span
 | 
			
		||||
from spacy.lang.fa import Persian
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -97,3 +97,17 @@ def test_displacy_render_wrapper(en_vocab):
 | 
			
		|||
    assert html.endswith("/div>TEST")
 | 
			
		||||
    # Restore
 | 
			
		||||
    displacy.set_render_wrapper(lambda html: html)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_displacy_options_case():
 | 
			
		||||
    ents = ["foo", "BAR"]
 | 
			
		||||
    colors = {"FOO": "red", "bar": "green"}
 | 
			
		||||
    renderer = EntityRenderer({"ents": ents, "colors": colors})
 | 
			
		||||
    text = "abcd"
 | 
			
		||||
    labels = ["foo", "bar", "FOO", "BAR"]
 | 
			
		||||
    spans = [{"start": i, "end": i + 1, "label": labels[i]} for i in range(len(text))]
 | 
			
		||||
    result = renderer.render_ents("abcde", spans, None).split("\n\n")
 | 
			
		||||
    assert "red" in result[0] and "foo" in result[0]
 | 
			
		||||
    assert "green" in result[1] and "bar" in result[1]
 | 
			
		||||
    assert "red" in result[2] and "FOO" in result[2]
 | 
			
		||||
    assert "green" in result[3] and "BAR" in result[3]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -257,7 +257,7 @@ If a setting is not present in the options, the default value will be used.
 | 
			
		|||
| Name                                    | Description                                                                                                                                                                                                                                                                 |
 | 
			
		||||
| --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
 | 
			
		||||
| `ents`                                  | Entity types to highlight or `None` for all types (default). ~~Optional[List[str]]~~                                                                                                                                                                                        |
 | 
			
		||||
| `colors`                                | Color overrides. Entity types in uppercase should be mapped to color names or values. ~~Dict[str, str]~~                                                                                                                                                                    |
 | 
			
		||||
| `colors`                                | Color overrides. Entity types should be mapped to color names or values. ~~Dict[str, str]~~                                                                                                                                                                                 |
 | 
			
		||||
| `template` <Tag variant="new">2.2</Tag> | Optional template to overwrite the HTML used to render entity spans. Should be a format string and can use `{bg}`, `{text}` and `{label}`. See [`templates.py`](https://github.com/explosion/spaCy/blob/master/spacy/displacy/templates.py) for examples. ~~Optional[str]~~ |
 | 
			
		||||
 | 
			
		||||
By default, displaCy comes with colors for all entity types used by
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -122,9 +122,9 @@ import DisplacyEntHtml from 'images/displacy-ent2.html'
 | 
			
		|||
The entity visualizer lets you customize the following `options`:
 | 
			
		||||
 | 
			
		||||
| Argument | Description                                                                                                   |
 | 
			
		||||
| -------- | -------------------------------------------------------------------------------------------------------------------------- |
 | 
			
		||||
| -------- | ------------------------------------------------------------------------------------------------------------- |
 | 
			
		||||
| `ents`   | Entity types to highlight (`None` for all types). Defaults to `None`. ~~Optional[List[str]]~~                 | `None` |
 | 
			
		||||
| `colors` | Color overrides. Entity types in uppercase should be mapped to color names or values. Defaults to `{}`. ~~Dict[str, str]~~ |
 | 
			
		||||
| `colors` | Color overrides. Entity types should be mapped to color names or values. Defaults to `{}`. ~~Dict[str, str]~~ |
 | 
			
		||||
 | 
			
		||||
If you specify a list of `ents`, only those entity types will be rendered – for
 | 
			
		||||
example, you can choose to display `PERSON` entities. Internally, the visualizer
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user