I tried this code:
var:number vProgress = sum(children,$WordCount + $Name.split("[ |,]").count);
var:string vPct = ((vProgress/$TargetNumber)*100).format(2);
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
Automatic word count and percentage statistics-demo (1).tbx (108.7 KB)
In the screenshot below, the content of $name is “你好,你好”, which is 4 words, but the split of the code is separated by symbols. It actually counts 2 words, which is inaccurate.
I asked ChatGPT, which gave several pieces of code, but could not count the number of $Name words
Compared with the following paragraph, tinderbo does not support the length method:
var:number vProgress = sum(children,$WordCount + $Name.length(" ").count);
var:string vPct = ((vProgress/$TargetNumber)*100).format(1) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
In this code, it tries to use a match function to count the number of $Name words, but it can’t either:
var:number vProgress = sum(children,$WordCount);
var:number vName = sum(children,match($Name, "[\\u4e00-\\u9fa5]").count);
var:string vPct = ((vProgress+vName/$TargetNumber)*100).format(1) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
It tries to use Javascript, but it can’t:
var:number vChineseCount = javascript("$Name.match(/[\u4e00-\u9fa5]/g).length;");
var:number vProgress = sum(children,$WordCount + vChineseCount);
var:string vPct = ((vProgress/$TargetNumber)*100).format(1) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
It tries to pass a regular expression, but it can’t:
var:number vProgress = sum(children,$WordCount + $Name.replace(/[^\x00-\xff]/g, "xx").length/2);
var:string vPct = ((vProgress/$TargetNumber)*100).format(2) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
It tries to split the string $Name into character arrays and count them, but it can’t either:
var:array vNameArray = $Name.toArray();
var:number vCharCount = 0;
for each (vChar in vNameArray) {
vCharCount++;
}
var:number vProgress = $WordCount + vCharCount;
var:string vPct = ((vProgress/$TargetNumber)*100).format(1) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
它认为tinderbox有一种内置的$Chars属性获取字符数,也不行:
var:number vProgress = sum(children,$Chars);
var:string vPct = ((vProgress/$TargetNumber)*100).format(2) + '%';
$ReportString = $Name;
$ReportString += " ("+ $TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
And custom functions:
var vProgress = 0;
var children = children(this);
for (var i = 0; i < children.length; i++) {
vProgress += children[i].getAttribute("WordCount");
var name = children[i].getAttribute("Name");
for (var j = 0; j < name.length; j++) {
if (name.charCodeAt(j) > 255) {
vProgress += 2;
} else {
vProgress += 1;
}
}
}
var vPct = ((vProgress/TargetNumber)*100).toFixed(2) + '%';
$ReportString = $Name;
$ReportString += " ("+ TargetNumber+"/"+vProgress+") ";
$ReportString += vPct;
$Pattern = "bar("+vPct+")";
And this:
var title = "$Name";
var titleLength = 0;
for (var i = 0; i < title.length; i++) {
if (title.charCodeAt(i) >= 0 && title.charCodeAt(i) <= 128) {
titleLength += 1;
} else {
titleLength += 2;
}
}
var vProgress = sum(children, $WordCount) + titleLength;
var vPct = ((vProgress / $TargetNumber) * 100).toFixed(2) + '%';
$ReportString = $Name;
$ReportString += " (" + $TargetNumber + "/" + vProgress + ") ";
$ReportString += vPct;
$Pattern = "bar(" + vPct + ")";
It seems that there is no effective way to count the number of Chinese words of $Name