Skip to content

Instantly share code, notes, and snippets.

@JungeAlexander
Last active October 25, 2020 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JungeAlexander/dd651e5079853b31f5db6738148f85b6 to your computer and use it in GitHub Desktop.
Save JungeAlexander/dd651e5079853b31f5db6738148f85b6 to your computer and use it in GitHub Desktop.
Simple NER visualization using displaCy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spacy\n",
"from spacy import displacy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.3.2'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spacy.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"doc_text = \"A common human skin tumour is caused by activating mutations in beta-catenin.\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"start_end_labels = [[15, 26, \"disease\"], [64, 76, \"protein\"]]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"colors = {\"DISEASE\": \"linear-gradient(90deg, #999999, #cccccc)\",\n",
" \"PROTEIN\": \"linear-gradient(90deg, #aa9cfc, #fc9ce7)\"\n",
" }\n",
"options = {\"ents\": [\"DISEASE\", \"PROTEIN\"], \"colors\": colors}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<span class=\"tex2jax_ignore\"><h2 style=\"margin: 0\">Displacy test from Jupyter</h2>\n",
"\n",
"<div class=\"entities\" style=\"line-height: 2.5; direction: ltr\">A common human \n",
"<mark class=\"entity\" style=\"background: linear-gradient(90deg, #999999, #cccccc); padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" skin tumour\n",
" <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\">disease</span>\n",
"</mark>\n",
" is caused by activating mutations in \n",
"<mark class=\"entity\" style=\"background: linear-gradient(90deg, #aa9cfc, #fc9ce7); padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;\">\n",
" beta-catenin\n",
" <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\">protein</span>\n",
"</mark>\n",
".</div></span>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ex = [{\"text\": doc_text,\n",
" \"ents\": [{\"start\": x[0], \"end\": x[1], \"label\": x[2]} for x in start_end_labels],\n",
" \"title\": \"Displacy test from Jupyter\"}]\n",
"html = displacy.render(ex, style=\"ent\", manual=True, options=options)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import spacy
from spacy import displacy
# In[2]:
spacy.__version__
# In[3]:
doc_text = "A common human skin tumour is caused by activating mutations in beta-catenin."
# In[4]:
start_end_labels = [[15, 26, "disease"], [64, 76, "protein"]]
# In[5]:
colors = {"DISEASE": "linear-gradient(90deg, #999999, #cccccc)",
"PROTEIN": "linear-gradient(90deg, #aa9cfc, #fc9ce7)"
}
options = {"ents": ["DISEASE", "PROTEIN"], "colors": colors}
# In[6]:
ex = [{"text": doc_text,
"ents": [{"start": x[0], "end": x[1], "label": x[2]} for x in start_end_labels],
"title": "Displacy test from Jupyter"}]
html = displacy.render(ex, style="ent", manual=True, options=options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment