简体   繁体   English

计算一个列表框中的重复项,然后将其添加到另一个列表框中

[英]Count duplicates on a listbox, then add it on another listbox

I'm trying to create a 'search engine' (sorta) for a simple library database system. 我正在尝试为简单的图书馆数据库系统创建一个“搜索引擎”(sorta)。

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. 然后将它们都迭代为两个For循环,以将每个拆分搜索字符串与拆分标记字符串进行比较。 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. 通常,这将导致同一本书的多个实例被添加到列表中,但是我已经在列表框中添加了防止重复的语句,这就是“ if not”语句。

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. 假设用户输入的搜索结果最多可重复两(2)个重复项。 The 2 duplicate items will be added on the other listbox first, then add the rest of the duplicated items at the end. 这2个重复项将首先添加到另一个列表框中,然后在末尾添加其余重复项。

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. 与“ ”,“ 喜剧 ”,“ 冒险 ”和“ 时光旅行 ”匹配的5本书 They're automatically added to the list . 它们会自动添加到列表中 (This means that these 5 books got duplicated 4 times each) (这意味着这5本书各重复了4次)

2 books with " Comedy ", " Adventure ", and " Time Travel " matches. 2本带有“ 喜剧 ”,“ 冒险 ”和“ 时间旅行 ”匹配的书籍 They're added to the list (This means that these 2 books got duplicated 3 times each) 它们已添加到列表中 (这意味着这两本书各被重复3次)

10 books with " Love ", " Comedy ", and " Adventure " matches. 10本书与“ ”,“ 喜剧 ”和“ 冒险 ”匹配。 They're also added to the list (10 books with 3 duplicates each) 它们也被添加到列表中 (每本10本书,每本重复3本)

25 books with " Love ", and " Comedy " matches. 25本书与“ ”和“ 喜剧 ”匹配。 But they're not added to the list (They were only duplicated 2 times) 但它们并未添加到列表中 (它们仅重复了2次)

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. 我试图寻找类似的东西,但只发现了有关计算重复项或只是删除重复项的结果。

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. 首先,我添加了一个名为“ Max”的变量以获取最多的重复项。 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. 之后,如果重复项的数量大于或等于“ Max”(最大)-1,我添加了if语句。就是这样,我能够在列表框中获得前两个重复项。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM