Created
January 21, 2019 00:18
-
-
Save atweiden/23c881c066d2a5aafecd2378f5f4c2ab to your computer and use it in GitHub Desktop.
Practice hiragana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
=begin pod | |
=head Learn Hiragana | |
=head3 L<Tofugu: Learn Hiragana|https://www.tofugu.com/japanese/learn-hiragana/> | |
=begin markdown | |
letter | sound | kana | memorization | |
--- | --- | --- | --- | |
a | "ah" | あ | look for the capital *A* | |
i | "ee" | い | a couple *eels* hanging out | |
u | "oo" | う | notice the *U* shape right in it | |
e | "ey" | え | think of it like an *e*xotic bird | |
o | "oh" | お | see the letter *o* in here, two times | |
=end markdown | |
=end pod | |
constant %HIRAGANA = | |
a => 'あ', | |
i => 'い', | |
u => 'う', | |
e => 'え', | |
o => 'お'; | |
my class Answer | |
{ | |
has Str:D $.text is required; | |
has UInt:D $.num is required; | |
} | |
my class Prompt | |
{ | |
has Str:D $.question is required; | |
has Answer:D $.answer is required; | |
} | |
multi sub gen-prompt(Bool:D :en($)! where .so --> Prompt:D) | |
{ | |
my Str:D $jp = pick-hiragana(:jp); | |
my Str:D $question-text = | |
sprintf(Q{The hiragana is: %s. What is the English spelling?}, $jp); | |
my Str:D @choice = %HIRAGANA.keys.pick(*); | |
my Str:D $fmt-choice = fmt-choice(@choice); | |
my Str:D $question = qq:to/EOF/.trim; | |
$question-text | |
$fmt-choice | |
EOF | |
my Str:D $answer-text = | |
%HIRAGANA.grep({ .values.first eq $jp }).values.first.keys.first; | |
my UInt:D $answer-num = @choice.first({ $_ eq $answer-text }, :k); | |
my Answer $answer .= new(:text($answer-text), :num($answer-num)); | |
my Prompt $prompt .= new(:$question, :$answer); | |
} | |
multi sub gen-prompt(Bool:D :jp($)! where .so --> Prompt:D) | |
{ | |
my Str:D $en = pick-hiragana(:en); | |
my Str:D $question-text = | |
sprintf(Q{The English spelling is: %s. What is the hiragana?}, $en); | |
my Str:D @choice = %HIRAGANA.values.pick(*); | |
my Str:D $fmt-choice = fmt-choice(@choice); | |
my Str:D $question = qq:to/EOF/.trim; | |
$question-text | |
$fmt-choice | |
EOF | |
my Str:D $answer-text = | |
%HIRAGANA.grep({ .keys.first eq $en }).values.first.values.first; | |
my UInt:D $answer-num = @choice.first({ $_ eq $answer-text }, :k); | |
my Answer $answer .= new(:text($answer-text), :num($answer-num)); | |
my Prompt $prompt .= new(:$question, :$answer); | |
} | |
multi sub pick-hiragana(Bool:D :en($)! where .so --> Str:D) | |
{ | |
my Str:D $en = %HIRAGANA.pick.keys.first; | |
} | |
multi sub pick-hiragana(Bool:D :jp($)! where .so --> Str:D) | |
{ | |
my Str:D $jp = %HIRAGANA.pick.values.first; | |
} | |
multi sub fmt-choice(Str:D @choice --> Str:D) | |
{ | |
my Str:D $fmt-choice = | |
@choice.kv.map(-> UInt:D $elem, Str:D $choice { | |
my Str:D $line = fmt-choice($elem, $choice); | |
}).join("\n"); | |
} | |
multi sub fmt-choice(UInt:D $elem, Str:D $choice --> Str:D) | |
{ | |
my Str:D $fmt-choice = sprintf(Q{%s. %s}, $elem, $choice); | |
} | |
sub fmt-answer(Answer:D $answer --> Str:D) | |
{ | |
my UInt:D $num = $answer.num; | |
my Str:D $text = $answer.text; | |
my Str:D $fmt-answer = sprintf(Q{Answer: 「%s. %s」}, $num, $text); | |
} | |
sub practice(--> Nil) | |
{ | |
my Bool:D %mode{Str:D} = :en, :jp; | |
my Prompt:D $prompt = gen-prompt(|%mode.pick); | |
my Answer:D $answer = $prompt.answer; | |
my Str:D $fmt-answer = fmt-answer($answer); | |
say($prompt.question); | |
my UInt:D $res = prompt(Q{Your response: }); | |
say($fmt-answer); | |
} | |
practice(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment