Results 1 to 17 of 17

Thread: [RESOLVED] [2005] Word Counter - Any improvments?

  1. #1

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Resolved [RESOLVED] [2005] Word Counter - Any improvments?

    Hey guys and gals, I have spent (with the help of a friend) 2 days working on this piece of code for a word counter.

    Now we have tested and tested all sorts of combinations, and so far I have not come along a single piece that does not work.

    But if you have any suggestions on how to improve please let me know!

    Module Module1 Code:
    1. Module Module1
    2.     Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer
    3.         Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found.
    4.         Dim X As Integer ' X tells InString from which char position to start from.
    5.         Dim WordCount As Integer ' How many words there are.
    6.         Dim NoMore As Boolean ' Yes or No.
    7.  
    8.         WordCount = 0
    9.         X = 1
    10.         NoMore = False
    11.  
    12.         If Len(Trim(TextToBeCounted)) > 0 Then
    13.             Do While NoMore = False
    14.                 SpacePos = InStr(X, Trim(TextToBeCounted), " ")
    15.                 If SpacePos > 0 Then
    16.                     If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then
    17.                         WordCount += 1
    18.                     End If
    19.                     X = SpacePos + 1
    20.                     Do While InStr(X, Mid(TextToBeCounted, X, 1), " ") > 0
    21.                         X += 1
    22.                     Loop
    23.                 Else
    24.                     If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then
    25.                         WordCount += 1
    26.                     End If
    27.                     NoMore = True
    28.                 End If
    29.  
    30.             Loop
    31.  
    32.         End If
    33.         MyWordCount = WordCount
    34.     End Function
    35. End Module
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Word Counter - Any improvments?

    Good work A couple of things though..
    You're using nothing but VB6 legacy functions, if this wasnt the .NET forum, I wouldve been certain that this was VB6 code.
    And alot of times you're calling the same method twice (or more), when you couldve just called it once and use the result twice, take this for example:

    vb Code:
    1. If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then
    2.                         WordCount += 1
    3.                     End If

    In this case, you're calling Asc(Mid(TextToBeCounted, X, 1)) alot of times, it could be optimized by doing like this:
    vb Code:
    1. Dim charValue As Integer = Asc(Mid(TextToBeCounted, X, 1))
    2. If charValue > 64 And charValue < 91 Or charValue > 96 And charValue < 123 Or charValue > 47 And charValue < 58 Then
    3.                         WordCount += 1
    4.                     End If

    And to optimize it even further, make use of the AndAlso/OrElse operators:

    vb Code:
    1. Dim charValue As Integer = Asc(Mid(TextToBeCounted, X, 1))
    2. If charValue > 64 AndAlso charValue < 91 OrElse charValue > 96 AndAlso charValue < 123 OrElse charValue > 47 AndAlso charValue < 58 Then
    3.                         WordCount += 1
    4.                     End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [2005] Word Counter - Any improvments?

    You are very true Asc(Mid(TextToBeCounted, X, 1)) is repeated alot.

    Thank you for your very quick and helpful post, I have taken your ideas.

    What would you suggest, insead of using Asc and Mid then?

    Thank you
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Word Counter - Any improvments?

    I think .SubString is the .NET version of Mid if I remember correctly.

    vb Code:
    1. Dim myInteger As Integer = Asc(texttobeCounted.Substring(x, 1))

    Also, you can use this which is more conventional for .NET:
    vb Code:
    1. If texttobeCounted.Trim.Length > 0 Then

    I think inStr is from VB6 and usually you would use IndexOf but as you are looping through each char it probably wouldn't be viable so it's probably more trouble than it's worth changing it.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Word Counter - Any improvments?

    Here's what I would change: (Note that I dont know if this will improve the performance or not, I just think its a good idea to use these in a .NET language)
    Instead of using the Trim() function, use the String's Trim method.
    vb Code:
    1. Trim(MyString)
    2. 'becomes:
    3. MyString.Trim()
    Instead of using the Len() function, use the String's Length property.
    vb Code:
    1. Len(MyString)
    2. 'becomes:
    3. MyString.Length
    Instead of using the Mid() function, use the String's Substring method:
    vb Code:
    1. Mid(MyString, X, 1)
    2. 'becomes:
    3. MyString.Substring(X-1,1) 'Using substring, the first character in a string is at index 0, whereas Mid() sees 1 as the index of the first char.

    And to be honest, I think Asc() is okay to use. As there is no really good .NET equivallent.

    Edit: Ah yes I forgot InStr, good thing Stimbo brought that up
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [2005] Word Counter - Any improvments?

    Hey guys, I have gone through and changed all of the parts you have suggested.

    I have run through the prog and it seems to be working fine. Here is the end piece if you are interested.

    vb Code:
    1. Module Module1
    2.     Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer
    3.         Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found.
    4.         Dim X As Integer ' X tells InString from which char position to start from.
    5.         Dim WordCount As Integer ' How many words there are.
    6.         Dim NoMore As Boolean ' Yes or No.
    7.         Dim CharValue As Integer
    8.  
    9.         WordCount = 0
    10.         X = 1
    11.         NoMore = False
    12.  
    13.         If TextToBeCounted.Trim.Length > 0 Then
    14.             Do While NoMore = False
    15.                 SpacePos = InStr(X, Trim(TextToBeCounted), " ")
    16.                 If SpacePos > 0 Then
    17.                     CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    18.                     If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    19.                         WordCount += 1
    20.                     End If
    21.                     X = SpacePos + 1
    22.                     Do While InStr(X, (TextToBeCounted.Substring(X - 1, 1)), " ") > 0
    23.                         X += 1
    24.                     Loop
    25.                 Else
    26.                     If X <= TextToBeCounted.Length Then
    27.                         CharValue = Asc(TextToBeCounted.Substring(X - 1, 1))
    28.                         If CharValue > 64 AndAlso CharValue < 91 OrElse CharValue > 96 AndAlso CharValue < 123 OrElse CharValue > 47 AndAlso CharValue < 58 Then
    29.                             WordCount += 1
    30.                         End If
    31.                     End If
    32.                     NoMore = True
    33.                 End If
    34.  
    35.             Loop
    36.  
    37.         End If
    38.         MyWordCount = WordCount
    39.     End Function
    40. End Module

    Edit: Sorry forgot to mention, I did not understand Indexof very much so that is why I have left Instr inside (for the time being, currently searching now.)
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  7. #7
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [2005] Word Counter - Any improvments?

    I think this solution is overly complicated. To count words, why not use a regular expression? They're very very fast and efficient.

    This example is in VB6 but you can still use the same regular expression and the new .Net regex classes to simply count all of the matches. This way you can get your 20-30 lines of code down to about 2-3.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  8. #8

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [2005] Word Counter - Any improvments?

    Hey.... well that burst my bubble!

    I had a look though RegExp is not supported in VB.Net.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  9. #9
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Word Counter - Any improvments?

    check this out.
    How to use regular expressions in vb.net

  10. #10

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [2005] Word Counter - Any improvments?

    Hey, thank you all so much for all of your help!

    Kasracer and talkro I am sorry though I have read through both of these and I do not understand them enough to take that path.

    Thank you both though.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Word Counter - Any improvments?

    Code:
        Function CountWords(ByVal Text As String) As Long
            Dim re As New RegularExpressions.Regex("\b\w+\b")
            ' the following pattern means that we're looking for a word character (\w)
            ' repeated one or more times (the + suffix), and that occurs on a word
            ' boundary (leading and trailing \b sequences)
            ' the Execute method does the search and returns a MatchCollection object
            ' which in turn exposes the Count property,
            '  i.e. the result we're interested into
            CountWords = re.Matches(Text).Count
        End Function
    I copied pasted changed the link kas provided.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    How 'bout this

    Code:
        Function CountWords(ByVal theText As String) As Int32
            Dim loopCTR, wordCTR As Int32
            Dim words() As String
            Dim valid As String = "abcdefghijklmnopqrstuvwxyz" 'characters that define word beginning
            Dim strip As String = ":;?/.>,<`~!@#$%^&*()-_=+[{}]|\'0123456789" & ControlChars.Quote 'strip this out
            valid &= valid.ToUpper 'upper and lower
            theText = theText.Trim 'get rid of lead/trail spaces
            For loopCTR = 0 To strip.Length - 1 'strip characters out (is strip defined correctly?)
                theText = theText.Replace(strip.Substring(loopCTR, 1), " ")
            Next
            words = theText.Trim.Split(" "c) 'split into an array
            For loopCTR = 0 To words.Length - 1
                If words(loopCTR) <> "" Then
                    If valid.IndexOf(words(loopCTR).Substring(0, 1)) <> -1 Then
                        wordCTR += 1
                    End If
                End If
            Next
        End Function
    I just threw this together, but my gut says strip may need attention.
    Last edited by dbasnett; Mar 5th, 2008 at 01:52 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    I used the following as a test

    Do While NoMore = False SpacePos = InStr(X, Trim(TextToBeCounted), ) If SpacePos > 0 Then If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 End If X = SpacePos + 1 Do While InStr(X, Mid(TextToBeCounted, X, 1), ) > 0 X += 1 Loop Else If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 End If NoMore = True End If Loop


    My word count was 97. Microsoft Word thinks it is 124 words.

    It looks like this after parsing

    Do While NoMore False SpacePos InStr X Trim TextToBeCounted If SpacePos Then If Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Or Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Or Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Then WordCount End If X SpacePos Do While InStr X Mid TextToBeCounted X X Loop Else If Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Or Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Or Asc Mid TextToBeCounted X And Asc Mid TextToBeCounted X Then WordCount End If NoMore True End If Loop
    Last edited by dbasnett; Mar 5th, 2008 at 02:53 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    This also returns 124 words, and looks to be the simplest solution
    vb Code:
    1. Dim s As String = "Do While NoMore = False SpacePos = InStr(X, Trim(TextToBeCounted), ) If SpacePos > 0 Then If " & _
    2. "Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or " & _
    3. "Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or " & _
    4. "Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 " & _
    5. "End If X = SpacePos + 1 Do While InStr(X, Mid(TextToBeCounted, X, 1), ) > 0 X += 1 Loop Else If " & _
    6. "Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or " & _
    7. "Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or " & _
    8. "Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 " & _
    9. "End If NoMore = True End If Loop"
    10.  
    11.         Console.WriteLine(s.Split(" "c).Length)

  15. #15
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    Unless you're writing this function for learning purposes, String.Split with option RemoveEmptyEntries will do the job in a single line of code.

  16. #16
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    Quote Originally Posted by wild_bill
    This also returns 124 words, and looks to be the simplest solution
    Simplest? Maybe (though the Regex is very simple and probably faster). Accurate? Probably not 100% but it's a very good idea.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  17. #17

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Re: [RESOLVED] [2005] Word Counter - Any improvments?

    I had also used Word to test that, and my prog + code.

    The reason behind the difference is that My prog counts ONLY A-Z a-z and 0-9.

    WORD Counts = and > as well by the looks of it.

    Once you remove all of the Misc symbols in the group of text (Which I do not want to be counted as words in my prog), the result is the same.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

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