Jump to content

Wordcount


killaz219
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
Share on other sites

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
Link to comment
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))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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...