在VBA中查找类似的发声文本

Ed *_*Lee 15 ms-access vba soundex

我的经理告诉我,有一种方法可以评估拼写不同但名称与发音方式相似的名称.理想情况下,我们希望能够评估用户输入的搜索名称并返回完全匹配以及"类似的声音"名称.他称这个过程为"Soundits",但我在Google上找不到任何信息.

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

Law*_*ley 19

好问题!你的问题包括这个想法本身的一个很好的例子.

有一个叫拉塞尔的算法的Soundex算法,在许多应用中的标准技术,通过语音评估的名字,而不是实际的拼写.在这个问题中,SounditsSoundex的名字相似![编辑:刚跑了Soundex.Soundits = S532和Soundex = S532.

关于Soundex:

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

  1. 第一封信具有重要意义
  2. 许多辅音听起来很相似
  3. 辅音比元音更能影响发音

一个警告:Soundex是为名字而设计的.越短越好.随着名称越来越长,Soundex变得越来越不可靠.

资源:

  1. 以下是使用VBA进行Access的示例.
  2. Ken Getz和Mike Gilbert 在VBA开发者手册第2版中对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
Run Code Online (Sandbox Code Playgroud)

Levenshtein距离

比较弦的另一种方法是获得Levenshtein距离.以下是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
Run Code Online (Sandbox Code Playgroud)