0

I'm trying to create a 'search engine' (sorta) for a simple library database system.

As a brief description on what I really want to do, I want to create a search engine program that's connected to an access database, that searches for a "Tag" associated to a book.

The Tag, or Keyword, is an option that the searchbox can use. So instead of searching for a name, you can search for a tag in the book itself. The tag can contain anything - from the theme of the book, if it's a book or light novel, and so on,

Ex : I type on the search : "Horror", a ton of books with "Horror" on their tags will be listed on a listbox.

That's fine and all, but as a search engine, it's not accurate enough. With my current code, as soon as I type a tag that matches specific books, that's it. It won't be specific enough, if I type in "Horror adventure" for example, as it will still list the books that have horror in it.

What my code does is it splits the search input based on the space that it has. On the Tags itself, it also splits them by the comma that it has. So on the database, you'll see the tags as "Horror, Adventure, Romance" for example. Then they're both iterated into two For-loops, to compare each split search string to the split tag string. If it matches, it adds it in. The code for it is:

Dim comparenew As String
  Dim splittag As String
    For Each comparenew In search
      For Each splittag In compare
      If splittag.Contains(comparenew) = True And comparenew <> "" And comparenew.Count <> 1 Then
        If Not frmList.lstBooks.Items.Contains(dr("BookName").ToString()) Then 
          frmList.lstBooks.Items.Add(dr("BookName").ToString())
        End If
      End If
    Next
  Next
Next

Normally, this would result in multiple instances of the same book being added on the list, but I've already added the prevention of duplicates on the listbox, which is the "if not" statement.

But I want to utilize this duplication as a measure of accuracy. The more duplicates, the better search result.

Let's say a user inputs a search that resulted to a maximum of two (2) duplicated items. The 2 duplicate items will be added on the other listbox first, then add the rest of the duplicated items at the end.

If that's a bit confusing, that means that it's not always the 'most duplicated' item being added on the listbox, it will also add the lesser count duplicates as well.

Here's another example: Let's say that the user searched: "Love comedy adventure story, with time travel".

5 books with "Love", "Comedy", "Adventure", and "Time Travel" matches. They're automatically added to the list. (This means that these 5 books got duplicated 4 times each)

2 books with "Comedy", "Adventure", and "Time Travel" matches. They're added to the list (This means that these 2 books got duplicated 3 times each)

10 books with "Love", "Comedy", and "Adventure" matches. They're also added to the list (10 books with 3 duplicates each)

25 books with "Love", and "Comedy" matches. But they're not added to the list (They were only duplicated 2 times)

Hopefully everyone can understand the example, I think it was a bit clear. So as you can see, there's a 'level' wherein the results are added even though they're not the 'most duplicated' among the items. It's just one count below, but that's the problem on the search engine, as I don't know how to code it. I was thinking of counting the duplicates first, then put it on an Array for comparison. I'm not too sure how the code will work for this though.

Is there anyone that can help me with this? Let me know if you need the code, or make it clearer for you about this. I tried to look for something similar, but only found results about counting duplicates, or simply removing them.

4
  • Hello and welcome to Stack Overflow! It is very good that you've written a thorough explanation of what's going on and what you want the outcome to be, but it's a bit hard for us to rethink your explanation in a "code-ish" way as there are many ways to do this. Thus, this will likely result into more of a discussion, and since Stack Overflow is a Q&A site you should try to avoid discussions and just have a question that is easy to inspect (that is, almost always include code). Please see How to create a Minimal, Complete, and Verifiable example. Commented Sep 19, 2016 at 23:20
  • Another little note for the future: Please don't include the programming language(s) and such in the title (unless they really fit in there) as they're considered to be Tags, and therefore they belong in the "Tags" section. Please see this. Commented Sep 19, 2016 at 23:26
  • Thanks for the tips! I modified the post for review, but I seemed to have figured out the problem, so I'll just post this as a reference for others instead. Commented Sep 21, 2016 at 4:00
  • Glad to hear you found a solution! Good luck with your project! Commented Sep 21, 2016 at 5:33

1 Answer 1

1

OK so I think I had a mental block about this, as I was able to resolve my own problem a day after I posted this question.

Using this code here as the way to 'count' the number of duplicates on the listbox, I modified it a bit to suite my needs.

First, I added a variable called "Max" to get the highest number of duplicates. After that, I added the if statement if the number of duplicates is greater than or equal to "Max" - 1. And that was it, I was able to get the top two duplicates on the listbox. Here's the modified code for it:

    Try
        For Each nItem As String In lBox.Items
            If Not (lItems.ContainsKey(nItem)) Then 
                strCurrentItem = nItem
                For Each sItem As String In lBox.Items
                    If sItem.Equals(strCurrentItem) Then 
                        intCount += 1
                    End If
                Next
                If max < intCount Then
                    max = intCount
                End If
                lItems.Add(nItem, intCount)
                intCount = 0
                strCurrentItem = String.Empty
            End If
        Next

        For i As Integer = 0 To lItems.Count - 1
            If lItems.Values(i) >= max - 1 Then
                frmList.lstBooks.Items.Add(lItems.Keys(i).ToString)
            End If
        Next
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.