搜索基本视觉帮助! [英] Search in visual basic HELP!

查看:110
本文介绍了搜索基本视觉帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个巨大的问题,即用户必须从文本文件中搜索一些文本.

首先,我已经成功实现了一个代码,其中用户将在文本框中键入任何单词或句子,然后单击搜索按钮,文本文件中与单词或句子关联的所有行将显示在另一个文本框中.好!

I have a massive problem in a form where a user will have to search some text from a text file.

Firstly, I have successfully implemented a code where the user will type any word or sentence on a textbox, then click on a search button, all lines in the text file associated with the words or sentence will be presented on another textbox. Good!

推荐答案

如果wordToSearch只是一个单词,则可以测试文件中每个单词的每个字母.
假设每个单词的长度必须与您搜索到的单词的长度相等,并且可能有2个错误的字符,那么这可能行得通吗?

If the wordToSearch is only one word, you can test each letter in each word of the file.
Say that every word has to have an equal length as the word where you search to and may have 2 chars that are wrong, than maybe this can work?

Dim wordToSearch As String = txtSearch.Text.ToLower
Dim lines() As String = IO.File.ReadAllLines("c:\test.txt")
For x As integer = 0 To lines.length - 1
    Dim words() As string = lines(x).split(chr10)
    For y As integer = 0 To words.length - 1
        If words(y).length = wordToSearch.length Then
            Dim counter As integer = 0
            For z As integer = 0 To wordToSearch.length - 1
                If words(y).substring(z,1) = wordToSearch.substring(z,1) Then
                    counter += 1
                End If
                If counter > wordToSearch.length - 2
                    txtNew.Text += lines(x) + vbNewLine
                End If
            Next
        End If
    Next
Next




我知道它将比您的代码慢,并且也不能包含多个单词.
对于一个以上的单词,您可以为每个单词做到这一点,然后计算每个句子的分数?
(您可以将其设置为数组,然后在数据网格中显示.)

希望对您有帮助吗?

最好的问候,

Wim


__________________________________
最后添加:
如您所要求-具有数组的工具:





I know that it will be slower than your code and it also can''t have multiple words.
For more than 1 word, you can do it for every word and then count the points of each sentence?
(You can set it in an array and then show in a datagrind.)

I hope that this can help you?

Best regards,

Wim


__________________________________
Last add:
Like you have requested - the implement with an array:


Dim wordsSearch() As String = txtSearch.Text.ToLower.Split(" ")
Dim lines() As String = IO.File.ReadAllLines("c:\test.txt")
Dim PointsArray(lines.Length - 1) As Integer
Dim wordToSearch As String

'Set all values to 0
For count2 As Integer = 0 To PointsArray.Length - 1
    PointsArray(count2) = 0
Next

'Search in text
For x As Integer = 0 To lines.Length - 1
    Dim words() As String = lines(x).Split(" ")
    For a As Integer = 0 To wordsSearch.Length - 1
        wordToSearch = wordsSearch(a)
        For y As Integer = 0 To words.Length - 1
            If 0 <= words(y).Length - wordToSearch.Length And (words(y).Length - wordToSearch.Length) < 2 Then 'The length of the word may be 1 element 1 longer (e.g. a "." or a "?")
                Dim counter As Integer = 0
                For z As Integer = 0 To wordToSearch.Length - 1

                    If words(y).Substring(z, 1) = wordToSearch.Substring(z, 1) Then
                        counter += 1
                    End If
                Next
                If counter > wordToSearch.Length - 2 Then
                    PointsArray(x) += 1
                End If
            End If
        Next
    Next
Next

'Make a list with all the lines
Dim ArrayLines(lines.Length - 1) As Integer

For x As Integer = 0 To lines.Length - 1
    ArrayLines(x) = x
Next

'Sort: 'Implemented from wiki - http://nl.wikipedia.org/wiki/Bubblesort
Dim i As Integer
Dim v As Boolean
Dim j As Integer
Dim temp As Integer
Dim temp2 As Integer
Dim count As Integer = PointsArray.Length - 1

i = 0
v = True
Do While i < count - 1 And v = True
    v = False
    j = 0
    Do While j <= count - i - 1
        If PointsArray(j) > PointsArray(j + 1) Then
            temp = PointsArray(j)
            PointsArray(j) = PointsArray(j + 1)
            PointsArray(j + 1) = temp
            'Sort also the array with the lines....
            temp2 = ArrayLines(j)
            ArrayLines(j) = ArrayLines(j + 1)
            ArrayLines(j + 1) = temp2
            v = True
        End If
        j = j + 1
    Loop
    i = i + 1
Loop

'Now, we can show the result (Say, only the first 5)
Dim results As Integer = PointsArray.Length - 6
If results < 0 Then
    results = 0
End If
For i = PointsArray.Length - 1 To results Step -1
    If PointsArray(i) = 0 Then 'If the line has no "points"
        Exit For
    End If
    txtNew.Text += "Line nr. " + ArrayLines(i).ToString + " :" + lines(ArrayLines(i)) + vbNewLine
Next


要么写一个文件,每个单词的拼写可能都不正确.
(不可行).
还是...

选择单词后,对字符进行值计数(a = 1 b = 2等),然后搜索文本中每个单词的值计数.
您可以允许总差异为52(2 * Z值),并提供适合的单词作为替代.

将一个单词长度为+/- 1(例如,允许f = ph)作为一个过滤器,并且可能的最小字母数为4.
(假设大多数人不会拼错四个字母的单词).

这是一个好问题,我会对此表示赞成.
Either write a file with every possible mis-spelling of every word.
(Not feasible).
Or...

When the word is selected, do a value count of the characters (a=1 b=2 etc), and search a value count of each word in the text.
You can allow a total difference of, say 52 (2 * Z value) and offer the words that fit as an alternative.

Do a word length of +/- 1 (to allow for f = ph for example) as one filter, and possibly a minimum letter count of, say, 4.
(assuming most people could not mis-spell a four letter word).

It is a good question, I will give an upvote for it.


作为先前答案的一种可能替代方法,您可以考虑使用Soundex搜索.

这样可以解决拼写错误或大小写错误的问题. The Code Project上有几篇关于此的文章. C#和VB.NET中的Soundex实现 [
As a possible alternative to the previous answers, you might consider using a Soundex search.

This takes care of minor misspelling or incorrect capitalization. There are several articles about this here on The Code Project. Soundex Implementation in C# and VB.NET[^] is just one of them.

Well worth considering.


这篇关于搜索基本视觉帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