根据用户输入的文本创建“索引”报告 - VB 2010

时间:2012-08-02 20:33:32

标签: vb.net dictionary indexing richtextediting

我被指示了我称之为创建索引的内容。

基本上,用户需要能够在空文本框中找到一些文本。单击一个按钮,输出结果将显示按字母顺序排列的输入单词列表,以及它们出现的行号。

所以例如:

一条鱼
两条鱼
红鱼
蓝色的鱼。

黑鱼
蓝鱼
老鱼
新鱼。

这个有 一个小星星。

这个有一辆小车 说!真是太多了 还有鱼。

12,14,15
是16
黑色6
蓝4,7 CAR 14
鱼1,2,3,4,
有11,14
小12,14
很多15
新9
OF 16 OLD 8
ONE 1,11,11
RED 3
SAY 15
STAR 12
有16个 这11,14
两个2 什么15

本文与Java文档中的参考文献一起使用,用于创建索引,我一直遵循它,期望与我的相同,只是用另一种语言。

我现在正在写论文来制定一个算法,但我对我的努力感到有点沮丧!

还有一些要求:

出现的最大行数为4,因此即使10个不同的行出现一个单词,也只能引用4次

必须忽略语法所以包含!。,?的单词?必须删除 拼写的单词:HeLlO必须拼写为:hello

先谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

如果您需要按照它们在文本文件中出现的顺序显示单词,请从HashTable更改为SortedList。

    Dim hshResults As New Hashtable()

    Dim lstLinesOfText As List(Of String) = IO.File.ReadAllLines("C:\YourFile.txt").ToList()

    Dim intLineCursor As Integer = 0

    For Each strLine As String In lstLinesOfText

        Dim lstWords As List(Of String) = strLine.Split(" ").ToList()

        For Each strWord As String In lstWords

            ProcessWord(strWord, hshResults, intLineCursor)

        Next

        intLineCursor += 1

    Next

    Dim strOutput As String = String.Empty

    For Each o As DictionaryEntry In hshResults

        strOutput += CStr(o.Key) & " "

        Dim lstLinesWhereWordIsFount As List(Of Integer) = CType(o.Value, List(Of Integer))

        For Each i As Integer In lstLinesWhereWordIsFount

            strOutput += CStr(i) & " "

        Next

        'Some cleanup of extra spaces.
        strOutput = Trim(strOutput) & ControlChars.NewLine

    Next

Private Sub ProcessWord(ByVal strWord As String, ByRef hshResults As Hashtable, ByVal intLineIndex As Integer)

    Dim lstLinesWhereWordIsFound As List(Of Integer) = (From o As DictionaryEntry In hshResults _
                                                        Where CStr(o.Key) = strWord _
                                                        Select CType(o.Value, List(Of Integer))).FirstOrDefault()

    If lstLinesWhereWordIsFound Is Nothing Then

        'Add this word.
        Dim lstNewHashTableValue As New List(Of Integer)
        lstNewHashTableValue.Add(intLineIndex + 1) 'Indexes in the programming world start at 0.

        hshResults.Add(CObj(strWord), CObj(lstNewHashTableValue))

    Else

        'Add the line number for this word.
        If lstLinesWhereWordIsFound.Count < 5 Then

            'Make sure we're not duplicating a line number for this word.
            If (From i As Integer In lstLinesWhereWordIsFound _
                Where i = intLineIndex).Count = 0 Then

                lstLinesWhereWordIsFound.Add(intLineIndex + 1)

                hshResults(strWord) = CObj(lstLinesWhereWordIsFound)

            End If

        End If

    End If

End Sub

编辑:代码说明

首先,我实例化一个HashTable来存储它们被找到的单词和行。然后我将文本文件的每一行都放到List(of String)对象中。迭代文本文件的行,我使用Split方法将该行的每个单词放入另一个List(String)变量中。我通过一个方法(ProcessWord)发送该行的每个单词,该方法将适当地更新HashTable。最后,我遍历HashTable中的所有键/值对以生成所需的输出。 ProcessWord方法的逻辑是首先确定HashTable中是否已存在该单词。如果没有,请添加单词和行号。如果是,则确保行数不高于4的频率(根据您的问题中的要求),确保它没有将相同的行号放两次(如果一个单词在同一行中多个如果满足所有这些条件,请添加行号,然后更新HashTable。