Rosetta Code/Count examples: Difference between revisions

→‎{{header|Raku}}: Update code to latest bells and whistles version
m (→‎{{header|zkl}}: update regexp)
(→‎{{header|Raku}}: Update code to latest bells and whistles version)
Line 2,251:
Retrieves counts for both Tasks and Draft Tasks. Save / Display results as a sortable wikitable rather than a static list. Click on a column header to sort on that column. To do a secondary sort, hold down the shift key and click on a second column header. Tasks have a gray (default) background, Draft Tasks have a yellow background.
 
This example tracks the top 50 languages by number of examples completed but only generated reports in tiers of 10. More than 10 gets too heavy and difficult to navigate.
For a full output, see [[Rosetta Code/Count examples/Full list]]
 
Feed it parameters to control behaviour:
* :nf or :no-fetch to use saved information to generate the different reports. Recommended for the second, third, fourth, whatever report you are generating at any one time as fetching from the site is ''by far'' the most time consuming part.
* :t=1 (or :tier=1). :t=2, :t=3, whatever, up to :t=5 to control which tier of results to generate.
 
Language names with spaces will have underscored inserted to make the results uniform. Some oddball languages (lookin' at you Icon and Unicon) make it very difficult to obtain accurate results. The whole "conglomerate all of the Basic dialects together" movement of a few yeas ago also complicates things, but this should be pretty close.
 
For a full output, see [[Rosetta_Code/Count_examples/Full_list|Top tier]], [[Rosetta_Code/Count_examples/Full_list/Tier_2|Second tier]], [[Rosetta_Code/Count_examples/Full_list/Tier_3|Third tier]], [[Rosetta_Code/Count_examples/Full_list/Tier_4|Fourth tier]]
<lang perl6>use HTTP::UserAgent;
use URI::Escape;
use JSON::Fast;
use Lingua::EN::Numbers :short;
 
unit sub MAIN ( StrBool :nf(:$no-fetch) = ''False, :t(:$tier) = 1 );
 
# Friendlier descriptions for task categories
Line 2,265 ⟶ 2,274:
 
my $client = HTTP::UserAgent.new;
$client.timeout = 10;
 
my $url = 'http://rosettacode.org/mw';
 
my $hashfile = './RC_Task_count.json';
my $tablefile = '"./RC_Task_count-{$tier}.txt'";
 
my %tasks;
 
my @places = <① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ⑯ ⑰ ⑱ ⑲ ⑳ ㉑ ㉒ ㉓ ㉔ ㉕
㉖ ㉗ ㉘ ㉙ ㉚ ㉛ ㉜ ㉝ ㉞ ㉟ ㊱ ㊲ ㊳ ㊴ ㊵ ㊶ ㊷ ㊸ ㊹ ㊺ ㊺ ㊻ ㊼ ㊽ ㊾ ㊿>;
 
# clear screen
run($*DISTRO.is-win ?? 'cls' !! 'clear') unless $no-fetch;
 
my %counts =
Line 2,287 ⟶ 2,300:
.map({ .<title>.subst(/^'Category:'/, '') => .<categoryinfo><pages> || 0 });
 
my $per-tier = 10;
my @top10 = %counts.sort( -*.value ).head(10).map: *.key;
 
my $which = (^$per-tier) »+» $per-tier * ($tier - 1);
 
my @top10top-n = %counts.sort( {-*.value, ).head(10key} )[|$which].map: *.key.trans(' ' => '_');
 
# dump a copy to STDOUT, mostly for debugging purposes
say '"<pre>Top{tc ten$tier.&ord-n} {$per-tier.&card} programming languages by number of task examples completed:'";
say "{(' ', ..join ' ', .map( {("{(@places[|$which])[$_]} {@top10top-n[$_]}").fmt("%-15s")} ) for (^@top10top-n).batch(5);
say "</pre>\n";
 
Line 2,299 ⟶ 2,316:
 
mkdir('./pages') unless './pages'.IO.e;
 
@top-n = %counts.sort( {-.value, .key} )[^@places].map: *.key.trans(' ' => '_');;
 
for %cat.keys.sort -> $cat {
Line 2,309 ⟶ 2,328:
:prop<title>
).map({
my $page =;
my $response;
$client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
 
"./pages/{ uri-escape .<title>.subst(/' '/, '_', :g) } ".IO.spurt($page);
myloop $lc = $page.lc;{
$response = $client.get("{ $url }/index.php?title={ uri-escape .<title> }&action=raw").content;
if $response.is-success {
$page = $response.content;
('①' .. '⑩')[$_] !! ' 'last;
} else {
redo;
}
}
 
"./pages/{ uri-escape .<title>.subst(/' '/, '_', :g) } ".IO.spurt($page);
my $lc = $page.lc.trans(' ' => '_');
my $count = +$lc.comb(/ ^^'==' <-[\n=]>* '{{header|' <-[}]>+? '}}==' \h* $$ /);
%tasks{.<title>} = {'cat' => %cat{$cat}, :$count};
%tasks{.<title>}<top10top-n> = (^@top10top-n).map( {
($lc.contains("\n==\{\{header|{@top10top-n[$_].lc}}}==") ??or
# Deal with 3 part headers - {{header|F_Sharp|F#}}, {{header|C_Sharp|C#}}, etc.
('①' .. '⑩')[$_] !! ' '
$lc.contains("==\{\{header|{@top-n[$_].lc}|") or
# Icon and Unicon are thier own special flowers
$lc.contains("}}_and_\{\{header|{@top-n[$_].lc}}}==")) ??
(@places[$_]) !! ' '
} ).join;
print clear, 1 + $++, ' ', %cat{$cat}, ' ', .<title>;
Line 2,332 ⟶ 2,366:
# Load information from local file
%tasks = $hashfile.IO.e ?? $hashfile.IO.slurp.&from-json !! ( );
 
@top-n = %counts.sort( {-.value, .key} )[|$which].map: *.key.trans(' ' => '_');
 
# Convert saved task info to a table
Line 2,343 ⟶ 2,379:
my $out = open($tablefile, :w) or die "$!\n";
 
$out.say: '"<pre>Top{tc ten$tier.&ord-n} {$per-tier.&card} programming languages by number of task examples completed:'";
$out.say: "{(' ', ..join ' ', .map( {("{(@places[|$which])[$_]} {@top10top-n[$_]}").fmt("%-15s")} ) for (^@top10top-n).batch(5);
$out.say: "</pre>\n\n<div style=\"height:40em;overflow:scroll;\">";
 
Line 2,352 ⟶ 2,388:
"|+ As of { DateTime.new(time) } :: Tasks: { $taskcnt } ::<span style=\"background-color:#ffd\"> Draft Tasks:",
"{ $draftcnt } </span>:: Total Tasks: { $count } :: Total Examples: { $total }\n",
"!Count!!Task!!{('①' .. '⑩'@places[|$which]).join('!!')}"
;
 
Line 2,367 ⟶ 2,403:
!! "| [[{uri-escape $task.key}|{$task.key}]]\n"
),
"|{ $task.value<top10top-n>.comb[|$which].join('||') }"
}
 
10,327

edits