Results 1 to 14 of 14

Thread: Cases for a String [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Cases for a String [RESOLVED]

    Hi,

    It looks like im on a kinda post madness episode here, but I have a lot of questions for the program i am making.

    Here's the situation.

    When I receive some text from my client, it gets put into a string.

    Now, I need some pretty hefty checking in the string. As the text is being written by a user, the server needs to be able to interprete what the user is trying to say to some degree.

    Now, first of all i would need to know if you could do this with cases... could you give the same response to several different strings?

    Because like "Hello" "Hiya" "Hey" " Hi" have all the same meaning, and i would like to give one response.

    So could something like
    Case "Hello" Or "Hiya" Or "Hey" Or "Hi"
    work? Or how would i go about doing that? I have never used Cases before, and i am going from the examples here.

    And also, after i have that sorted, there may be a bit more of a prob. The string isn't going to be EXACTLY one of those responses.

    The user could have put a space before " Hello" like that, or after it, so doing a definite lookup won't help. The user might even say "Hello mate" instead, so i need it to recognize a single word within a sentence like this.

    At later times, i will make it more advanced but i need help at this stage on just getting the fundamentals down.

    So I'll really appreciate any insight on this, as i am kind of stuck of what to do to make it react like i want it.

    Thanks very much!
    Last edited by LITHIA; May 24th, 2004 at 12:34 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    1. Use Trim to remove trailing and leading spaces.

    2. Create a list of "hello" type words, like

    hiya,
    yo,
    heyya,
    hey,
    hi,
    hiiiiii
    OMMMGGG!!!!!!!!! HIIIII!!!!!!!! LOLOLO!!!!!!!!!!1111111111111

    and when you receive the string from the client, run a loop in this list, checking for each word in the string. Stop when you do.

    3. Use Instr() to check if a word exists in a string.



    HTH

  3. #3
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    This code should produce the desired results:
    VB Code:
    1. Dim sMessage As String = "Hello world!" 'Sorry, I just had to :D
    2.         Dim sWords() As String
    3.         Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",")
    4.         Dim sGreet As String
    5.         Dim wIdx As Int32
    6.         Dim dIdx As Int32
    7.         sWords = sMessage.ToLower.Split(" ")
    8.         For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
    9.             For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
    10.                 If sWords(wIdx) = sDict(dIdx) Then
    11.                     sGreet = sWords(wIdx)
    12.                     Exit For
    13.                 End If
    14.             Next
    15.             If sGreet.Length > 0 Then Exit For
    16.         Next
    17.         If sGreet.Length > 0 Then MessageBox.Show(sGreet)

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746
    Probably want to start reading up on regular expressions (System.Text.RegularExpressions). I think that's going to be your answer for anything more complex than "hello" variations.

  5. #5

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    thanks for the quick replies!

    Cyberhawke, your code is great - it does exactly what i want it to.

    but theres one prob. When the message received is not equal to one of the things in sDict, it gives an error:

    Object reference not set to an instance of an object.

    Now... thats really strange... it highlights this:
    If sGreet.Length > 0
    from line
    If sGreet.Length > 0 Then Exit For

    Do you have any idea why it would do that? I'm totally confused...

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    It's because it's not pointing to any string
    \m/\m/

  7. #7

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Originally posted by PT Exorcist
    It's because it's not pointing to any string
    but why...? i thought it was pointing at:

    Dim sGreet As String

    but its acting as though its not... so why isn't it? I don't understand this... it will be obvious when you say lol, i just don't see it right now...

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    It's pointing to it but unlike vb6, when you declare a string it doesnt point to "" by default. If you look with attention to your code you will see that in some situations your string will point to nothing
    \m/\m/

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    you can resolve this by declaring the string as
    VB Code:
    1. Dim sMessage As String = ""

  10. #10
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Sorry,
    VB Code:
    1. Dim sGreet As String = ""

  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Btw: Briantcva

    I was doing some investegation into your suggestion about regular expressions. I was not familiar with this until I read your posting. It is definitely worth some research, I believe it has the potential to be a better solution than the one I posted.

    Thanks for the info...

  12. #12

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    ah yeah!!! thanks! works now, i should have realised about the 'nothing' as i even put my cursor over it in debug and it said nothing... lol, i just didnt click.

    thanks very much for the help guys, and thanks for the code cyberhawke

  13. #13

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    here's the ending code, with option strict on:

    VB Code:
    1. Dim sMessage As String = e.Message.Text
    2.         Dim sWords() As String
    3.         Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",".ToCharArray)
    4.         Dim sGreet As String = ""
    5.         Dim wIdx As Int32
    6.         Dim dIdx As Int32
    7.         sWords = sMessage.ToLower.Split(" ".ToCharArray)
    8.         For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
    9.             For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
    10.                 If sWords(wIdx) = sDict(dIdx) Then
    11.                     sGreet = sWords(wIdx)
    12.                     Exit For
    13.                 End If
    14.             Next
    15.             If sGreet.Length > 0 Then Exit For
    16.         Next
    17.         If sGreet.Length > 0 Then MessageBox.Show(sGreet)
    e.Message.Text is the message that gets received from the client

  14. #14
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Not bad, but I wouldn't bother with the additional variable:
    VB Code:
    1. Dim sWords() As String
    2.         Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",".ToCharArray)
    3.         Dim sGreet As String = ""
    4.         Dim wIdx As Int32
    5.         Dim dIdx As Int32
    6.         sWords = e.Message.Text.ToLower.Split(" ".ToCharArray)
    7.         For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
    8.             For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
    9.                 If sWords(wIdx) = sDict(dIdx) Then
    10.                     sGreet = sWords(wIdx)
    11.                     Exit For
    12.                 End If
    13.             Next
    14.             If sGreet.Length > 0 Then Exit For
    15.         Next
    16.         If sGreet.Length > 0 Then MessageBox.Show(sGreet)

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