-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt.pl
executable file
·69 lines (60 loc) · 1.28 KB
/
prompt.pl
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
#! /usr/bin/perl -w
# User configurable parameters:
my $TRAIN_FILE = "/Users/linse/Documents/cloned/anki-prompt/pagetext.txt";
my $TOP_ENTRIES = 100;
my $has_color = 0;
eval("use Color::Output");
if ($@) {
use Term::ANSIColor;
}else{
eval("Color::Output::Init");
$has_color = 1;
}
use strict;
# adjust the path to your facts file accordingly
open(MYINPUTFILE, "<$TRAIN_FILE"); # open for input
my(@lines) = <MYINPUTFILE>; # read file into list
my(@hanzi_list);
my $hanzi="";
my($line);
my $number=0;
shift @lines; # skip first one
foreach $line (@lines) {
if ($line =~ m/^\s+(\d+)(.+)/) {
if ($hanzi ne "") {
push(@hanzi_list, $hanzi);
}
$hanzi = "";
$hanzi .= $2;
}
else {
$line =~ s/\s+$//;
chomp $line;
$hanzi .= $line;
}
}
close(MYINPUTFILE);
# print one out of the first $TOP_ENTRIES ones, randomly
printHanzi($hanzi_list[rand $TOP_ENTRIES]);
# possibly adjust to the formatting of your facts
sub printHanzi {
my $input = shift;
while ($input =~ m/^\s*(\S*)\s*(\[\S+\])\s+([^\[]*)(.*)/) {
print("$1 ");
printRed("$2 ");
chomp($3);
print "$3";
$input = $4;
print "\n";
}
}
sub printRed {
my $input = shift;
if ($has_color) {
cprint ("\x034$input\x030");
} else{
print color 'red';
print $input;
print color 'reset';
}
}