All Projects → explosion → Displacy

explosion / Displacy

Licence: mit
💥 displaCy.js: An open-source NLP visualiser for the modern web

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Displacy

Practical Machine Learning With Python
Master the essential skills needed to recognize and solve complex real-world problems with Machine Learning and Deep Learning by leveraging the highly popular Python Machine Learning Eco-system.
Stars: ✭ 1,868 (+500.64%)
Mutual labels:  natural-language-processing, spacy
Cookiecutter Spacy Fastapi
Cookiecutter API for creating Custom Skills for Azure Search using Python and Docker
Stars: ✭ 179 (-42.44%)
Mutual labels:  natural-language-processing, spacy
Spacy Course
👩‍🏫 Advanced NLP with spaCy: A free online course
Stars: ✭ 1,920 (+517.36%)
Mutual labels:  natural-language-processing, spacy
Spacy Dev Resources
💫 Scripts, tools and resources for developing spaCy
Stars: ✭ 123 (-60.45%)
Mutual labels:  natural-language-processing, spacy
Spacy Api Docker
spaCy REST API, wrapped in a Docker container.
Stars: ✭ 222 (-28.62%)
Mutual labels:  natural-language-processing, spacy
Textacy
NLP, before and after spaCy
Stars: ✭ 1,849 (+494.53%)
Mutual labels:  natural-language-processing, spacy
Cleannlp
R package providing annotators and a normalized data model for natural language processing
Stars: ✭ 174 (-44.05%)
Mutual labels:  natural-language-processing, spacy
Tageditor
🏖TagEditor - Annotation tool for spaCy
Stars: ✭ 92 (-70.42%)
Mutual labels:  natural-language-processing, spacy
Spacy Lookup
Named Entity Recognition based on dictionaries
Stars: ✭ 212 (-31.83%)
Mutual labels:  natural-language-processing, spacy
Thinc
🔮 A refreshing functional take on deep learning, compatible with your favorite libraries
Stars: ✭ 2,422 (+678.78%)
Mutual labels:  natural-language-processing, spacy
Spacy Js
🎀 JavaScript API for spaCy with Python REST API
Stars: ✭ 123 (-60.45%)
Mutual labels:  natural-language-processing, spacy
Spacy Services
💫 REST microservices for various spaCy-related tasks
Stars: ✭ 230 (-26.05%)
Mutual labels:  natural-language-processing, spacy
Pytextrank
Python implementation of TextRank for phrase extraction and summarization of text documents
Stars: ✭ 1,675 (+438.59%)
Mutual labels:  natural-language-processing, spacy
Rasa
💬 Open source machine learning framework to automate text- and voice-based conversations: NLU, dialogue management, connect to Slack, Facebook, and more - Create chatbots and voice assistants
Stars: ✭ 13,219 (+4150.48%)
Mutual labels:  natural-language-processing, spacy
Jupyterlab Prodigy
🧬 A JupyterLab extension for annotating data with Prodigy
Stars: ✭ 97 (-68.81%)
Mutual labels:  natural-language-processing, spacy
Spacymoji
💙 Emoji handling and meta data for spaCy with custom extension attributes
Stars: ✭ 151 (-51.45%)
Mutual labels:  natural-language-processing, spacy
Python nlp tutorial
This repository provides everything to get started with Python for Text Mining / Natural Language Processing (NLP)
Stars: ✭ 72 (-76.85%)
Mutual labels:  natural-language-processing, spacy
Spacy Graphql
🤹‍♀️ Query spaCy's linguistic annotations using GraphQL
Stars: ✭ 81 (-73.95%)
Mutual labels:  natural-language-processing, spacy
Displacy Ent
💥 displaCy-ent.js: An open-source named entity visualiser for the modern web
Stars: ✭ 191 (-38.59%)
Mutual labels:  natural-language-processing, spacy
Prodigy Recipes
🍳 Recipes for the Prodigy, our fully scriptable annotation tool
Stars: ✭ 229 (-26.37%)
Mutual labels:  natural-language-processing, spacy

displaCy.js: An open-source NLP visualiser for the modern web

⚠️ As of v2.0.0, the displaCy visualizers are now integrated into the core library. See here for more details on how to visualize a Doc object from within spaCy. We're also working on a new suite of tools for serving and testing spaCy models. The code of the standalone visualizers will still be available on GitHub, just not actively maintained.

displaCy.js is a modern and service-independent visualisation library. We hope this makes it easy to compare different services, and explore your own in-house models. If you're using spaCy's syntactic parser, displaCy should be part of your regular workflow. Because spaCy's parser is statistical, it's often hard to predict how it will analyse a given sentence. Using displaCy, you can simply try and see. You can also share the page for discussion with your team, or save the SVG to use elsewhere. If you're developing your own model, you can run the service yourself — it's 100% open source.

To read more about displaCy.js, check out the blog post.

npm

Run the demo

This demo is implemented in Jade (aka Pug), an extensible templating language that compiles to HTML, and is built or served by Harp. To serve it locally on http://localhost:9000, simply run:

sudo npm install --global harp
git clone https://github.com/explosion/displacy
cd displacy
harp server

Or simply install it from npm:

npm install displacy-demo

The demo is written in ECMAScript 6. For full, cross-browser compatibility, make sure to use a compiler like Babel. For more info, see this compatibility table.

Using displacy.js

To use displaCy in your project, download displacy.js from GitHub or via npm:

