Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor a bit; start to record state (a bit broken atm)

  • Loading branch information...
commit 3937fa137014178395cf2b156f460c001be60eb3 1 parent 7428321
@moritz authored
Showing with 44 additions and 19 deletions.
  1. +44 −19 coach.pl
View
63 coach.pl
@@ -1,31 +1,50 @@
use v6;
-my $fn = 'data/words-no-de';
-
-
-my $file = open $fn;
-
-my %words;
+use JSON::Tiny;
-for $file.lines -> $l {
- my @lang = $l.split(/\s+ '-' \s+ /);
- if @lang != 2 {
- warn "Igoring line '$l'";
- next;
+sub read-dict($fn) {
+ my %words;
+ my $file = open $fn;
+ for $file.lines -> $l {
+ my @lang = $l.split(/\s+ '-' \s+ /);
+ if @lang != 2 {
+ warn "Igoring line '$l'";
+ next;
+ }
+ @lang>>.=trim;
+ if %words.exists(@lang[1]) {
+ warn "Ignoring second translation for @lang[1]"
+ ~ "('@lang[0]' vs. '%words{@lang[1]}'";
+ } else {
+ %words{@lang[1]} = @lang[0];
+ }
}
- @lang>>.=trim;
- if %words.exists(@lang[1]) {
- warn "Ignoring second translation for @lang[1]"
- ~ "('@lang[0]' vs. '%words{@lang[1]}'";
- } else {
- %words{@lang[1]} = @lang[0];
+
+ unless %words {
+ die "No valid lines in data file found";
}
+ return %words;
}
-unless %words {
- die "No valid lines in data file found";
+sub read-state($fn) {
+ return () unless $fn.IO.e;
+ my $contents = slurp $fn;
+ my $results = from-json($contents);
+ return $results.hash;
}
+sub save-state($fn, %state) {
+ my $file = open :w, "$fn.tmp";
+ $file.say: to-json(%state);
+ $file.close;
+ run("mv -f $fn.tmp $fn");
+}
+
+my $fn = 'data/words-no-de';
+
+my %words = read-dict($fn);
+my %state = read-state("$fn.state");
+
sub normalize($x) {
$x.trans([<å ø æ Å Ø Æ ä ö ü Ä Ö Ü>]
=> [<aa oe ae Aa Oe Ae ae oe ue Ae Oe Ue>]);
@@ -43,6 +62,12 @@ ($x)
say "$right :-) or :-/";
say "$wrong :-(";
printf "%.2f%% right\n", (100 * $right / ($right + $wrong));
+
+ %state<__meta__><right> += $right;
+ %state<__meta__><wrong> += $wrong;
+
+ save-state("$fn.state", %state);
+ say "hade bra!";
last;
}
if $response eq $fl {
Please sign in to comment.
Something went wrong with that request. Please try again.