Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VBText
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 26-Mar-11 6:22am
555336631
Edited 11-Apr-11 8:42am
v7
Comments
Dalek Dave at 26-Mar-11 13:31pm
   
Excellent Question!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 

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
  Permalink  
v4
Comments
Dalek Dave at 26-Mar-11 13:32pm
   
Good answer, I did a slightly different approach, but similar in functionality.
willempipi at 26-Mar-11 14:16pm
   
Nice solution!
bunnyali2011 at 26-Mar-11 14:17pm
   
Can you implement it with an array for me? Im new in VB.
wimvr at 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
bunnyali2011 at 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?
wimvr at 27-Mar-11 5:15am
   
I'm sorry, I made this code with notepad (so I didn't thought about the 'count'. It must be .length :S)
bunnyali2011 at 27-Mar-11 7:07am
   
it says "Split" is not a member of an array, underline by the blue colour, only this, why? this sentence is underlined: IO.File.ReadAllLines("c:\test.txt").Split
wimvr at 27-Mar-11 8:20am
   
Maybe you van try IO.File.ReadAllLines("c:\test.txt") instead of IO.File.ReadAllLines("c:\test.txt").Split(Chr(10))... It was already split in lines. I'm sorry for my mistake :S
wimvr at 30-Mar-11 15:37pm
   
The length of the word may be 1 element longer (e.g. a "." or a "?") => If the word you want to search is to short (f.ex.: lo won't give results with love) or words that you want to search are longer, you also won't get a result. This is because you have this command: 0 <= words(y).Length - wordToSearch.Length And (words(y).Length - wordToSearch.Length) < 2 So the words you see in you're sentence must be of an equal length or 1 char longer than the word you are searching. If you set 0 to - 1, you'r word may be 1 item longer than the word you are searching but then you will have to rewrite the rest of you're code. Also 'leov' won't get you 'love' because this search-solution see to the places of the chars. (The right letter must be on the right place). And even 'loove' won't get you 'love' for the same reasons.
bunnyali2011 at 30-Mar-11 17:58pm
   
If I change 0 to -1, you mean everything on the code must change? By the way I'm new in VB since 2 weeks. So understanding these codes fluently is quite difficult for me. Can I have your email? I have 2 small problems related to search items, I can give you my email.
bunnyali2011 at 31-Mar-11 3:45am
   
The solution 3 seems good for me, but don't know where to insert the code to search the text file in my drive c and to display the results in the textbox. What you think of Soundex? I just read it 15 minutes ago.
wimvr at 31-Mar-11 14:29pm
   
Reply on 20 hrs ago: You will have to add this code: If words(y).length - 1 = z Then Exit For End if After: If words(y).Substring(z, 1) = wordToSearch.Substring(z, 1) Then counter += 1 End If But before: Next Reply on 10 hrs ago: Maybe that is what you need if you want to give 'sound-like' words. So you will have to set every word to the "Code" and then check every word... Maybe a fusion of the 2 is the best for you. (replace 'Search in text' into a solution with Soundex) You can find my mail on my "Professional Profile"-page.
bunnyali2011 at 31-Mar-11 14:49pm
   
Ok I will add it in some minutes. Im having a break. I will tell u then.
bunnyali2011 at 1-Apr-11 5:49am
   
Well, I changed it...here the code below and tell me if it is good because it is the same as results. 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 If words(y).length - 1 = z Then 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
bunnyali2011 at 1-Apr-11 8:08am
   
if the code above is good, then it is the same dear.. All i want is if the Search has not found any results, at least it could display all letters and sentence which are related.
wimvr at 1-Apr-11 12:33pm
   
It's not the same that I mean, you have not changed anything I think (or of what I see). But maybe, we will first wait at your lecturer...
bunnyali2011 at 1-Apr-11 8:10am
   
i will contact you soon by your email, because i have to discuss that with my lecturer about your codes etc... in fact many friends are the same boat, o doubt our lecturer must intervene now.
bunnyali2011 at 1-Apr-11 12:38pm
   
is in this problem lol
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
bunnyali2011 at 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 at 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.
bunnyali2011 at 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 at 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.
bunnyali2011 at 31-Mar-11 17:55pm
   
Ok thank you I will do it in the next hours then will tell you.
bunnyali2011 at 1-Apr-11 5:51am
   
oh no lol, i have VB 2005 and it is not opened! :( by the way, can you just paste the code here?
bunnyali2011 at 1-Apr-11 5:55am
   
i have read your codes dear... if you can, just tell me what you included in the form in terms of objects and i will rebuit the application again and paste the code in VB 2005.
Henry Minute at 1-Apr-11 6:31am
   
The Form has a TextBox (txtSearch), a ListView (lvwMatches) and a Button (btnGo). The ListView has it's View property set to Details. It has three columns with their Text set to 'Pos in File', 'Pos in Line' and 'Line' respectively.
bunnyali2011 at 1-Apr-11 7:18am
   
ok I made a new project, where a form Now where to insert the SoundexLibrary.vb?
Henry Minute at 1-Apr-11 7:27am
   
If you double click the Form to paste the code it will be put inside a Form_Load event handler. That is not what you want. Simply replace the whole of the code for your form with my code. To add the SoundexLibrary.vb: Right click on the name of your project in 'Solution Explorer'. From the context menu choose 'Add' then 'Class'. When the dialog appears change the name (at the bottom. it should read 'class1.vb') to SoundexLibrary.vb and hit return. When the code window opens for that class, paste in the code and you should be good to go.
bunnyali2011 at 1-Apr-11 7:37am
   
wait wait wait dear...i think its good...im testing it again then tell you...
bunnyali2011 at 1-Apr-11 7:54am
   
well, I tested it, for example if im searching the word 'Love' in my text
Henry Minute at 1-Apr-11 8:00am
   
The routine would find Luve, Lave, loVe etc. which is one of the thtings you indicated that you wanted. Still, speaking to your lecturer sounds like a good idea. If you need to contact me, just reply to this comment. We can talk about eMail at that time.
bunnyali2011 at 1-Apr-11 8:06am
   
yes...it would be great if we contact through email. By the way, I think i have not put some details in my explanation, well normally, if the search query cannot find a result, atleast it gives a messagebox saying "We have not found your results but we are giving you some suggestions", then it gives all letters and sentences related to the search
wimvr at 1-Apr-11 12:32pm
   
Maybe you can work with the same points system as said before, but then see that f.ex. the search 'Hello everyone' has to have 4 points to be complete (2 points for hello and 2 points for everyone if the word is equal, if the word is soundlike-equal, just give 1 point), searches with less points will be suggestions. But maybe it's easier to set it in a '.txt' file or a new solution then entering the suggestion here...
bunnyali2011 at 1-Apr-11 12:38pm
   
yes...have to show that to my lecturer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
wimvr at 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...

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,628
1 Tadit Dash 375
2 Manas Bhardwaj 319
3 Sergey Alexandrovich Kryukov 275
4 OriginalGriff 243
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,711
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 11 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid