0

我需要对字符的音译进行模糊搜索,例如:

我有一个 ASP.NET 应用程序数据库,它有一个包含西班牙语单词列表(200,000 个条目)的表,我还有一个带有输入字段的页面。关键是我不懂西班牙语,也不知道如何用西班牙语拼写搜索词,但我知道它的发音。因此,我在文本框中输入了搜索词,例如“beautiful”,但在录制错误时 - “prekieso”,我需要从数据库中获取正确的版本:“precioso”。

如何实施?换句话说,我需要类似于谷歌建议的东西......

4

2 回答 2

0

存储过程/函数,算法计算距离Levenshtein:

USE [**dbname**]
GO
/****** Object:  UserDefinedFunction [dbo].[levenshtein]    Script Date: 05/27/2013 17:54:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[levenshtein](@left varchar(100), @right varchar(100)) 
   returns int
as
BEGIN
   DECLARE @difference int, @lenRight int, @lenLeft int, @leftIndex int, @rightIndex int, @left_char char(1), @right_char char(1), @compareLength int 
   SET @lenLeft = LEN(@left) 
   SET @lenRight = LEN(@right) 
   SET @difference = 0  
   If @lenLeft = 0 
   BEGIN
      SET @difference = @lenRight GOTO done 
   END
   If @lenRight = 0 
   BEGIN
      SET @difference = @lenLeft 
      GOTO done 
   END 
   GOTO comparison  

   comparison: 
   IF (@lenLeft >= @lenRight) 
      SET @compareLength = @lenLeft 
   Else
      SET @compareLength = @lenRight  
   SET @rightIndex = 1 
   SET @leftIndex = 1 
   WHILE @leftIndex <= @compareLength 
   BEGIN
      SET @left_char = substring(@left, @leftIndex, 1)
      SET @right_char = substring(@right, @rightIndex, 1)
      IF @left_char <> @right_char 
      BEGIN -- Would an insertion make them re-align? 
         IF(@left_char = substring(@right, @rightIndex+1, 1))    
            SET @rightIndex = @rightIndex + 1 
         -- Would an deletion make them re-align? 
         ELSE
            IF(substring(@left, @leftIndex+1, 1) = @right_char)
               SET @leftIndex = @leftIndex + 1
               SET @difference = @difference + 1 
      END
      SET @leftIndex = @leftIndex + 1 
      SET @rightIndex = @rightIndex + 1 
   END 
   GOTO done  

   done: 
      RETURN @difference 
END

调用:

select
 dbo.edit_distance('Fuzzy String Match','fuzzy string match'),
 dbo.edit_distance('fuzzy','fuzy'),
 dbo.edit_distance('Fuzzy String Match','fuzy string match'),
 dbo.edit_distance('levenshtein distance sql','levenshtein sql server'),
 dbo.edit_distance('distance','server')

或者:

SELECT [Name]
FR OM [tempdb].[dbo].[Names]
WHERE dbo.edit_distance([Name],'bozhestvennia') <= 3
于 2013-05-27T22:04:27.917 回答
0

我认为您需要的是这样的拼写检查功能:http: //www.codeproject.com/KB/string/netspell.aspx

类似于 google 的功能,但它要先进得多,实施起来并不容易: Google“你的意思是什么?” 算法工作?

希望这有帮助。

于 2013-05-27T14:27:56.177 回答