Results 1 to 27 of 27

Thread: Help!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Exclamation 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:
    1. Private Sub cmdsentences_Click()
    2.     Dim Sentence As String, Char As String 'setting the data types
    3.     Dim Count As Integer, I As Integer 'setting the data types
    4.     Count = 0 'initialise the count to 0
    5.     Sentence = Trim(txtInput.Text) 'set the end of the sentence.
    6.     If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
    7.         For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
    8.             Char = Mid(Sentence, I, 1) 'characters in sentence
    9.             If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
    10.                 Count = Count + 1 'add one to count
    11.             End If
    12.         Next I
    13.             Count = Count 'count remains the same
    14.     End If
    15.     lblCountsentences.Caption = Count 'where to display the result
    16. End Sub

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Help!

    Do your sentances only end in ".", "!" and "?" ?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    yeh they do

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Help!

    Try something along these lines:
    VB Code:
    1. Sentence = Replace$(Sentence, "?", ".")
    2. Sentence = Replace$(Sentence, "!", ".")
    3. Do Until Instr(Sentence, "..") = 0
    4.     Sentence = Replace$(Sentence, "..", ".")
    5. 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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    can i leave it exactly as you put it or do i need to change it?

  6. #6
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    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:
    1. Private Sub cmdsentences_Click()
    2.    
    3.     Dim InsideASentence As Boolean
    4.    
    5.     Dim Sentence As String, Char As String 'setting the data types
    6.     Dim Count As Integer, I As Integer 'setting the data types
    7.     Count = 0 'initialise the count to 0
    8.     Sentence = Trim(txtInput.Text) 'set the end of the sentence.
    9.     If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
    10.         For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
    11.             Char = Mid(Sentence, I, 1) 'characters in sentence
    12.            
    13.             If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
    14.               InsideASentence = True
    15.             End If
    16.            
    17.             If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
    18.                
    19.                 If InsideASentence Then
    20.                   Count = Count + 1 'add one to count
    21.                   InsideASentence = False
    22.                 End If
    23.            
    24.             End If
    25.         Next I
    26.             Count = Count 'count remains the same
    27.     End If
    28.     lblCountsentences.Caption = Count 'where to display the result
    29. 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)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    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.

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Help!

    It should work exactly as it is
    VB Code:
    1. Private Sub cmdsentences_Click()
    2.     Dim Sentence As String, Char As String 'setting the data types
    3.     Dim Count As Integer, I As Integer 'setting the data types
    4.     Count = 0 'initialise the count to 0
    5.     Sentence = Trim(txtInput.Text) 'set the end of the sentence.
    6.     If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
    7.  
    8.         Sentence = Replace$(Sentence, "?", ".")
    9.         Sentence = Replace$(Sentence, "!", ".")
    10.         Do Until Instr(Sentence, "..") = 0
    11.             Sentence = Replace$(Sentence, "..", ".")
    12.         Loop
    13.  
    14.         For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
    15.             Char = Mid(Sentence, I, 1) 'characters in sentence
    16.             If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
    17.                 Count = Count + 1 'add one to count
    18.             End If
    19.         Next I
    20.  [b]Don't need to do this=>[/b] Count = Count 'count remains the same
    21.     End If
    22.     lblCountsentences.Caption = Count 'where to display the result
    23. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    haha, seems like it should work but the insideasentence comes up with "variable not found" should i be replacing it with something?

  10. #10
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Help!

    Quote 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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    i put that in, but it doesnt like the replace$ thing. i'm not sure why?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    i'm using vb 5

  13. #13
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    nooo it doesnt!! oh no!! what should i put :O

  15. #15
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Help!

    Quote 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)

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    i dont have a replace...so what should i do with dim??????

  17. #17
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Help!

    VB Code:
    1. Private Sub cmdsentences_Click()
    2.    
    3.     Dim InsideASentence As Boolean
    4.    
    5.     Dim Sentence As String, Char As String 'setting the data types
    6.     Dim Count As Integer, I As Integer 'setting the data types
    7.     Count = 0 'initialise the count to 0
    8.     Sentence = Trim(txtInput.Text) 'set the end of the sentence.
    9.     If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
    10.         For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
    11.             Char = Mid(Sentence, I, 1) 'characters in sentence
    12.            
    13.             If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
    14.               InsideASentence = True
    15.             End If
    16.            
    17.             If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
    18.                
    19.                 If InsideASentence Then
    20.                   Count = Count + 1 'add one to count
    21.                   InsideASentence = False
    22.                 End If
    23.            
    24.             End If
    25.         Next I
    26.             Count = Count 'count remains the same
    27.     End If
    28.     lblCountsentences.Caption = Count 'where to display the result
    29. 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)

  18. #18
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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:
    1. ' Replace all occurrances of from_str with to_str.
    2. Public Function ReplaceText(ByVal txt As String, ByVal _
    3.     from_str As String, ByVal to_str As String) As String
    4. Dim result As String
    5. Dim from_len As Integer
    6. Dim pos As Integer
    7.  
    8.     from_len = Len(from_str)
    9.     Do While Len(txt) > 0
    10.         ' Find from_str.
    11.         pos = InStr(txt, from_str)
    12.         If pos = 0 Then
    13.             ' No more occurrences.
    14.             result = result & txt
    15.             txt = ""
    16.         Else
    17.             ' Make the replacement.
    18.             result = result & Left$(txt, pos - 1) & to_str
    19.             txt = Mid$(txt, pos + from_len)
    20.         End If
    21.     Loop
    22.  
    23.     ReplaceText = result
    24. End Function
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    yep, its works!! thanks so much!!

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    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.

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Help!

    Post your code. I don't know which code you are using.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    Re: Help!

    VB Code:
    1. Private Sub cmdsentences_Click()
    2.    
    3.     Dim InsideASentence As Boolean
    4.    
    5.     Dim Sentence As String, Char As String 'setting the data types
    6.     Dim Count As Integer, I As Integer 'setting the data types
    7.     Count = 0 'initialise the count to 0
    8.     Sentence = Trim(txtInput.Text) 'set the end of the sentence.
    9.     If Len(Sentence) > 0 Then 'if there is no text, count stays at zero
    10.         For I = 1 To Len(Sentence) 'when it does have characters then execute instructions
    11.             Char = Mid(Sentence, I, 1) 'characters in sentence
    12.            
    13.             If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
    14.               InsideASentence = True
    15.             End If
    16.            
    17.             If Char = "." Or Char = "?" Or Char = "!" Then 'characters which show end of sentence(loop begins)
    18.                
    19.                 If InsideASentence Then
    20.                   Count = Count + 1 'add one to count
    21.                   InsideASentence = False
    22.                 End If
    23.            
    24.             End If
    25.         Next I
    26.             Count = Count 'count remains the same
    27.     End If
    28.     lblCountsentences.Caption = Count 'where to display the result
    29. End Sub

  23. #23
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Location
    perth
    Posts
    30

    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!

  25. #25
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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.

  26. #26
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  27. #27
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: Help!

    Change

    VB Code:
    1. If InStr("abcdefghijklmnopqrstuvwxyz1234567890", LCase$(Char)) > 0 Then
    2.               InsideASentence = True
    3.             End If

    to

    VB Code:
    1. If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", Char) > 0 Then
    2.               InsideASentence = True
    3.             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
  •  



Click Here to Expand Forum to Full Width