Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

An English Thesaurus for Windows Forms Applications

4.82/5 (13 votes)
28 Feb 2010CPOL4 min read 56.3K   991  
A thesaurus based on data from the WordNet database, easily implemented
ScrnShot_Thesaurus.jpg

Introduction

I've always believed that all text editors should have a spell checker and thesaurus. While there are several third party spell checkers available, I've not found a thesaurus that suited me. I wrote this one for my own text editor, and thought others might find it useful as well. At this point, it only does synonyms. A later version will also include antonyms. Note that both US and UK English are present in the data file. The current download is a bug-fix release. If you have the February 20th release, you should update with this one.

Background

This thesaurus, like many others, uses content from the WordNet database, provided free of charge by Princeton University (WordNet 3.0 Copyright 2006 by Princeton University. All rights reserved.)

Using the Code

The .zip file contains the VB project file, the data file in plain text format, and a documentation text file similar to what you'll see here. The thesaurus is quick and easy to implement and use from a text editing application. To use it, first follow these two steps:

  1. Add the "Thes_Eng.txt" file to My.Resources in your application
  2. Add "thesaurus.vb" to your project

There are 142,690 entries in its data file, all but a few pulled from the WordNet database. I have reformatted the data, so don't expect it to look like this if you access the WordNet database yourself. I won't say my format is better than WordNet's format, but it worked better for me this way. You can easily add to the data file by following the simple formatting below:

thesaurus_EntryDiagram.gif

From left to right: first is a word or phrase to be compared against the text you've selected in your text editor, followed by a pipe character. The pipe serves as the first delimiter, separating the word to check from the rest of the entry. Following the word will be a series of synonyms grouped by part-of-speech ("pos" from here on out). Each pos is enclosed in parenthesis - (noun), (verb), (adj), (adv). The pos and the synonyms are comma-delimited. The various pos's are contained in an array named "groups". When a meaning is selected from the Meaning listbox, the appropriate group is displayed in the Suggestions listbox.

The various groups of parts of speech are delimited by the colon character. If a match is found, the thesaurus breaks the entry down with the pos as element #0 in an array, followed by the next element. These two are added to the "Meanings" listbox. The second and following elements are added to the "Suggestions" listbox. Selecting a suggestion copies it to the "Replace With" textbox. You can remove this textbox if you wish; just be sure your code gets the replacement word from the selected item in the suggestions listbox.

The sample code below is an event handler for a menu item to run the thesaurus. Add it to your application that'll call the thesaurus:

VB.NET
Private Sub menu_Thes_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles menu_Thes.Click
    'This line sets the "Word or Phrase"
    'TextBox in the thesaurus that displays the word
    'you've chosen
    'It assumes your app has a TextBox named "myTextBox"
    'There mustn't be any leading or trailing spaces
    'in the text you send to the thesaurus.
    'The code below trims it for you
    thesaurus.txt_Term.Text = myTextBox.SelectedText.Trim

    'the variable "res" is type DialogResult
    res = thesaurus.ShowDialog()

    If res = Windows.Forms.DialogResult.OK Then
      myTextBox.SelectedText = thesaurus.lb_Sugg.SelectedItem.ToString
    End If
End Sub

The thesaurus uses the following objects:

VB.NET
Dim title As String = "Thesaurus"
Dim arr() As String
Dim myStr As String
Dim arr2() As String
Dim x As Integer
Dim groups() As String
Dim line As String
Dim found As Boolean
Dim thesPath As String = My.Application.Info.DirectoryPath & "\Thes_Eng.txt"

Here's the code for the thesaurus's Load event...

VB.NET
Private Sub thesaurus_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles MyBase.Load

    lb_Meanings.Items.Clear()
    lb_Sugg.Items.Clear()
    tList.Clear()
    found = False
    'check for existence of data file & create it if needed...

    If Not File.Exists(thesPath) Then
      Try
        File.WriteAllText(thesPath, My.Resources.Thes_Eng)
      Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
        Me.Close()
      End Try
    End If
    '------------------------------------------

    'search for the word...
    Try
      Dim sr As New StreamReader(thesPath)
      Do While sr.Peek() >= 0
        line = sr.ReadLine
        If line.StartsWith(txt_Term.Text.ToLower & "|") Then
          found = True
          Exit Do
        End If
      Loop

      sr.Dispose()
    Catch ex As Exception
      MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
      Me.Close()
    End Try

    'load the suggestions into the listboxes...
    If found = True Then
      arr = line.Split("|")
      groups = arr(1).Split(":")
      For g As Integer = 0 To groups.Length - 1
        Dim pos() As String = groups(g).Split(",")
        lb_Meanings.Items.Add(pos(0) & Chr(32) & pos(1))
      Next
      lb_Meanings.SelectedIndex = 0
    Else
      MsgBox("Not found in Thesaurus", MsgBoxStyle.Information, title)
      Me.Close()
    End If

  End Sub

When the thesaurus loads, it checks for the existence of the data file on disk. If it isn't found, it writes it from My.Settings to your application directory. The thesaurus then reads directly from disk to find the word in question. In my own tests, the elapsed time between clicking "Thesaurus" in my test app's menu and seeing the thesaurus onscreen is between 1 and 2 seconds. This is different from the original release. In the original, it loaded the entire data file into memory each time, which was a little slow.

This code updates the Suggestions listbox when you select an item from the Meanings listbox...

VB.NET
'update UI when a "meaning" listbox item is clicked...
Private Sub lb_Meanings_IndexChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs) _
            Handles lb_Meanings.SelectedIndexChanged

    Dim s() As String = groups(lb_Meanings.SelectedIndex).Split(",")
    lb_Sugg.Items.Clear()
    For l As Integer = 1 To s.Length - 1
      Dim tf As Boolean = Char.IsUpper(txt_Term.Text, 0)
      If tf Then
        myStr = s(l).Substring(0, 1).ToUpper & s(l).Substring(1)
        lb_Sugg.Items.Add(myStr)
      Else
        lb_Sugg.Items.Add(s(l))
      End If

    Next
    lb_Sugg.SelectedIndex = 0
  End Sub

This updates the "Replace With" text box when an item is selected in the Suggestion listbox:

VB.NET
Private Sub lb_Sugg_SelectedIndexChanged _
(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles lb_Sugg.SelectedIndexChanged
    txt_Replc.Text = lb_Sugg.SelectedItem.ToString.Trim
  End Sub

Once all is done, you can highlight a word or phrase in a text file, then call the thesaurus, and it'll give you synonyms for each part of speech where the word is found. I use it in my text editor from a context menu - nice and convenient. At present, it only does synonyms. A later version will also provide antonyms. I hope you find it useful, and all feedback is welcome.

History

  • First upload: Feb 20, 2010
  • Second upload: Feb 25, 2010 (bug fix and slight speed improvement)
  • Third upload: Feb 26, 2010 (contains the VB project, documentation and a data file required for the program to run)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)