字符统计

字符统计

学习有一段时间的AHK了,但一直感觉自己水平不够,自己写的都不好意思发。先前在论坛搜索发现好像还没有人发字符统计的脚本,所以写了一个与大家分享,希望与大家一起学习交流,融入AHK大家庭。

下面的隐藏内容 无内容,谢谢积分富裕的大佬!!!

下载文件包括脚本以及两个图标

字符统计脚本文件

提取码:cu75复制
解压码:无
;****************************
;* 【单字符统计工具】*
;****************************

Label_ScriptSetting: ;脚本前参数设置
	#MaxMem 256
	Process, Priority, , High						;脚本高优先级
	#Persistent										;让脚本持久运行(关闭或ExitApp)
	#SingleInstance,Force 							;运行替换旧实例
	#WinActivateForce								;强制激活窗口

Label_DefVar: ;初始化变量
	global maxMemary := 655360000
	OnMessage( 0x201 , "move_Win")
	global ChangeContentFlag := False
	global CharStatistic_Content := ""		;字符统计字符串
	global WinID := ""
	global Content_Hwnd := ""
	global TAB_Hwnd := ""
	global StatusBar_Hwnd := ""
	global Statistic_Progress := ""
	global AutoUpdate := ""
	global pos_x_Progress := 0
	global pos_y_Progress := 0
	global FontSize := 10
	global custom_CharStatistic := ["自","定","义"]
	global custom_len := custom_CharStatistic.Length()

Label_LoadIcos: ;加载图标
	wait_path = %A_ScriptDir%\CharStatistic_red_ball.ico
	comp_path = %A_ScriptDir%\CharStatistic_green_ball.ico
	global waitIcon := LoadPicture(wait_path,, imgtype)
	global compIcon := LoadPicture(comp_path,, imgtype)
	Gosub,Char_Statistic_Gui
Return

str_Count: ; 统计字符串
	; 中文标点代码
	CHPUList := "183,215,8212,8216,8217,8220,8221,8230,12289,12290,12298,12299,12302,12303,12304,12305,65281,65288,65289,65292,65306,65307,65311"

	modeBase := Format("{:d}", len/128)
	firstChar := Asc(SubStr(CharStatistic_Content, 1, 1))
	lastChar := Asc(SubStr(CharStatistic_Content, 0, 1))

	strCount := 0		;字符数量
	CHPUCount := 0		;中文标点数量
	CHCount := 0		;中文数量
	ENPUCount := 0		;英文标点数量
	ENCount := 0		;英文字母数量
	ENFlag := False
	ENWordCount := 0	;英文单词数量
	NUCount := 0		;数字数量
	CHSPCount := 0		;中文空格数量
	ENSPCount := 0		;英文空格数量
	TABCount := 0		;TAB数量
	PACount := 0		;段落数量
	PAFlag := False
	BlankLineCount := 0	;空白行数量
	Loop, %custom_len%{
		custom_CharStatistic_Count_%A_index% := 0
	}
	Loop, Parse, CharStatistic_Content
	{
		If (ChangeContentFlag){
			Return
		}
		If (Mod(A_Index, modeBase)=0){
			percent := Format("{:.2f}", A_Index/len*100)
			SB_SetText(percent . " %",3)
			GuiControl,, %Statistic_Progress%, %percent%
		}
		char := Asc(A_LoopField)
		; 统计中文标点
		if char In %CHPUList%
		{
			CHPUCount += 1
		}
		; 统计中文字符
		Else if (char >= 0x4E00 && char <= 0x29FA5)
		{
			CHCount += 1
		}
		; 统计英文字符
		Else if ((char >= 65 && char <= 90)||(char >= 97 && char <= 122)){
			ENCount += 1
			ENFlag = True
		}
		; 统计数字
		Else if (char >= 48 && char <= 57){
			NUCount += 1
		}
		; 统计段落数量和空白行数量
		Else if (char = 13 || char = 10)
		{
			If (char = 10){
				If (PAFlag){
					BlankLineCount += 1
				}Else{
					PAFlag := True
					PACount += 1	
				}
			}
		}
		; 统计中文空格数量
		Else if (char = 12288)
		{
			CHSPCount += 1
		}
		; 统计英文空格数量
		Else if (char = 32)
		{
			ENSPCount += 1
		}
		; 统计TAB数量
		Else if (char = 9)
		{
			TABCount += 1
		}
		; 统计英文标点数量
		Else{
			ENPUCount += 1
		}
		; 统计英文单词,上一个是英文字母,下一个不是英文字母则是单词
		If (!((char >= 65 && char <= 90)||(char >= 97 && char <= 122)) && ENFlag){
			ENWordCount += 1
			ENFlag := False
		}
		; 统计段落,如果当前字符不是回车或换行,则进入新段落
		Else If (char != 13 && char != 10){
			PAFlag := False
		}
		;-----自定义统计-----
		For ki, kv in custom_CharStatistic{
			If (A_LoopField = kv){
				custom_CharStatistic_Count_%ki% += 1
				break
			}
		}
	}
	; 最后是字母,单词加一
	if ((lastChar >= 65 && lastChar <= 90)||(lastChar >= 97 && lastChar <= 122)){
		ENWordCount += 1
	}
	; 最后是不是换行,段落加一
	If (lastChar != 10 && lastChar != 13){
		PACount += 1
	}
	; 开始是换行不计入段落,但计入空白行
	If (firstChar = 13 || firstChar = 10){
		PACount -= 1
		BlankLineCount += 1
	}
	If (len = 0){
		PACount = 0
	}
	SB_SetText("100.00 %",3)
	GuiControl,, %Statistic_Progress%, 100
	strCount := CHCount + ENCount + NUCount + CHPUCount + ENPUCount + CHSPCount + ENSPCount + TABCount
Return

Char_Statistic_Gui: ;字符统计页面GUI
	Statistic_Width := 75
	Statistic_text_Width := 53

	Gui, 55:Destroy
	Gui, 55:Default
	Gui, 55:+Resize +hwndWinID
	Gui, 55:Margin,10,10
	Gui, 55:Font, s%FontSize%, Microsoft YaHei
	Gui, 55:Add, Edit, Limit100000000 r30 w900 x5 y10 gChangeContent HwndContent_Hwnd section
	Gui, 55:Add, Button,ys w25 gAddFontSize HwndAddFont_Hwnd, +
	Gui, 55:Add, Button,y+10 w25 gSubFontSize HwndSubFont_Hwnd, -
	Gui, 55:Add, Button,ys w25 gReStatistic HwndReStatistic Default, √
	Gui, 55:Add, Button,ys w25 gAddCustom HwndAddCustom, +
	Gui, 55:Add, Button,ys w25 gSubCustom HwndSubCustom, -
	Gui, 55:Add, CheckBox, ys gChangeContent HwndAutoUpdate Checked, 自动统计
	Gui, 55:Add, Tab3, xs y+15 h200 vConfigTab HwndTAB_Hwnd section gTABchange vWhichTab, 统计|自定义
	Gui, 55:Tab, 统计, , Exact
	Gui, 55:Add, Text,y+15 w%Statistic_text_Width% Ce74c3c section, 中文数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %CHCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, 字母数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %ENCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, 数字数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %NUCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, 中文标点
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %CHPUCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, 英文标点
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %ENPUCount%
	Gui, 55:Add, Text,xs y+15 w%Statistic_text_Width% Ce74c3c section, 中文空格
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %CHSPCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, 英文空格
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %ENSPCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% Ce74c3c, TAB数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %TABCount%
	Gui, 55:Add, Text,xs y+15 w%Statistic_text_Width% Ccf1322 section, 字符总数
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %strCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% C389e0d, 英文单词
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %ENWordCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% C08979c, 段落数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %PACount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% C096dd9, 空行数量
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %BlankLineCount%
	Gui, 55:Add, Text,ys w%Statistic_text_Width% C434343, 预估长度
	Gui, 55:Add, Edit,ReadOnly x+5 yp-2 h23 w%Statistic_Width%, %len%

	Gui, 55:Tab, 自定义, , Exact
	For ki, kv in custom_CharStatistic
	{
		If (Mod(ki, 5) = 1){
			Gui, 55:Add, Edit,xs y+15 w%Statistic_text_Width%  h23 section Limit1 C2c80c5 Hwndcustom_Char_Hwnd_%ki%, %kv%
			Gui, 55:Add, Edit,ReadOnly x+5 h23 w%Statistic_Width% Hwndcustom_CharStatistic_Hwnd_%ki%
		}
		Else{
			Gui, 55:Add, Edit,ys w%Statistic_text_Width% h23 Limit1 C2c80c5 Hwndcustom_Char_Hwnd_%ki%, %kv%
			Gui, 55:Add, Edit,ReadOnly x+5 h23 w%Statistic_Width% Hwndcustom_CharStatistic_Hwnd_%ki%
		}		
	}

	Gosub,TABchange


	Gui, 55:Tab
	Gui, 55:Add, StatusBar

	FontSize := 15
	Gui, 55:Font,cblack s%FontSize%, Segoe UI
	GuiControl, Font, %Content_Hwnd%
	Gui, 55:Show,W780 H550 , 单字符统计
	SendInput {End}
	StatusBar_Hwnd := SB_SetParts(20,85,65)
	SB_SetIcon("HICON:*" compIcon,,1)
	SB_SetText("统计已完成!",2)
	SB_SetText("100%",3)
	GuiControlGet, MyEdit, Pos, %StatusBar_Hwnd%
	pos_x_Progress := MyEditX + 170
	pos_y_Progress := MyEditY + (MyEditH - 10)/2
	w_Progress := MyEditW - 170 -10
	Gui, 55:Add, Progress, x%pos_x_Progress% y%pos_y_Progress% w%w_Progress% h10 cBlue HwndStatistic_Progress , 0
	SetTimer, checkContent, 10
