Transliteration and fuzzy search, like Google suggestions

拜拜、爱过 提交于 2019-12-11 18:58:05

问题


I need to do a fuzzy search with transliteration of the characters, for example:

I have an ASP.NET application, database, which has a table with a list of Spanish words (200,000 entries), I also have a page with an input field. The point is that I do not know Spanish, and I do not know how to spell a search word in Spanish, but I know how it sounds. Therefore, in the text box I enter the search word, such as "beautiful", but in the recording err - "prekieso", and I need to get from the database got the correct version: "precioso".

How can this be implemented? In other words, I need something similar to Google suggestions...


回答1:


I think what you need here is a spell checking functionality like this one: http://www.codeproject.com/KB/string/netspell.aspx

A google like functionality it's much more advanced though and will not be easy to implement: How does the Google "Did you mean?" Algorithm work?

hope this help.




回答2:


The stored procedure / function, the algorithm calculates the distance 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

invoking:

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')

or:

SELECT [Name]
FR OM [tempdb].[dbo].[Names]
WHERE dbo.edit_distance([Name],'bozhestvennia') <= 3


来源:https://stackoverflow.com/questions/16771754/transliteration-and-fuzzy-search-like-google-suggestions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!