Results 1 to 6 of 6

Thread: Word search

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    53

    Word search

    My program is going through each word in a story looking for names. I am using the following function to check against a string full of names (aaron, adam, ...) to see if the word is a name, but it is returning true for partial word matches such as "the" in "Theodore". What am I doing wrong?
    Code:
        Public Function WordExists(ByVal searchString As String, ByVal findString As String) As Boolean
            Dim returnValue As Boolean = False
            If System.Text.RegularExpressions.Regex.Matches(searchString, "\b" & findString & "\b").Count > 0 Then returnValue = True
            Return returnValue
        End Function
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If WordExists(NameList, TextBox2.Text) = True Then
                MsgBox("Yep!")
            Else
                MsgBox("Nope!")
            End If
        End Sub
    Thank you for your help!

  2. #2
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    457

    Re: Word search

    you need to check that the next character is a space or whatever separates the words.
    for example if you used "," then the word adam would be adam,
    George

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    53

    Re: Word search

    I had add a space, but then it didn't work at all.
    Code:
    If System.Text.RegularExpressions.Regex.Matches(searchString, "\b" & findString + " " & "\b").Count > 0 Then returnValue = True

  4. #4
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,909

    Re: Word search

    Actually, a better check would be to see if either the next character after the match is NOT a letter or number, or the word is not at the end of the text file.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2018
    Posts
    53

    Re: Word search

    I'll try that. The method I'm using is supposed to only match whole words. It doesn't make since that it's returning partial words. Maybe it's somewhere else in my actual code...

    Code:
        Private Sub AnalyzeBtn_Click(sender As Object, e As EventArgs) Handles AnalyzeBtn.Click
            For Each Line As String In Splitter(TextBox1.Text, " ", """", False)
                'skip words within quotation marks
                If Not Line.StartsWith("""") And Not Line.EndsWith("""") Then
    
                    For Each Word As String In Line.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                        Word = Trim(TrimPunctuation(Word))
                        'TextBox1.Selected = Word
                        I = Line.IndexOf(Word)
                        If WordExists(NameList, Word) = True Then 'Or WordExists(Dict, Word) = False
                            MsgBox(Word)
                            'ERROR: getting words that are not names.
                            Dim Result As MsgBoxResult = MsgBox("Is """ & Trim(Word) & """ a name?", MsgBoxStyle.YesNo)
                            If Result = MsgBoxResult.Yes Then
                                NewName = Word
                                'MsgBox("I=" & I)
                                'get the next quoted text
                                Dim Quote As String = GetNextQuote(I)
                                Result = MsgBox("Did " & NewName & " say: """ & Quote & """", MsgBoxStyle.YesNo)
                                If Result = MsgBoxResult.Yes Then
                                    'mark quoted text with tag (we will use a find & replace method to change the name to a voice later)
                                    Dim insertData As String = vbCrLf + "#" + NewName + " "
                                    Dim marker As String = Quote
                                    Dim insertPos As Integer = TextBox1.Text.IndexOf(marker)
                                    If insertPos >= 0 Then
                                        TextBox1.Text = TextBox1.Text.Insert(insertPos, insertData)
                                    End If
                                End If
                            End If
                        End If
                    Next
                End If
            Next
        End Sub
    I am having several other issues as well. What I'm trying to do is semi-automatically 'tag' the original text with # + name so that a program called DSpeech will read it using different voices for each name.

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,824

    Re: Word search

    try this for the word search, see if it helps

    Code:
     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim words() As String = IO.File.ReadAllText("E:\Book01.txt").Split({" "c, ","c, "\"c}, StringSplitOptions.RemoveEmptyEntries)
            Dim str As String = "adam the" '<-- the words to search for, with space between each word
            Dim strarr() As String
            strarr = str.Split(" "c)
            For Each s As String In strarr
                Dim sensitiveWordCount As Integer = words.Count(Function(word) word = s)
                Debug.Print("word :" & s & " " & sensitiveWordCount & " found")
                Debug.Print("-----------------")
            Next
           
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width