Return

ChangeContent(){ ;检测输入内容变化
	GuiControlGet, is_AutoUpdate ,, %AutoUpdate%
	If (is_AutoUpdate){
		ChangeContentFlag := True
	}
	Return
}

AddFontSize: ;增加字体大小
	FontSize += 1
	Gui, 55:Font, s%FontSize%, Microsoft YaHei
	GuiControl, Font, %Content_Hwnd%
Return

SubFontSize: ;增加字体大小
	FontSize -= 1
	Gui, 55:Font, s%FontSize%, Microsoft YaHei
	GuiControl, Font, %Content_Hwnd%
Return

ReStatistic: ;重新统计
	custom_CharStatistic := []
	Loop, %custom_len%
	{
		Hwnd_temp := custom_Char_Hwnd_%A_index%
		GuiControlGet, Temp, ,%Hwnd_temp%
	    custom_CharStatistic.Insert(Temp)
	}
	ChangeContentFlag := True
Return

AddCustom: ;增加一个统计字符
	MsgBox, 增加一个统计字符,暂未实现!
Return

SubCustom: ;减少一个统计字符
	MsgBox, 减少一个统计字符,暂未实现!
Return

checkContent: ;内容变化更新统计数据
	If (ChangeContentFlag){
		Gui, 55:Default
		ChangeContentFlag := False
		SB_SetIcon("HICON:*" waitIcon,,1)
		SB_SetText("正在统计中!",2)
		try_Save_Content:
			Try
				GuiControlGet, CharStatistic_Content, ,%Content_Hwnd%
			Catch e{
				maxMemary *= 2
				MsgBox, %maxMemary%
				VarSetCapacity(CharStatistic_Content, 0)
				VarSetCapacity(CharStatistic_Content, %maxMemary%)
	   			Gosub, try_Save_Content
			}
		global len := StrLen(CharStatistic_Content)
		GuiControl, Text, Edit14, %len%
		gosub, str_Count
		gosub, RefreshStatic
		SB_SetIcon("HICON:*" compIcon,,1)
		SB_SetText("统计已完成!",2)
		VarSetCapacity(CharStatistic_Content, 0)
	}
Return

RefreshStatic: ;刷新统计GUI
	GuiControl, Text, Edit2, %CHCount%
	GuiControl, Text, Edit3, %ENCount%
	GuiControl, Text, Edit4, %NUCount%
	GuiControl, Text, Edit5, %CHPUCount%
	GuiControl, Text, Edit6, %ENPUCount%
	GuiControl, Text, Edit7, %CHSPCount%
	GuiControl, Text, Edit8, %ENSPCount%
	GuiControl, Text, Edit9, %TABCount%
	GuiControl, Text, Edit10, %strCount%
	GuiControl, Text, Edit11, %ENWordCount%
	GuiControl, Text, Edit12, %PACount%
	GuiControl, Text, Edit13, %BlankLineCount%
	For ki, kv in custom_CharStatistic{
		Hwnd_temp := custom_CharStatistic_Hwnd_%ki%
		Count_temp := custom_CharStatistic_Count_%ki%
		GuiControl, Text, %Hwnd_temp%, %Count_temp%
	}

