|
-
May 24th, 2004, 11:08 AM
#1
Thread Starter
Fanatic Member
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.
-
May 24th, 2004, 11:23 AM
#2
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
-
May 24th, 2004, 11:28 AM
#3
Hyperactive Member
This code should produce the desired results:
VB Code:
Dim sMessage As String = "Hello world!" 'Sorry, I just had to :D
Dim sWords() As String
Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",")
Dim sGreet As String
Dim wIdx As Int32
Dim dIdx As Int32
sWords = sMessage.ToLower.Split(" ")
For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
If sWords(wIdx) = sDict(dIdx) Then
sGreet = sWords(wIdx)
Exit For
End If
Next
If sGreet.Length > 0 Then Exit For
Next
If sGreet.Length > 0 Then MessageBox.Show(sGreet)
-
May 24th, 2004, 11:41 AM
#4
Fanatic Member
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.
-
May 24th, 2004, 11:50 AM
#5
Thread Starter
Fanatic Member
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...
-
May 24th, 2004, 11:56 AM
#6
yay gay
It's because it's not pointing to any string
\m/  \m/
-
May 24th, 2004, 11:59 AM
#7
Thread Starter
Fanatic Member
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...
-
May 24th, 2004, 12:20 PM
#8
yay gay
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/
-
May 24th, 2004, 12:27 PM
#9
Hyperactive Member
you can resolve this by declaring the string as
VB Code:
Dim sMessage As String = ""
-
May 24th, 2004, 12:27 PM
#10
Hyperactive Member
Sorry,
VB Code:
Dim sGreet As String = ""
-
May 24th, 2004, 12:31 PM
#11
Hyperactive Member
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...
-
May 24th, 2004, 12:32 PM
#12
Thread Starter
Fanatic Member
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
-
May 24th, 2004, 12:33 PM
#13
Thread Starter
Fanatic Member
here's the ending code, with option strict on:
VB Code:
Dim sMessage As String = e.Message.Text
Dim sWords() As String
Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",".ToCharArray)
Dim sGreet As String = ""
Dim wIdx As Int32
Dim dIdx As Int32
sWords = sMessage.ToLower.Split(" ".ToCharArray)
For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
If sWords(wIdx) = sDict(dIdx) Then
sGreet = sWords(wIdx)
Exit For
End If
Next
If sGreet.Length > 0 Then Exit For
Next
If sGreet.Length > 0 Then MessageBox.Show(sGreet)
e.Message.Text is the message that gets received from the client
-
May 24th, 2004, 12:38 PM
#14
Hyperactive Member
Not bad, but I wouldn't bother with the additional variable:
VB Code:
Dim sWords() As String
Dim sDict() As String = "hi,hey,hiya,hello,howdy".Split(",".ToCharArray)
Dim sGreet As String = ""
Dim wIdx As Int32
Dim dIdx As Int32
sWords = e.Message.Text.ToLower.Split(" ".ToCharArray)
For wIdx = sWords.GetLowerBound(0) To sWords.GetUpperBound(0)
For dIdx = sDict.GetLowerBound(0) To sDict.GetUpperBound(0)
If sWords(wIdx) = sDict(dIdx) Then
sGreet = sWords(wIdx)
Exit For
End If
Next
If sGreet.Length > 0 Then Exit For
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|