CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2006
    Posts
    21

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Fuzzy String Search

    Post what you have, if you expect us to help. We can't read minds.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2006
    Posts
    21

    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.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Fuzzy String Search


  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

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

  6. #6
    Join Date
    Dec 2007
    Posts
    16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured