Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-680: rendering HTML with highlighted named entities #933

Merged
merged 7 commits into from
Jul 31, 2019

Conversation

azeevg
Copy link
Contributor

@azeevg azeevg commented Jul 27, 2019

Added rendering HTML with highlighted entities. It's possible to override colors and labels of entities. One can also render part of html not wrapped by body and html tags - it can be useful when one needs to concatenate few htmls.

Usage

tagger = SequenceTagger.load('ner')

sentence = Sentence(
    "Thibaut Pinot's challenge ended on Friday due to injury, and then Julian Alaphilippe saw "
    "his lead fall away. The BBC's Hugh Schofield in Paris reflects on 34 years of hurt."
)

tagger.predict(sentence)
html = Visualizer.render_ner_html(sentence)

with open("sentence.html", "w") as writer:
    writer.write(html)

sentence.html
image

Custom labels and colors could be used.

# overriding color of PER, and changing ORG label to ORGANIZATION
settings = {"colors": {"PER": "lightgreen"}, "labels": {"ORG": "Organization"}}
html = Visualizer.render_ner_html(sentence, settings=settings)

image

If we want to integrate html into another web page, we could use wrap_page parameter to produce html without and tags wrapping it.

sentence_1 = Sentence(
    "Thibaut Pinot's challenge ended on Friday due to injury, and then Julian Alaphilippe saw his lead fall away."
)
tagger.predict(sentence_1)

sentence_2 = Sentence(
    "The BBC's Hugh Schofield in Paris reflects on 34 years of hurt."
)
tagger.predict(sentence_2)

html_1 = Visualizer.render_ner_html(sentence_1, wrap_page=False)
html_2 = Visualizer.render_ner_html(sentence_2, wrap_page=False)

# no wrapping by <html> and <body>
html = html_1 + " " + html_2

Now we can substitute html into any other html code.

result = HTML_PAGE.format(text=html)

image

@azeevg
Copy link
Contributor Author

azeevg commented Jul 29, 2019

Hi @alanakbik !
Do you understand why travis build fails? I tried black --check . locally, it works fine.

@alanakbik
Copy link
Collaborator

Looks like it's test_visual that is causing a black formatting error. I'll reformat!

@alanakbik
Copy link
Collaborator

@azeevg thanks for contributing this. Could you add a minimal full example code snippet on how to call this visualization method to this PR description? This will make it easier for other community members to use the new function!

@azeevg
Copy link
Contributor Author

azeevg commented Jul 30, 2019

@alanakbik , I updated my first comment.

@alanakbik
Copy link
Collaborator

@azeevg thanks for adding this - looks great!

@alanakbik
Copy link
Collaborator

👍

1 similar comment
@yosipk
Copy link
Collaborator

yosipk commented Jul 31, 2019

👍

@yosipk yosipk merged commit 8607177 into flairNLP:master Jul 31, 2019
@dcabo
Copy link

dcabo commented Aug 30, 2019

Seems like the rendering code was moved to a new location just before the 4.3 release, so the code snippet should now be something like:

from flair.visual.ner_html import render_ner_html

tagger = SequenceTagger.load('ner')

sentence = Sentence(
    "Thibaut Pinot's challenge ended on Friday due to injury, and then Julian Alaphilippe saw "
    "his lead fall away. The BBC's Hugh Schofield in Paris reflects on 34 years of hurt."
)

tagger.predict(sentence)
html = render_ner_html(sentence)

with open("sentence.html", "w") as writer:
    writer.write(html)

(I'd suggest a change to the 4.3 release notes, but not sure how to do that.)

@alanakbik
Copy link
Collaborator

@dcabo yes you're right, thanks for spotting this - just updated the release notes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants