Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
See more:
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!
Posted
Updated 11-Apr-11 8:42am
v7
Comments
Dalek Dave 26-Mar-11 13:31pm    
Excellent Question!

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



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:


VB
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
 
Share this answer
 
v4
Comments
Dalek Dave 26-Mar-11 13:32pm    
Good answer, I did a slightly different approach, but similar in functionality.
willempipi 26-Mar-11 14:16pm    
Nice solution!
555336 26-Mar-11 14:17pm    
Can you implement it with an array for me? Im new in VB.
wimvr 27-Mar-11 6:42am    
I have added it to the solution ;-)
Good luck with it...

I have correct a bit so that the length of a word may be 1 char longer because a "." or "," is also be seen as a letter
555336 26-Mar-11 14:20pm    
I'm getting the blue underline on the section where you stated "line.count" and "words.count". sorry im new in VB and it it is telling me that they are not a member in array. what should i insert so that the blue underline is removed?
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.
 
Share this answer
 
Comments
wimvr 26-Mar-11 13:40pm    
In the idea of Dalek Dave ("Either write a file with every possible mis-spelling of every word.")

Maybe you can use NetSpell, that's an open source spellingcorrector. With a bit of work, you can get the corrections of the word with this dll...
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.
 
Share this answer
 
Comments
555336 31-Mar-11 3:44am    
Hi Henry thank for posting, as I said I'm new in VB since 2 weeks, by the way can you help me how and where to insert the code where to search from my text file in drive C? and also where to insert the code to appear the results on a text box?
Henry Minute 31-Mar-11 7:15am    
If you download the code from the article and run it then examine it you should be able to figure those things out.

If you have tried that and are still stuck, please ask again.
555336 31-Mar-11 9:27am    
Yes I tried the code by pasting directly and no errors found but i dont know where to insert a code or modify the current one to search the text in my text file and where to insert the code to display the results in my textbox
Henry Minute 31-Mar-11 17:01pm    
Sorry for the delay in replying, I've been busy.

I have made a little demo project, you can download it from http://dl.dropbox.com/u/17020164/SoundexSearch.zip.

It is VS2008 and you should be able to get the idea of how soundex works from it.

Let me know if there are any problems.
555336 31-Mar-11 17:55pm    
Ok thank you I will do it in the next hours then will tell you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900