Skip to content

Instantly share code, notes, and snippets.

@artemgurzhii
Created September 16, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemgurzhii/973b937e801f8b470686dd2299f6507b to your computer and use it in GitHub Desktop.
Save artemgurzhii/973b937e801f8b470686dd2299f6507b to your computer and use it in GitHub Desktop.
Train words

I'm using this program to improve my skills in different languages

How

All of the diacritic characters(czech, slovak, etc languages) are replaced with their analogues from the latin abc. š -> s; ě -> e So you don't have to worry about perfect character matching.

Usage

perl learn-languages.pl some-path.txt some-other-path/file.txt

Mine files are written in the following format {x} - {y} where x - word you want to learn, y - translation in your native language

Example

překladač - compiler tlumočník - interpreter ověření - validation etc... - etc...

use strict;
use warnings;
use diagnostics;
use List::Util qw( shuffle );
use Unicode::Diacritic::Strip 'strip_diacritics';
use v5.18;
my $HELP_MESSAGE = <<"END";
List of files from where to use words
Example:
perl learn-languages.pl words/0-25.txt words/26-50.txt
END
sub get_files {
my @files = ();
push @files, $_ foreach (@ARGV);
return @files;
}
sub generate_list_of_words {
my @words = ();
my @files = @{$_[0]};
foreach my $path (@files) {
open my $fh, '<:encoding(UTF-8)', $path or die "Can't open file: $_";
while (my $line = <$fh>) {
chomp($line);
my @arr = generate_translation($line);
push @words, \@arr;
}
close $fh or die "Couldn't close file: $_";
}
return @words;
}
sub generate_translation {
my $line = shift;
chomp $line;
$line = strip_diacritics $line;
return split / - /, $line;
}
sub train_words {
my @translations = @{$_[0]};
for (my $i = 0; $i <= scalar(@translations); $i++) {
print $translations[$i][1] . ': ';
chomp(my $guess = <STDIN>);
unless ($guess eq $translations[$i][0]) {
print "Inccorect, should be - " . $translations[$i][0] . "\n";
}
}
}
sub main {
my @files = get_files();
my @words = generate_list_of_words(\@files);
my @translations = shuffle(@words);
train_words(\@translations);
}
if ($ARGV[0] eq '--help') {
print $HELP_MESSAGE;
} else {
main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment