ms-access - 在 VBA 中查找发音相似的文本

标签 ms-access vba soundex

我的经理告诉我,有一种方法可以评估拼写不同但发音相似的名字。理想情况下,我们希望能够评估用户输入的搜索名称并返回完全匹配以及“听起来相似”的名称。他将该过程称为“Soundits”,但我在 Google 上找不到任何信息。

这个存在吗?有谁知道它是否可用于 VBA (Access)?

最佳答案

问得好!你的问题包含了这个想法本身的一个很好的例子。

有一种名为 Russell Soundex 算法的算法,它是许多应用程序中的标准技术,它通过语音而不是实际拼写来评估名称。在这个问题中,SounditsSoundex 是听起来相似的名字! [编辑:刚刚运行了 Soundex。 Soundits=S532 和 Soundex=S532。]

关于 Soundex:

Soundex 算法基于英语的特征,例如:

  1. 第一个字母具有重要意义
  2. 许多辅音听起来相似
  3. 辅音比元音对发音的影响更大

警告:Soundex 是为名称而设计的。越短越好。随着名称变长,Soundex 的可靠性就会降低。

资源:

  1. 下面是使用 VBA 来表示 Access 的示例。
  2. Ken Getz 和 Mike Gilbert 的VBA 开发人员手册,第二版中有一篇关于 Soundex 的文章。
  3. 有大量有关 Soundex 和 Soundex2 等其他变体的信息(搜索“Soundex”和“VBA”)。

代码示例:

下面是通过快速网络搜索找到的一些 VBA 代码,它实现了 Soundex 算法的变体。

Option Compare Database
Option Explicit

Public Function Soundex(varText As Variant) As Variant
On Error GoTo Err_Handler
    Dim strSource As String
    Dim strOut As String
    Dim strValue As String
    Dim strPriorValue As String
    Dim lngPos As Long

    If Not IsError(varText) Then
        strSource = Trim$(Nz(varText, vbNullString))
        If strSource <> vbNullString Then
            strOut = Left$(strSource, 1&)
            strPriorValue = SoundexValue(strOut)
            lngPos = 2&

            Do
                strValue = SoundexValue(Mid$(strSource, lngPos, 1&))
                If ((strValue <> strPriorValue) And (strValue <> vbNullString)) Or (strValue = "0") Then
                    strOut = strOut & strValue
                    strPriorValue = strValue
                End If
                lngPos = lngPos + 1&
            Loop Until Len(strOut) >= 4&
        End If
    End If

    If strOut <> vbNullString Then
        Soundex = strOut
    Else
        Soundex = Null
    End If

Exit_Handler:
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Soundex()"
    Resume Exit_Handler
End Function
Private Function SoundexValue(strChar As String) As String
    Select Case strChar
    Case "B", "F", "P", "V"
        SoundexValue = "1"
    Case "C", "G", "J", "K", "Q", "S", "X", "Z"
        SoundexValue = "2"
    Case "D", "T"
        SoundexValue = "3"
    Case "L"
        SoundexValue = "4"
    Case "M", "N"
        SoundexValue = "5"
    Case "R"
        SoundexValue = "6"
    Case vbNullString
        SoundexValue = "0"
    Case Else
        'Return nothing for "A", "E", "H", "I", "O", "U", "W", "Y", non-alpha.
    End Select
End Function

编辑距离

比较字符串的另一种方法是获取 Levenshtein distance 。这是VBA给出的例子,取自LessThanDot Wiki :

Function LevenshteinDistance(word1, word2)

Dim s As Variant
Dim t As Variant
Dim d As Variant
Dim m, n
Dim i, j, k
Dim a(2), r
Dim cost

   m = Len(word1)
   n = Len(word2)

   ''This is the only way to use
   ''variables to dimension an array
   ReDim s(m)
   ReDim t(n)
   ReDim d(m, n)

   For i = 1 To m
       s(i) = Mid(word1, i, 1)
   Next

   For i = 1 To n
       t(i) = Mid(word2, i, 1)
   Next

   For i = 0 To m
       d(i, 0) = i
   Next

   For j = 0 To n
       d(0, j) = j
   Next


   For i = 1 To m
       For j = 1 To n

           If s(i) = t(j) Then
               cost = 0
           Else
               cost = 1
           End If

           a(0) = d(i - 1, j) + 1             '' deletion
           a(1) = d(i, j - 1) + 1             '' insertion
           a(2) = d(i - 1, j - 1) + cost      '' substitution

           r = a(0)

           For k = 1 To UBound(a)
               If a(k) < r Then r = a(k)
           Next

           d(i, j) = r

       Next

   Next

   LevenshteinDistance = d(m, n)

End Function

关于ms-access - 在 VBA 中查找发音相似的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1607690/

相关文章:

database - 从现有的 Access 数据库创建模型图

ms-access - 字符串整数转换?编译器错误消息: CS1502

mysql - 将交叉表查询从 ms-access 转换为 mysql

vba - 如何更改插入 MS Word 文档中的注释的背景颜色?

php - 葡萄牙语 (pt_PT) 的本地化(双)变音位

vba - 执行查询 DoCmd.RunSQL 时出现错误 3340 查询 ' ' 已损坏

vba - Virtual Protect 的 64 位声明是否正确?

ms-access - 更改 Access 中链接表中的表名称

sql - LINQ to SQL SOUNDEX - 可能吗?

c# - 在 .Net 中为整个句子实现 soundex