npm install displacy

Then include the file and initialize a new instance specifying the API and settings:

const displacy = new displaCy('http://localhost:8000', {
    container: '#displacy',
    format: 'spacy',
    distance: 300,
    offsetX: 100
});

Our service that produces the input data is open source, too. You can find it at spacy-services.

The following settings are available:

Setting Description Default
container element to draw displaCy in, can be any query selector #displacy
format format used to generate parse ('spacy' or 'google') 'spacy'
defaultText text used if displaCy is run without text specified 'Hello World.'
defaultModel model used if displaCy is run without model specified 'en'
collapsePunct collapse punctuation true
collapsePhrase collapse phrases true
distance distance between words in px 300
offsetX spacing on left side of the SVG in px 50
arrowSpacing spacing between arrows in px to avoid overlaps 20
arrowWidth width of arrow head in px 10
arrowStroke width of arc in px 2
wordSpacing spacing between words and arcs in px 50
font font face for all text 'inherit'
color text color, HEX, RGB or color names '#000000'
bg background color, HEX, RGB or color names '#ffffff'
onStart function to be executed on start of server request false
onSuccess callback function to be executed on successful server response false
onError function to be executed if request fails false

Visualising a Parse

The parse() method renders a parse generated by spaCy as an SVG in the container.

displacy.parse('This is a sentence.', 'en', {
    collapsePunct: false,
    collapsePhrase: false,
    color: '#ffffff',
    bg: '#000000'
});

The visual settings specified here override the global settings. The available settings are collapsePunct, collapsePhrase, font, color and bg.

Rendering a Parse Manually

Alternatively, you can use render() to manually render a JSON-formatted set of arcs and words:

const parse = {
    arcs: [
        { dir: 'right', end: 1, label: 'npadvmod', start: 0 }
    ],
    words: [
        { tag: 'UH', text: 'Hello' },
        { tag: 'NNP', text: 'World.' }
    ]
};

displacy.render(parse, {
    color: '#ff0000'
});

The visual settings specified here override the global settings. The available settings are font, color and bg.

Converting output from other formats and adding your own

By default, displaCy expects spaCy's JSON output in the following style:

{
    "arcs": [
        { "dir": "left", "end": 4, "label": "nsubj", "start": 0 }
    ],

    "words": [
        { "tag": "NNS", "text": "Robots" }
    ]
}

If format is set to 'google', the API response is converted from Google's format. To add your own conversion rules, add a new case to handleConversion():

handleConversion(parse) {
    switch(this.format) {
        case 'spacy': return parse; break;
        case 'google': return({ words: ..., arcs: ... }); break;
        case 'your_format': return({ words: ..., arcs: ... }); break;
        default: return parse;
    }
}

You can now initialize displaCy with format: 'your_format'.

Changing the theme and colours

You can find the current theme settings in /assets/css/_displacy-theme.sass. All elements contained in the SVG output come with tags and data attributes and can be styled flexibly using CSS. By default, the currentColor of the element is used for colouring, meaning only need to change the color property in CSS.

The following classes are available:

Class name Description
.displacy-word word text
.displacy-tag POS tag
.displacy-token container of word and POS tag
.displacy-arc arrow arc (without label or arrow head)
.displacy-label relation type (arrow label)
.displacy-arrowhead arrow head
.displacy-arrow container of arc, label and arrow head

Additionally, you can use these attributes as attribute selectors:

Attribute Value On Element
data-tag POS tag value .displacy-token, .displacy-word, .displacy-tag
data-label relation type value .displacy-arrow, .displacy-arc, .displacy-arrowhead, .displacy-label
data-dir direction of arrow .displacy-arrow, .displacy-arc, .displacy-arrowhead, .displacy-label

Using a combination of those selectors and some basic CSS logic, you can create pretty powerful templates to style the elements based on their role and function in the parse. Here are a few examples:

/* Format all words in 12px Helvetica and grey */

.displacy-word {
    font: 12px Helvetica, Arial, sans-serif;
    color: grey;
}


/* Make all noun phrases (tags that start with "NN") green */

.displacy-tag[data-tag^="NN"] {
    color: green;
}


/* Make all right arrows red and hide their labels */

.displacy-arc[data-dir="right"],
.displacy-arrowhead[data-dir="right"] {
    color: red;
}

.displacy-label[data-dir="right"] {
    display: none;
}


/* Hide all tags for verbs (tags that start with "VB") that are NOT the base form ("VB") */

.displacy-tag[data-tag^="VB"]:not([data-tag="VB"]) {
    display: none;
}


/* Only display tags if word is hovered (with smooth transition) */

.displacy-tag {
    opacity: 0;
    transition: opacity 0.25s ease;
}

.displacy-word:hover + .displacy-tag {
    opacity: 1;
}

Adding custom data attributes

displaCy lets you define custom attributes via the JSON representation of the parse on both words and arcs:

"words": [
    {
        "tag": "NNS",
        "text": "Robots",
        "data": [
            [ "custom", "your value here" ],
            [ "example", "example text here" ]
        ]
    }
]

Custom attributes are added as data attributes prefixed with data-, so their names shouldn't contain spaces or special characters. If added to words, the data attributes are attached to the token (.displacy-token), if added to arcs, they're attached to the arrow (.displacy-arrow):

<text class="displacy-token" data-custom="your value here" data-example="example text here">...</text>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].