Sign in to follow this  
Followers 0
killaz219

Wordcount

6 posts in this topic

#1 ·  Posted (edited)

I made this yesterday, because I needed it for a small project. I threw it together so it is a little messy. I hope somebody finds it useful

Example

#include <GuiConstants.au3>
GuiCreate("WordCounter", 392, 316,-1, -1)

$Edit_1 = GuiCtrlCreateEdit("", 10, 10, 370, 270)
$Button_2 = GuiCtrlCreateButton("Count", 10, 280, 370, 30)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $button_2
        $woot = GUICtrlRead($Edit_1)
        MsgBox(0, "", WordCount($woot))
    EndSelect
WEnd
Exit

Func WordCount($text)
    Global $word = 0, $temp = 0
    For $i = 1 To StringLen($text)
        If (Asc(StringMid($text, $i, 1))) < 33 Then
            Do
                $i = $i + 1
            Until (Asc(StringMid($text, $i, 1))) >= 33 Or $i > StringLen($text)
            $word = $word + 1
        Else
            If $word = 0 Then
                $word = 1
            Endif
        EndIf
    Next
    Return $word
EndFunc
Edited by killaz219

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You could simplify your WordCount() function a tad if all you are doing is looking for spaces.

Func _WordCount($text)
    Return UBound(StringSplit(StringReplace($text, @CRLF, ' '), ' ')) - 1
EndFuncoÝ÷ ØGb´êæk&Þz÷§jëh×6Func _WordCount($text)
    Return UBound(StringRegExp($text, '(?s)(?i)(.*?)\s', 3))
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You could simplify your WordCount() function a tad if all you are doing is looking for spaces.

Func _WordCount($text)
    Return UBound(StringSplit(StringReplace($text, @CRLF, ' '), ' ')) - 1
EndFuncoÝ÷ ØGb´êæk&Þz÷§jëh×6Func _WordCount($text)
    Return UBound(StringRegExp($text, '(?s)(?i)(.*?)\s', 3))
EndFunc
Yes but sometimes words aren't seperated by only spaces. this would look like one word:

Hello
you

And your second example is messed up if there is more then one space

hello                  you
Edited by killaz219

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$text = "Hi,      these    " & @CRLF & "   are           19                 words.Even if i forget that space after the dot the words are counted correctly"
$array = StringRegExp($text, "[\s,;:\.]*([[:alnum:]]+)[\s,;:\.]*", 3)
ConsoleWrite(UBound($array))oÝ÷ Ù§]ب©eÉ©e×Ü"­Û­è"½éâØ^jºÚÊÊ-«r¢ç(ºWp®+^iû§rبƥçp¢·l¦X­jëh×6Func word_split($text)
    Return StringRegExp($text, "[\s,;:\.]*([[:alnum:]]+)[\s,;:\.]*", 3)
EndFunc

works fine so far

Edited by tmo

Share this post


Link to post
Share on other sites

$text = "Hi,      these    " & @CRLF & "   are           19                 words.Even if i forget that space after the dot the words are counted correctly"
$array = StringRegExp($text, "[\s,;:\.]*([[:alnum:]]+)[\s,;:\.]*", 3)
ConsoleWrite(UBound($array))
I like it. Much shorter then mine.

Share this post


Link to post
Share on other sites

A handy script.

Cheers,

llamnuds

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0