How to Use displaCy.js: An Open-Source NLP Visualizer for the Modern Web

Mar 24, 2023 | Programming

Welcome to the world of natural language processing (NLP)! If you’ve ever wanted to visualize the parsing of sentences in a clean and interactive way, displaCy.js is your go-to solution. As of version 2.0.0, displaCy visualizers are integrated into the spaCy core library, ensuring better accessibility for all your NLP needs. In this article, we will walk you through how to set up and use displaCy.js, troubleshoot common issues, and make the most out of its functionalities.

Setting Up displaCy.js

Let’s begin with the basics. To run displaCy.js locally, you need to set up your development environment. Here’s how:

1. Install Harp

  • Run the following command to install Harp globally:
  • sudo npm install --global harp

2. Clone the Repository

  • Clone the displaCy repository from GitHub:
  • git clone https://github.com/explosion/displacy

3. Start the Server

  • Navigate into the displaCy directory and start the server:
  • cd displacy
    harp server

4. Access the Demo

Open your browser and go to http://localhost:9000 to see the displaCy demo in action!

Using displaCy.js in Your Projects

To integrate displaCy.js into your existing projects, follow these steps:

1. Install displaCy.js

  • You can install displaCy.js via npm:
  • npm install displacy

2. Initialize displaCy

Once installed, include the displaCy script in your HTML and initialize it using the following JavaScript code:

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

How displaCy.js Works: An Analogy

Imagine you are an artist trying to illustrate the elements of a sentence — each word is a unique color, and the relationships between them resemble the lines connecting points in a constellation. Just as you would use a canvas to create your artwork, displaCy.js acts as your canvas for visualizing the intricate relationships within syntax.

When you render a parse with displaCy.js, it takes predefined shapes (the words) and connects them with colored arcs representing the grammatical relationships (like stars with lines). This visual representation helps demystify the sentence structure, making it easier to comprehend how words interact.

Troubleshooting Tips

While setting up or using displaCy.js, you may encounter a few challenges. Here are some troubleshooting ideas:

  • Problem: The local server won’t start.
  • Solution: Ensure that you have installed Harp correctly and that you are in the correct directory before starting the server.
  • Problem: The visualization does not display.
  • Solution: Make sure the container specified in your initialization code matches an existing element in your HTML.
  • Problem: Errors related to ES6 compatibility.
  • Solution: Use Babel to transpile your ES6 code into ES5. Check out the compatibility table for more details.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Customizations and Advanced Features

You can easily modify the visual appearance by changing themes, font styles, and colors using CSS. For example, if you want to make all noun phrases green, you can style your elements with specific CSS classes defined in the SVG output.

Conclusion

DisplaCy.js is a powerful tool that allows you to visualize NLP processes effectively, enabling both beginners and experts to better understand textual structures. Dive into the fascinating world of NLP today!

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox