Simple script to count characters and words in selection

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
surfmadpig
Posts: 1
Joined: 02 Apr 2019, 07:22

Simple script to count characters and words in selection

Post by surfmadpig » 02 Apr 2019, 07:46

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.

User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Simple script to count characters and words in selection

Post by sinkfaze » 02 Apr 2019, 08:26

Character count:

Code: Select all

t=This isn't a test.
MsgBox %	StrLen(t)
Character count excluding spaces (this does not include `r`n or other vertical whiteapce):

Code: Select all

t=This isn't a test.
MsgBox %	StrLen(StrReplace(t," "))
Word count:

Code: Select all

t=This isn't a test. This is just an exercise. Promise.
RegExReplace(t,"\b[\w\-\']+\b","",c)
MsgBox %	c

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Simple script to count characters and words in selection

Post by jeeswg » 02 Apr 2019, 16:27

- Here's an attempt at some word count code.

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 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:: ;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

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Simple script to count characters and words in selection

Post by Sweetins » 02 Jul 2022, 10:59

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.
Then wouldn't this do just fine?

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

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Simple script to count characters and words in selection

Post by BoBo » 02 Jul 2022, 14:16

Then wouldn't this do just fine?
Probably - even three years late(r) :mrgreen:

Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Simple script to count characters and words in selection

Post by Sweetins » 08 Jul 2022, 11:07

BoBo wrote:
02 Jul 2022, 14:16
Probably - even three years late(r) :mrgreen:
:D

User avatar
Chunjee
Posts: 1419
Joined: 18 Apr 2014, 19:05
Contact:

Re: Simple script to count characters and words in selection

Post by Chunjee » 08 Jul 2022, 12:15

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

Post Reply

Return to “Ask for Help (v1)”