|
-
Sep 1st, 2005, 12:31 AM
#1
Thread Starter
Junior Member
Help!
I have until the end of today to find a way to stop my word counter counting (word)..(word). as 2 sentences when it isnt! i want it to count one full stop as a sentence but not two or three!! HELP!
VB Code:
Private Sub cmdsentences_Click()
Dim Sentence As String, Char As String 'setting the data types
Dim Count As Integer, I As Integer 'setting the data types
Count = 0 'initialise the count to 0
Sentence = Trim(txtInput.Text) 'set the end of the sentence.
If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
Char = Mid(Sentence, I, 1) 'characters in sentence
If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
Count = Count + 1 'add one to count
End If
Next I
Count = Count 'count remains the same
End If
lblCountsentences.Caption = Count 'where to display the result
End Sub
-
Sep 1st, 2005, 01:08 AM
#2
Re: Help!
Do your sentances only end in ".", "!" and "?" ?
-
Sep 1st, 2005, 01:16 AM
#3
Thread Starter
Junior Member
-
Sep 1st, 2005, 01:54 AM
#4
Re: Help!
Try something along these lines:
VB Code:
Sentence = Replace$(Sentence, "?", ".")
Sentence = Replace$(Sentence, "!", ".")
Do Until Instr(Sentence, "..") = 0
Sentence = Replace$(Sentence, "..", ".")
Loop
Add this code just before your For/Next loop starts. Good luck.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 01:55 AM
#5
Thread Starter
Junior Member
Re: Help!
can i leave it exactly as you put it or do i need to change it?
-
Sep 1st, 2005, 01:57 AM
#6
Fanatic Member
Re: Help!
Try this.
Basically I've used a flag to show if we are inside a sentence or not.
If we are in a sentence, then a .! or ? is the end of that sentence, so we count it and then we are NOT in a sentence, so if we get any more we don't add to the sentence count.
We detect that we are back in a sentence by checkign the character to see if it is a letter or number. (You may want to add other characters to that line, depending on how you want to define a sentence.)
VB Code:
Private Sub cmdsentences_Click()
Dim InsideASentence As Boolean
Dim Sentence As String, Char As String 'setting the data types
Dim Count As Integer, I As Integer 'setting the data types
Count = 0 'initialise the count to 0
Sentence = Trim(txtInput.Text) 'set the end of the sentence.
If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
Char = Mid(Sentence, I, 1) 'characters in sentence
If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
InsideASentence = True
End If
If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
If InsideASentence Then
Count = Count + 1 'add one to count
InsideASentence = False
End If
End If
Next I
Count = Count 'count remains the same
End If
lblCountsentences.Caption = Count 'where to display the result
End Sub
You can take out count=count if you like. It does nothing.
Last edited by BrianHawley; Sep 1st, 2005 at 02:14 AM.
Reason: typo
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Sep 1st, 2005, 01:57 AM
#7
Thread Starter
Junior Member
Re: Help!
it doesnt like the replace$ thing....o_O what should i do?? "sub or function not defined" <--thats what it says. its a compile error.
-
Sep 1st, 2005, 01:59 AM
#8
Re: Help!
It should work exactly as it is
VB Code:
Private Sub cmdsentences_Click()
Dim Sentence As String, Char As String 'setting the data types
Dim Count As Integer, I As Integer 'setting the data types
Count = 0 'initialise the count to 0
Sentence = Trim(txtInput.Text) 'set the end of the sentence.
If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
Sentence = Replace$(Sentence, "?", ".")
Sentence = Replace$(Sentence, "!", ".")
Do Until Instr(Sentence, "..") = 0
Sentence = Replace$(Sentence, "..", ".")
Loop
For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
Char = Mid(Sentence, I, 1) 'characters in sentence
If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
Count = Count + 1 'add one to count
End If
Next I
[b]Don't need to do this=>[/b] Count = Count 'count remains the same
End If
lblCountsentences.Caption = Count 'where to display the result
End Sub
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 01:59 AM
#9
Thread Starter
Junior Member
Re: Help!
haha, seems like it should work but the insideasentence comes up with "variable not found" should i be replacing it with something?
-
Sep 1st, 2005, 02:00 AM
#10
Re: Help!
 Originally Posted by spazzirifick
it doesnt like the replace$ thing....o_O what should i do?? "sub or function not defined" <--thats what it says. its a compile error.
What version of VB are you using??
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 02:01 AM
#11
Thread Starter
Junior Member
Re: Help!
i put that in, but it doesnt like the replace$ thing. i'm not sure why?
-
Sep 1st, 2005, 02:01 AM
#12
Thread Starter
Junior Member
-
Sep 1st, 2005, 02:04 AM
#13
Re: Help!
I've got a feeling that VB 5 doesn't have the Replace$() function
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 02:06 AM
#14
Thread Starter
Junior Member
Re: Help!
nooo it doesnt!! oh no!! what should i put :O
-
Sep 1st, 2005, 02:08 AM
#15
Fanatic Member
Re: Help!
 Originally Posted by spazzirifick
