Natural Language Processing with Spacy in Node.js | by Jeff Flynt | Towards Data Science

Many use cases exist for Natural Language Processing to be used in the browser with JavaScript but unfortunately not many tools exist in this space. To go beyond regular expressions and manipulation of strings, you’d want to leverage a good NLP framework. SpaCy which bills itself as Industrial-Strength Natural Language Processing is a popular toolkit but is a Python library. This creates a barrier to entry for those who only work with JavaScript.
SpaCy features neural network models, integrated word vectors, Multi-language support, tokenization, POS Tagging, sentence segmentation, dependency parsing, and entity recognition. SpaCy boasts a 92.6% accuracy rate and claims to be the fastest syntactic parser in the world.
An npm package called spacy-nlp aims to bridge this barrier by exposing SpaCy’s natural language processing capabilities to node.js. Some methods that are available include:
Syntax Parsing
Noun Parsing
Verb Parsing
Adjective Parsing
Named Entity Parsing
Date Parsing
Time Parsing
Some helper methods that are available are:
Splitting Large Text
Remove Duplicate Strings
Top n Words in a String
This package was originally created by Wah Loon Keng and I have since forked it to expand functionality. You can find the forked version here. Textalytic also uses this package to interface with Spacy and other Python natural language processing frameworks.
To get started, Python3 is recommended but Python2 can be used. You’d want to first install SpaCy via Python package index:
python3 -m pip install spacy
Next, you’ll want to install socketIO-client via Python. This package is used to facilitate the internal cross-language communication.
python3 -m pip install -U socketIO-client
By default this package uses the en_core_web_md language model from SpaCy. To download this, you’d want to run this command:
python3 -m spacy download en_core_web_md
Next, install the package via NPM:
npm install git+https://github.com/jeffreyflynt/spacy-nlp.git
In your JavaScript file, you’ll import the package and start the server with the Python client that exposes spacyIO. (Default port of 6466).
const nlp = require("spacy-nlp");const serverPromise = spacyNLP.server({ port: process.env.IOPORT });
Loading SpaCy into memory can take up to 15s.
The language model will stay in memory during the life-cycle of the application. (en_core_web_md — Size: 91 MB)
If you load balance with multiple instances of your app, you’d want to provide a unique port for each spacyNLP.server().
Say you want to extract all of the dates from this text:
The United States increased diplomatic, military, and economic pressures on the Soviet Union, at a time when the communist state was already suffering from economic stagnation. On 12 June 1982, a million protesters gathered in Central Park, New York to call for an end to the Cold War arms race and nuclear weapons in particular. In the mid-1980s, the new Soviet leader Mikhail Gorbachev introduced the liberalizing reforms of perestroika (“reorganization”, 1987) and glasnost (“openness”, c. 1985) and ended Soviet involvement in Afghanistan.
Input your text and select either to return words or just the total count. The result returned is:
12 June 1982
the mid-1980s
1987
1985
Or you may want to extract all of the named entities from this text:
The first phase of the Cold War began in the first two years after the end of the Second World War in 1945. The USSR consolidated its control over the states of the Eastern Bloc, while the United States began a strategy of global containment to challenge Soviet power, extending military and financial aid to the countries of Western Europe (for example, supporting the anti-communist side in the Greek Civil War) and creating the NATO alliance.
This returns:
the Cold War
the Second World War
USSR
the Eastern Bloc
the United States
Soviet
Western Europe
the Greek Civil War
NATO
You can get more fine-grained and extract all nouns in a text.
On 22 June 1941, the European Axis powers launched an invasion of the Soviet Union, opening the largest land theatre of war in history, which trapped the Axis, most crucially the German Wehrmacht, into a war of attrition. World War II (often abbreviated to WWII or WW2), also known as the Second World War, was a global war that lasted from 1939 to 1945.
This returns:
powers
invasion
land
theatre
war
history
war
attrition
war
More examples can be found on Github and more methods will be coming soon.
The above methods are starting points to an NLP pipeline. One could get creative and combine date/time parsing with named entity extraction to build a timeline of events with actors/players. Or in the alternative, aggregate mentions with a web scraper, the possibilities are endless.
I plan to add more functionality such as training models, text classification, sentence segmentation, and more so stay tuned. Any and all feedback is welcome.
Thanks for reading this article. If you have any questions or comments, I can be reached via Twitter @jeffrey_flynt.