killaz219 Posted January 2, 2007 Share Posted January 2, 2007 (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 January 2, 2007 by killaz219 Infiniterider 1 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted January 2, 2007 Moderators Share Posted January 2, 2007 (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 January 2, 2007 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 More sharing options...
killaz219 Posted January 2, 2007 Author Share Posted January 2, 2007 (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 January 2, 2007 by killaz219 Link to comment Share on other sites More sharing options...
tmo Posted January 2, 2007 Share Posted January 2, 2007 (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¦Xjëh×6Func word_split($text) Return StringRegExp($text, "[\s,;:\.]*([[:alnum:]]+)[\s,;:\.]*", 3) EndFunc works fine so far Edited January 2, 2007 by tmo Link to comment Share on other sites More sharing options...
killaz219 Posted January 2, 2007 Author Share Posted January 2, 2007 $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. Infiniterider 1 Link to comment Share on other sites More sharing options...
llamnuds Posted January 3, 2007 Share Posted January 3, 2007 A handy script. Cheers, llamnuds Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now