Search Results for

    Show / Hide Table of Contents

    WordNet Tutorial

    In this tutorial we will learn how to use the WordNetEngine available in Syn.WordNet library to import WordNet's lexical database files and work with SynSets.

    Creating a Console application

    To create a Console application in Visual Studio following the steps given below.

    • Start Visual Studio 2019 or above
    • Select Create a new project and choose Console App
    • Click Next
    • Name the project WordNetConsole and choose OK.

    Importing WordNet

    Especially for Oscova, we released Syn.WordNet and made it available in NuGet. To import Syn.WordNet to your project follow the steps below:

    • In Visual Studio, click on Tools, point to NuGet Package Manager
    • Select Package Manager Console
    • Type Install-Package Syn.WordNet

    Syn.WordNet library is a standalone library and has no external dependencies.

    Loading Data

    Once Syn.WordNet library is successfully referenced in your project. You'll need to load WordNet database files for each part of speech. There are 4 main parts of speech for which we'll need to load data.

    • Adjective
    • Adverb
    • Noun
    • Verb
    Tip

    To download WordNet source files please visit the official WordNet download page

    Once download, extract all the files in the bin/debug/wordnet of your project. For Syn.WordNet we just need the following files.

    Data

    • data.adj
    • data.adv
    • data.noun
    • data.verb

    Index

    • index.adj
    • index.adv
    • index.noun
    • index.verb

    As Syn.WordNet is a portable class library the mechanism of loading WordNet files doesn't use traditional approach instead we use StreamReader to load the specific files into the WordNetEngine.

    Loading Data and Index files

    Within the static void Main(string[] args) method add the following code. The code assumes that you've already copied the aforementioned WordNet files in the bin/debug/wordnet folder.

    var directory =  Directory.GetCurrentDirectory();
    
    var wordNet = new WordNetEngine();
    
    Console.WriteLine("Loading database...");
    wordNet.LoadFromDirectory(directory);
    Console.WriteLine("Load completed.");
    

    Or (Depending on your platform)

    var directory =  Directory.GetCurrentDirectory();
    
    var wordNet = new WordNetEngine();
    
    wordNet.AddDataSource(new StreamReader(Path.Combine(directory, "data.adj")), PartOfSpeech.Adjective);
    wordNet.AddDataSource(new StreamReader(Path.Combine(directory, "data.adv")), PartOfSpeech.Adverb);
    wordNet.AddDataSource(new StreamReader(Path.Combine(directory, "data.noun")), PartOfSpeech.Noun);
    wordNet.AddDataSource(new StreamReader(Path.Combine(directory, "data.verb")), PartOfSpeech.Verb);
    
    wordNet.AddIndexSource(new StreamReader(Path.Combine(directory, "index.adj")), PartOfSpeech.Adjective);
    wordNet.AddIndexSource(new StreamReader(Path.Combine(directory, "index.adv")), PartOfSpeech.Adverb);
    wordNet.AddIndexSource(new StreamReader(Path.Combine(directory, "index.noun")), PartOfSpeech.Noun);
    wordNet.AddIndexSource(new StreamReader(Path.Combine(directory, "index.verb")), PartOfSpeech.Verb);
    
    Console.WriteLine("Loading database...");
    wordNet.Load();
    Console.WriteLine("Load completed.");
    

    And that's it you have now successfully instantiated WordNetEngine.

    Getting SynSets

    After the WordNetEngine.Load() method has finished generating SynSets you can retrieve synsets of known words in just a few lines of code. In the aforementioned code append the following lines of code.

    while (true)
    {
        Console.WriteLine("\nType first word");
    
        var word = Console.ReadLine();
        var synSetList = wordNet.GetSynSets(word);
    
        if (synSetList.Count == 0) Console.WriteLine($"No SynSet found for '{word}'");
    
        foreach (var synSet in synSetList)
        {
            var words = string.Join(", ", synSet.Words);
    
            Console.WriteLine($"\nWords: {words}");
            Console.WriteLine($"POS: {synSet.PartOfSpeech}");
            Console.WriteLine($"Gloss: {synSet.Gloss}");
        }
    }
    

    You may now run the application by clicking on the Start button or by pressing F5 in Visual Studio. After the Load completed message type Car. You will now be presented with all the SynSets of the word Car along with their definitions and parts of speech.

    Theme
    Back to top Copyright © 2011-2020 Synthetic Intelligence Network