Return

TABchange: ;切换TAB时的变化
	GuiControlGet, OutputVar ,, %TAB_Hwnd%
	If (OutputVar="自定义"){
		GuiControl, Show, %AddCustom%
		GuiControl, Show, %SubCustom%
	}Else{
		GuiControl, Hide, %AddCustom%
		GuiControl, Hide, %SubCustom%
	}
	Gosub, fix_move
Return

55GuiClose: ;关闭功能
	ExitApp
Return

55GuiSize: ;自适应调整GUI大小
	If (strCount >= 50000){
		Gosub, fix_move
		Return
	}
	if ErrorLevel = 1  ; 窗口被最小化了. 无需进行操作.
	    return
	; 否则, 窗口的大小被调整过或被最大化了. 调整编辑控件的大小以匹配窗口.
	NewWidth := A_GuiWidth - 40
	NewHeight := A_GuiHeight - 200
	GuiControl, Move, Edit1, W%NewWidth% H%NewHeight%
	X_AddFont := NewWidth +10
	GuiControl, MoveDraw, %AddFont_Hwnd%, X%X_AddFont%
	GuiControl, MoveDraw, %SubFont_Hwnd%, X%X_AddFont%
	ReStatisti_Height := NewHeight + 40
	GuiControl, MoveDraw, %ReStatistic%, X%X_AddFont%, y%ReStatisti_Height%
	ReStatisti_Height := ReStatisti_Height + 35
	GuiControl, MoveDraw, %AddCustom%, X%X_AddFont%, y%ReStatisti_Height%
	ReStatisti_Height := ReStatisti_Height + 35
	GuiControl, MoveDraw, %SubCustom%, X%X_AddFont%, y%ReStatisti_Height%
	X_AutoUpdate := NewWidth - 70
	Y_AutoUpdate := NewHeight + 20
	ReStatisti_Height := ReStatisti_Height + 35
	GuiControl, MoveDraw, %AutoUpdate%, X%X_AutoUpdate%, y%Y_AutoUpdate%
	; GuiControl, MoveDraw, %AutoUpdate%, X%X_AddFont%, y%ReStatisti_Height%
	Statistic_Height := A_GuiHeight - 200 + 15
	fix_move:
		GuiControl, MoveDraw, %TAB_Hwnd%, Y%Statistic_Height% W%NewWidth%
		GuiControlGet, MyEdit, Pos, %StatusBar_Hwnd%
		pos_x_Progress := MyEditX + 170
		pos_y_Progress := MyEditY + (MyEditH - 12)/2
		w_Progress := MyEditW - 170 -10
		GuiControl, MoveDraw, %Statistic_Progress%, X%pos_x_Progress% Y%pos_y_Progress% W%w_Progress% h12
		SetTimer, fix_Process, -10
return

fix_Process:
	GuiControlGet, OutputVar ,, %Statistic_Progress%
	GuiControl,, %Statistic_Progress%, %OutputVar%
Return

move_Win(){ ;左键移动窗口
    PostMessage, 0xA1, 2
}

给TA捐赠
共{{data.count}}人
人已捐赠
AHKV1教程

【小工具】非默认应用打开软件新想法

2022-1-28 14:41:40

AHKV1案例

ListBox/ListView与tab控件结合示例

2022-2-4 7:04:14

10 条回复 A文章作者 M管理员
  1. james

    谢谢大神分享,学习

  2. yukingzou

    谢谢大神分享,学习

  3. 蜜獾哥

    效率不够,还不够全面。字符串遍历用loop还是有缺陷,如果碰到占四个字节的汉字怎么统计?用正则可能效率更快

    • hei

      我去试试正则改一改,不过想请教下四个字节汉字是啥意思😁

    • 蜜獾哥

      正常汉字是双字节,一些扩展字集是占四个字节,普通的字符遍历和计数无效无法切割

    • hei

      好的,谢谢

  4. 11010010

    感觉你是不是没框住?😊

    • hei

      尬住了,想骗积分没骗到😜

    • 11010010

      可以修改

个人中心
有新私信 私信列表
搜索