How to Use WordNet in Node.js Applications

  Programming

If you are creating an application that needs some form of natural language processing, you’ll probably want to use WordNet. If you aren’t familiar with it already, you can think of WordNet as a huge database of English words, their synonyms, definitions, and parts of speech. In this tutorial, I’m going to show you how to create a simple English-English dictionary application, that queries the WordNet database via wordnet-db and natural, which are popular Node.js packages.

Project Setup

Create a new directory for the project, and enter it.

mkdir MyDictionary && cd MyDictionary

Next, generate a package.json file with default options.

npm init --yes

You can now add the latest versions of wordnet-db and natural as dependencies of the project.

npm install wordnet-db --save
npm install natural --save

I’ll be using wordnet-db v3.1.2 and natural v0.4.0.

Use WordNet

Create a new file called index.js and open it using your favorite code editor. We’ll be writing all our code inside this file.

In this tutorial, we are creating a command-line dictionary application where the user passes an English word as a command-line argument. So, let us get that word first from the process.argv array.

const word = process.argv[1];

Next, initialize natural and wordnet using the following code:

const natural = require('natural');
const wordnet = new natural.WordNet();

At this point, we can start using WordNet. For now, let’s say our dictionary application displays only the definition of the word, along with a few synonyms. To implement that, we need to fetch the def and synonyms properties of the word’s synset. To do so, we can use the lookup() method. Here’s how:

wordnet.lookup(word, function(details) {
    console.log("Definition: " + details[0].def);
    console.log("Synonyms: " + details[0].synonyms);
});

Note that a word can have multiple definitions. That’s the reason why details is an array.

Let us now enhance our app to display a list all the definitions available, along with all the synonyms. To do so, we can loop through the details array using forEach. Let’s also indicate the part of speech of the word this time, using the pos field, because the definition differs based on it.

WordNet has example sentences for most words. Therefore, let’s also display all the example sentences associated with the input word. To do so, we use the exp field, which is an array.

Accordingly, update your code so that it looks like this:

wordnet.lookup(word, function(details) {
    details.forEach(function(detail) {
        console.log("------------------------------");
        console.log("Definition: " + detail.def);
        console.log("Synonyms: " + detail.synonyms);
        console.log("POS: " + detail.pos);

        // Display examples, if available
        detail.exp.forEach(function(example) {
            console.log("EXAMPLE: " + example);
        });
        console.log("------------------------------");
    });
});

Our dictionary application is ready. So, go ahead and run the app. Make sure you pass a valid word to it.

node index.js fashionable

You should see output that looks like this:

------------------------------
Definition: popular and considered appealing or fashionable at the time  
Synonyms: fashionable,in_fashion
POS: s
------------------------------
------------------------------
Definition: having elegance or taste or refinement in manners or dress
Synonyms: stylish,fashionable
POS: a
EXAMPLE: a little less posh but every bit as stylish as Lord Peter Wimsey
EXAMPLE: the stylish resort of Gstadd
------------------------------
------------------------------
Definition: being or in accordance with current social fashions
Synonyms: fashionable,stylish
POS: a
EXAMPLE: fashionable clothing
EXAMPLE: the fashionable side of town
EXAMPLE: a fashionable cafe
------------------------------

Conclusion

You now know how to use WordNet in a Node.js application. Although we created only a very simple dictionary today, I’m sure you can use the skills you learned to easily create more complex applications.

If you found this article useful, please share it with your friends and colleagues!