Simple script to count characters and words in selection
-
- Posts: 1
- Joined: 02 Apr 2019, 07:22
Simple script to count characters and words in selection
How can I get a MsgBox displaying the number of words, number of characters (including spaces) and number of characters (not counting spaces) in a selection?
Apologies if this is a pretty basic question. I haven't managed to find an AHK script that does all of the above.
Apologies if this is a pretty basic question. I haven't managed to find an AHK script that does all of the above.
Re: Simple script to count characters and words in selection
Character count:
Character count excluding spaces (this does not include `r`n or other vertical whiteapce):
Word count:
Code: Select all
t=This isn't a test.
MsgBox % StrLen(t)
Code: Select all
t=This isn't a test.
MsgBox % StrLen(StrReplace(t," "))
Code: Select all
t=This isn't a test. This is just an exercise. Promise.
RegExReplace(t,"\b[\w\-\']+\b","",c)
MsgBox % c
Re: Simple script to count characters and words in selection
- Here's an attempt at some word count code.
- The idea is this: if you see a stream of spaces/tabs/enter characters, the first character of that stream is removed.
- The number of characters removed = the number of gaps between words.
- The number of gaps between words + 1 = the number of words.
- I also consider that if the text only contains spaces/tabs/enter characters, there are zero words.
- Here are some tests:
Code: Select all
q:: ;word count
vText := "This isn't a test. This is just an exercise. Promise."
vLen1 := StrLen(vText)
vLen2 := StrLen(RegExReplace(vText, "([ `t`r`n])((?1)*)", "$2"))
;word count = gaps + 1:
;vWordCount := vLen1 - vLen2 + 1
;word count = gaps + 1 (if the text is just a gap with no words, word count = 0):
vWordCount := vLen1 - vLen2 + !RegExMatch(vText, "^[ `t`r`n]*$")
;vWordCount := vLen1 - vLen2 + !(Trim(vText, " `t`r`n") = "") ;an alternative
MsgBox, % vWordCount
return
- The number of characters removed = the number of gaps between words.
- The number of gaps between words + 1 = the number of words.
- I also consider that if the text only contains spaces/tabs/enter characters, there are zero words.
- Here are some tests:
Code: Select all
q:: ;RegEx - remove first character in pattern (handle repeated subpatterns)
;(subpatterns as 'variables') (backreferences) (capturing groups) (word count)
vText := "aaabbbcccaaabbbcccaaabbbccc"
MsgBox, % RegExReplace(vText, "(b)(b*)", "$2")
MsgBox, % RegExReplace(vText, "(?<!b)b(?=b+)")
MsgBox, % RegExReplace(vText, "(b)((?1)*)", "$2")
MsgBox, % RegExReplace(vText, "(?P<p1>b)((?P=p1)*)", "$2")
return
;source: http://www.pcre.org/pcre.txt
;(?n) call subpattern by absolute number
;(?P<name>...) named capturing group (Python)
;(?P=name) reference by name (Python)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Simple script to count characters and words in selection
Then wouldn't this do just fine?jeeswg wrote: ↑02 Apr 2019, 16:27- The idea is this: if you see a stream of spaces/tabs/enter characters, the first character of that stream is removed.
- The number of characters removed = the number of gaps between words.
- The number of gaps between words + 1 = the number of words.
- I also consider that if the text only contains spaces/tabs/enter characters, there are zero words.
Code: Select all
q:: ;word count
vText := "This isn't a test. This is just an exercise. Promise."
If !(RegExMatch(vText, "^[ `t`r`n]*$"))
RegExReplace(vText, "([ `t`r`n])((?1)*)", "$2", vWordCount), vWordCount++
else
vWordCount=0
MsgBox, % vWordCount
return
Re: Simple script to count characters and words in selection
Probably - even three years late(r)Then wouldn't this do just fine?
Re: Simple script to count characters and words in selection
2024 technology:
Code: Select all
A := new biga() ; requires https://github.com/biga-ahk/biga.ahk
vText := "This isn't a test. This is just an exercise. Promise."
; number of words
msgbox, % A.size(A.words(vText))
; => 10
; number of characters (including spaces)
msgbox, % strLen(vText)
; => 53
; number of characters (not counting spaces)
msgbox, % strLen(strReplace(vText, " "))
; => 44