-
Fuzzy String Search
I'm creating a search application that needs to have a fuzzy search, much like google. For example, if I type "Californea", I need my program to return"California" after doing a query on my DB for similarity.
If anyone knows of any already existing methods written, I'd appreciate if you could provide a link. If anyone has any suggestions or pseudocode, I'd appreciate those as well.
Ideally, I would like to be able to set the sensitivity. Just not sure where to start.... I already have my search function working properly for exact strings.
Thanks
-
Re: Fuzzy String Search
Post what you have, if you expect us to help. We can't read minds.
-
Re: Fuzzy String Search
right now I'm feeding my query into an array.
Code:
Dim ds As New DataSet
Dim dt As New DataTable
Dim dtCompare As New DataTable
Dim tmpCount As Integer
Dim tmpI As Integer
Dim i As Integer
Dim myAdapter As New MySqlDataAdapter
Dim myAdapterCompare As New MySqlDataAdapter
cmdNext.Visible = False
cmdPrevious.Visible = False
Arrays() 'sets up the control arrays
'clear all labels and images
For i = 0 To 5
lblTitle(i).Text = ""
lblAuthor(i).Text = ""
lblPrice(i).Text = ""
imgPath(i).ImageLocation = ""
Next
Dim strSearch As String = txtSearch.Text
Select Case (cmbSearchBy.Text)
Case "Title"
cmd.CommandText = "SELECT * FROM tblItems WHERE Title LIKE '%" & txtSearch.Text & "%';"
Case "Author"
cmd.CommandText = "SELECT * FROM tblItems WHERE Author LIKE '%" & txtSearch.Text & "%';"
Case "Genre"
cmd.CommandText = "SELECT * FROM tblItems WHERE Genre LIKE '%" & txtSearch.Text & "%';"
Case "ISBN"
cmd.CommandText = "SELECT * FROM tblItems WHERE ISBN LIKE '%" & txtSearch.Text & "%';"
End Select
myAdapter.SelectCommand = cmd
myAdapter.Fill(dt)
'This is used for the fuzzy search
Select Case (cmbSearchBy.Text)
Case "Title"
cmd.CommandText = "SELECT Title FROM tblItems;"
Case "Author"
cmd.CommandText = "SELECT Author FROM tblItems;"
Case "Genre"
cmd.CommandText = "SELECT Genre FROM tblItems;"
Case "ISBN"
cmd.CommandText = "SELECT ISBN FROM tblItems;"
End Select
myAdapter.SelectCommand = cmd
myAdapter.Fill(dtCompare)
Dim arrTitles = (From row In dt.AsEnumerable() Select row.Field(Of String)("title")).ToArray()
Dim arrAuthors = (From row In dt.AsEnumerable() Select row.Field(Of String)("author")).ToArray()
Dim arrPrices = (From row In dt.AsEnumerable() Select row.Field(Of Decimal)("price")).ToArray()
Dim arrPaths = (From row In dt.AsEnumerable() Select row.Field(Of String)("imagepath")).ToArray()
If UBound(arrTitles) > 5 Then
tmpCount = 0
For i = tmpI To tmpCount + 5
lblTitle(i).Text = arrTitles(i)
lblAuthor(i).Text = arrAuthors(i)
lblPrice(i).Text = arrPrices(i)
If File.Exists(arrPaths(i)) Then
imgPath(i).ImageLocation = arrPaths(i)
Else
imgPath(i).ImageLocation = "C:\Test\noimage.jpg"
End If
Next
tmpCount = tmpCount + 6
tmpI = tmpCount
cmdNext.Visible = True
cmdPrevious.Visible = True
ElseIf UBound(arrTitles) <= 5 And UBound(arrTitles) >= 0 Then
For i = 0 To UBound(arrTitles)
lblTitle(i).Text = arrTitles(i)
lblAuthor(i).Text = arrAuthors(i)
lblPrice(i).Text = arrPrices(i)
If File.Exists(arrPaths(i)) Then
imgPath(i).ImageLocation = arrPaths(i)
Else
imgPath(i).ImageLocation = "C:\Test\noimage.jpg"
End If
Next
ElseIf UBound(arrTitles) = -1 Then
MsgBox("No results")
End If
What I've begun to do is bring in all of the results that I'd like to compare against into an array. This way I don't have to send out a query every time I want to compare. I'm thinking I can some how give each comparison a score based on how many letters are in the proper position or something and then accepting or rejecting that result based on the final score.
-
Re: Fuzzy String Search
-
Re: Fuzzy String Search
You can use SOUNDEX function that is built-in SQL. Something like this Select SELECT * FROM tblItems WHERE SOUNDEX(TITLE) = SOUNDEX('Claifornea')
-
Re: Fuzzy String Search