haha, seems like it should work but the insideasentence comes up with "variable not found" should i be replacing it with something?
Did you Dim it?
Anyway pnishes solution is better (unless you don't have replace).
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Sep 1st, 2005, 02:09 AM
#16
Thread Starter
Junior Member
Re: Help!
i dont have a replace...so what should i do with dim??????
-
Sep 1st, 2005, 02:17 AM
#17
Fanatic Member
Re: Help!
VB Code:
Private Sub cmdsentences_Click()
Dim InsideASentence As Boolean
Dim Sentence As String, Char As String 'setting the data types
Dim Count As Integer, I As Integer 'setting the data types
Count = 0 'initialise the count to 0
Sentence = Trim(txtInput.Text) 'set the end of the sentence.
If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
Char = Mid(Sentence, I, 1) 'characters in sentence
If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
InsideASentence = True
End If
If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
If InsideASentence Then
Count = Count + 1 'add one to count
InsideASentence = False
End If
End If
Next I
Count = Count 'count remains the same
End If
lblCountsentences.Caption = Count 'where to display the result
End Sub
This works. I've just tested it.
If in doubt, delete everything in your click event and post this instead.
The 'replace' method is better, but won't work if you don't have 'replace'. (Could write your own replace of course)
Brian
(Fighting with the RightToLeft bugs in VS 2005)
-
Sep 1st, 2005, 03:04 AM
#18
Re: Help!
I deleted the Replace code I posted as it was pretty buggy. Here's some code I found on the VB-Helper website. Hope this helps:
VB Code:
' Replace all occurrances of from_str with to_str.
Public Function ReplaceText(ByVal txt As String, ByVal _
from_str As String, ByVal to_str As String) As String
Dim result As String
Dim from_len As Integer
Dim pos As Integer
from_len = Len(from_str)
Do While Len(txt) > 0
' Find from_str.
pos = InStr(txt, from_str)
If pos = 0 Then
' No more occurrences.
result = result & txt
txt = ""
Else
' Make the replacement.
result = result & Left$(txt, pos - 1) & to_str
txt = Mid$(txt, pos + from_len)
End If
Loop
ReplaceText = result
End Function
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 10:38 PM
#19
Thread Starter
Junior Member
Re: Help!
yep, its works!! thanks so much!!
-
Sep 1st, 2005, 10:43 PM
#20
Thread Starter
Junior Member
Re: Help!
hey, that code works but when i hve two dots between something it counts it as the end of a sentence. eg. i have...a fish.
it would count this as having two sentences, which is better then what i had before saying "18 sentences" etc..but it still has that error. is this fixable? to say only count the sentence if there is one full stop by itself.
-
Sep 1st, 2005, 10:48 PM
#21
Re: Help!
Post your code. I don't know which code you are using.
-
Sep 1st, 2005, 10:54 PM
#22
Thread Starter
Junior Member
Re: Help!
VB Code:
Private Sub cmdsentences_Click()
Dim InsideASentence As Boolean
Dim Sentence As String, Char As String 'setting the data types
Dim Count As Integer, I As Integer 'setting the data types
Count = 0 'initialise the count to 0
Sentence = Trim(txtInput.Text) 'set the end of the sentence.
If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
Char = Mid(Sentence, I, 1) 'characters in sentence
If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
InsideASentence = True
End If
If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
If InsideASentence Then
Count = Count + 1 'add one to count
InsideASentence = False
End If
End If
Next I
Count = Count 'count remains the same
End If
lblCountsentences.Caption = Count 'where to display the result
End Sub
-
Sep 1st, 2005, 10:57 PM
#23
Re: Help!
Hmm. That's going to be tricky to try and handle every conceivable type of sentence. In strict grammatical terms, 'I have...a fish.' is not a proper sentence anyway, so perhaps you needn't worry about that.
You could have something like 'I.have.many.big.fish.' which your program would count as 5 sentences. I'm not sure how best to handle that situation.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 11:07 PM
#24
Thread Starter
Junior Member
Re: Help!
Yeh, its a tad cnfusing. hmhmmhhm
maybe i will just assume that its correct. how would i create an error message if its not enetered correctly..i think that might be a better way to go!
-
Sep 1st, 2005, 11:19 PM
#25
Re: Help!
Probably the best thing to do would be to validate the sentence as it's being entered by the user. eg
if the last char entered was a '.' or a '!' or a '?' the don't allow them to enter another '.' or '!' or '?'
don't allow a '.' or a '!' or a '?' to be entered if the sentence doesn't contain any spaces
Things like that.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Sep 1st, 2005, 11:23 PM
#26
Re: Help!
You could check for an occurrence of '..", but I would think that you could check for a space (actually two spaces) after the end of a sentence.
-
Sep 3rd, 2005, 02:18 AM
#27
Fanatic Member
Re: Help!
Change
VB Code:
If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
InsideASentence = True
End If
to
VB Code:
If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", Char) > 0 Then
InsideASentence = True
End If
That way it will not detect a new sentence unless it starts with a capital letter. Should nail most things, unless you have ... followed by a name.
Brian
(Fighting with the RightToLeft bugs in VS 2005)
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
|