forked from qwe7989199/aegisub_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_stat.lua
More file actions
184 lines (176 loc) · 6.3 KB
/
text_stat.lua
File metadata and controls
184 lines (176 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
local tr = aegisub.gettext
script_name = tr"Text Stats"
script_description = tr"Statistics for selected lines"
script_author = "domo"
script_version = "1.0"
include("unicode.lua")
k_threshold=1
function text_stat(subtitles, selected_lines, active_line)
for i=1,#subtitles do
if subtitles[i].class=="dialogue" then
dialogue_start=i
break
end
end
TotalLineNum=#selected_lines
TotalKNum=0
EngWordsNum=0
NonEngCharsNum=0
SpaceNum=0
MaxLineDuration=0
MaxDurationIndex=0
MinLineDuration=99999
MinDurationIndex=0
MaxLineLength=0
MaxLengthIndex=0
MinLineLength=99999
MinLengthIndex=0
fullWidthSpaceNum=0
TotalDuration=0
LineLength={}
LineDuration={}
min_k={dur=99999,line_index=0}
max_k={dur=0,line_index=0}
BoundaryKDurOfLine={[0]={max=0,min=99999}}
for z, i in ipairs(selected_lines) do
l = subtitles[i]
if l.comment==true or string.find(l.effect,"template") or string.find(l.effect,"code") then
aegisub.debug.out("Line #"..i-dialogue_start.." ignored.\n")
else
--Deal with karaoke first
if string.find(l.text,"\\[kK][fo]?%d+")~=nil then
karaoke=true
BoundaryKDurOfLine=k_stat(l.text,i)
TotalKNum=TotalKNum+BoundaryKDurOfLine[i].kNum
end
--Strip tags
text_stripped=string.gsub(l.text,"%{.-%}","")
if text_stripped=="" and TotalLineNum==1 then
aegisub.debug.out("Only empty line selected.")
aegisub.cancel()
end
--Space number half-width
for space in string.gmatch(text_stripped," ") do
SpaceNum=SpaceNum+1
end
--Space number full-width
for space in string.gmatch(text_stripped," ") do
SpaceNum=SpaceNum+1
fullWidthSpaceNum=fullWidthSpaceNum+1
end
--English words number
for word in string.gmatch(text_stripped,"(%w+)") do
EngWordsNum=EngWordsNum+1
end
--Non English words number
for utf8char in string.gmatch(text_stripped,"[%z\194-\244][\128-\191]*") do
NonEngCharsNum=NonEngCharsNum+1
end
--Line length
LineLength[i]=unicode.len(text_stripped)
--Line duration
LineDuration[i]=(l.end_time-l.start_time)/1000
TotalDuration=TotalDuration+LineDuration[i]
end
end
TotalWordsNum=EngWordsNum+NonEngCharsNum-fullWidthSpaceNum
NonEngCharsNum=NonEngCharsNum-fullWidthSpaceNum
for i,v in pairs(BoundaryKDurOfLine) do
if v.min<min_k.dur then
min_k.dur=v.min
min_k.line_index=i-dialogue_start+1
end
if v.max>max_k.dur then
max_k.dur=v.max
max_k.line_index=i-dialogue_start+1
end
end
max_k.dur=math.floor(max_k.dur*10+0.5)
min_k.dur=math.floor(min_k.dur*10+0.5)
for i,v in pairs(LineDuration) do
if v>=MaxLineDuration then
MaxLineDuration=v
MaxDurationIndex=i-dialogue_start+1
end
if v<=MinLineDuration and v>0 then
MinLineDuration=v
MinDurationIndex=i-dialogue_start+1
end
end
for i,v in pairs(LineLength) do
if v>=MaxLineLength then
MaxLineLength=v
MaxLengthIndex=i-dialogue_start+1
end
if v<=MinLineLength and v>0 then
MinLineLength=v
MinLengthIndex=i-dialogue_start+1
end
end
show_result()
end
function show_result()
if not karaoke or min_k.dur==3600000 or max_k.dur==0 then
max_k.dur='Not Valid'
min_k.dur='Not Valid'
max_k.line_index='Not Valid'
min_k.line_index='Not Valid'
elseif TotalLineNum==1 then
max_k.line_index='Not Valid'
min_k.line_index='Not Valid'
end
config = {
{x=0, y=0, class="label", label="Results: "},
{x=0, y=1, class="label", label=" Selected Line Num: "..TotalLineNum},
{x=0, y=2, class="label", label=" Total Duration: "..TotalDuration},
{x=0, y=3, class="label", label=" Total Syl Num: "..TotalKNum},
{x=0, y=4, class="label", label=" Total Words(Space counted): "..TotalWordsNum},
{x=0, y=5, class="label", label=" English Words Num: "..EngWordsNum},
{x=0, y=6, class="label", label=" Non-English Char Num: "..NonEngCharsNum},
{x=0, y=7, class="label", label=" Half-Width Space Num: "..SpaceNum-fullWidthSpaceNum},
{x=0, y=8, class="label", label=" Full-Width Space Num: "..fullWidthSpaceNum},
{x=6, y=1, class="label", label=" Max Line Length: "..MaxLineLength.." | Index: "..MaxLengthIndex},
{x=6, y=2, class="label", label=" Min Line Length: "..MinLineLength.." | Index: "..MinLengthIndex},
{x=6, y=3, class="label", label=" Max Line Duration: "..MaxLineDuration.." (s) | Index: "..MaxDurationIndex},
{x=6, y=4, class="label", label=" Min Line Duration: "..MinLineDuration.." (s) | Index: "..MinDurationIndex},
{x=6, y=5, class="label", label=" Max Syl Duration: "..max_k.dur.." (ms) | Index: "..max_k.line_index},
{x=6, y=6, class="label", label=" Min Syl Duration: "..min_k.dur.." (ms) | Index: "..min_k.line_index},
}
btn, result = aegisub.dialog.display(config,{"OK","Save"})
if btn=="Save" then
scriptname=string.sub(aegisub.file_name(),1,-5)
file_name=aegisub.dialog.save("Save to",aegisub.decode_path("?script").."\\", scriptname.."_stats", "*.csv",false)
if not file_name then aegisub.cancel() end
file=io.open(file_name,"w")
file:write("\239\187\191"
.."Selected Line Num,"..TotalLineNum.."\n"
.."Total Duration,"..TotalDuration.."\n"
.."Total Syl Num,"..TotalKNum.."\n"
.."Total Words(Space counted),"..TotalWordsNum.."\n"
.."English Words Num,"..EngWordsNum.."\n"
.."Non-English Char Num,"..NonEngCharsNum.."\n"
.."Half-Width Space Num,"..SpaceNum-fullWidthSpaceNum.."\n"
.."Full-Width Space Num,"..fullWidthSpaceNum.."\n"
.."Max Line Length,"..MaxLineLength..",Index,"..MaxLengthIndex.."\n"
.."Min Line Length,"..MinLineLength..",Index,"..MinLengthIndex.."\n"
.."Max Line Duration,"..MaxLineDuration..",Index,"..MaxDurationIndex.."\n"
.."Min Line Duration,"..MinLineDuration..",Index,"..MinDurationIndex.."\n"
.."Max Syl Duration,"..max_k.dur..",Index,"..max_k.line_index.."\n"
.."Min Syl Duration,"..min_k.dur..",Index,"..min_k.line_index.."\n"
)
file:close()
end
end
function k_stat(text,i)
k_dur_t={}
kNum=0
for kdur in string.gmatch(text,"\\[kK][fo]?(%d+)") do
kNum=kNum+1
if tonumber(kdur)>k_threshold then
k_dur_t[#k_dur_t+1]=kdur
end
end
BoundaryKDurOfLine[i]={max=math.max(unpack(k_dur_t)),min=math.min(unpack(k_dur_t)),kNum=kNum}
return BoundaryKDurOfLine
end
aegisub.register_macro(script_name, script_description,text_stat)