65.9K
CodeProject 正在变化。 阅读更多。
Home

面向 Windows Forms 应用程序的英文同义词词典

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (12投票s)

2010年2月20日

CPOL

4分钟阅读

viewsIcon

57226

downloadIcon

1011

基于 WordNet 数据库的同义词词典,易于实现

ScrnShot_Thesaurus.jpg

引言

我一直认为所有文本编辑器都应该有拼写检查器和同义词词典。虽然有几个第三方拼写检查器可用,但我还没有找到一个适合我的同义词词典。我为自己的文本编辑器编写了这个,并认为其他人可能也会觉得它有用。目前,它只提供同义词。稍后的版本还将包括反义词。请注意,数据文件中同时包含美国英语和英国英语。当前的下载是一个 bug 修复版本。如果您有 2 月 20 日的版本,您应该使用此版本进行更新。

背景

这个同义词词典,与其他许多同义词词典一样,使用来自 WordNet 数据库的内容,由普林斯顿大学免费提供(WordNet 3.0 版权所有 2006 年,普林斯顿大学。保留所有权利。)

Using the Code

.zip 文件包含 VB 项目文件、纯文本格式的数据文件以及类似于您在此处看到的文档文本文件。该同义词词典可以快速、轻松地从文本编辑应用程序中实现和使用。要使用它,请首先按照以下两个步骤操作

  1. 将 "Thes_Eng.txt" 文件添加到您应用程序的 My.Resources
  2. 将 "thesaurus.vb" 添加到您的项目中

其数据文件中包含 142,690 个条目,除了少数几个,所有条目都来自 WordNet 数据库。我已经重新格式化了数据,所以如果你自己访问 WordNet 数据库,不要指望它看起来像这样。我不会说我的格式比 WordNet 的格式更好,但它更适合我。您可以通过遵循下面的简单格式轻松地添加到数据文件中

thesaurus_EntryDiagram.gif

从左到右:首先是要与您在文本编辑器中选择的文本进行比较的单词或短语,后跟一个管道字符。管道作为第一个分隔符,将要检查的单词与条目的其余部分分隔开。在单词之后将是一系列按词性分组的同义词(从这里开始为 "pos")。每个 pos 都包含在括号中 - (名词),(动词),(形容词),(副词)。pos 和同义词用逗号分隔。各种 pos 包含在名为 "groups" 的数组中。从“含义”列表框中选择一个含义时,相应的组将显示在“建议”列表框中。

各种词性组由冒号字符分隔。如果找到匹配项,同义词词典将以 pos 作为数组中的元素 #0 来分解条目,然后是下一个元素。这二者被添加到 "含义" 列表框中。第二个及后续元素被添加到 "建议" 列表框中。选择一个建议会将它复制到 "替换为" 文本框中。如果您愿意,您可以删除此文本框;只需确保您的代码从建议列表框中选定的项目中获取替换词即可。

下面的示例代码是运行同义词词典的菜单项的事件处理程序。将它添加到将调用同义词词典的应用程序中

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

同义词词典使用以下对象

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"

这是同义词词典的 Load 事件的代码...

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

当同义词词典加载时,它会检查磁盘上是否存在数据文件。如果找不到,它会将其从 My.Settings 写入您的应用程序目录。然后,同义词词典直接从磁盘读取以查找相关单词。在我自己的测试中,在我的测试应用程序的菜单中单击“同义词词典”到看到同义词词典出现在屏幕上的时间大约在 1 到 2 秒之间。这与最初的版本不同。在最初的版本中,它每次都将整个数据文件加载到内存中,这有点慢。

当您从“含义”列表框中选择一个项目时,此代码会更新“建议”列表框...

'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

当在“建议”列表框中选择一个项目时,这会更新“替换为”文本框

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

完成所有操作后,您可以在文本文件中突出显示一个单词或短语,然后调用同义词词典,它将为您提供该单词出现的每个词性的同义词。我在我的文本编辑器中使用它,从上下文菜单 - 方便又实用。目前,它只提供同义词。稍后的版本还将提供反义词。希望您觉得它有用,并欢迎所有反馈。

历史

  • 首次上传:2010 年 2 月 20 日
  • 第二次上传:2010 年 2 月 25 日(bug 修复和轻微的速度改进)
  • 第三次上传:2010 年 2 月 26 日(包含 VB 项目、文档和程序运行所需的数据文件)
© . All rights reserved.