I started learning Japanese not too long ago, and I'm tired of Anki, so I'm making my own, simple flash card program. I want to be able to have the option to read the Japanese in romaji (e.g. "mizu" instead of "water"), or have it automatically converted to hiragana (e.g. "みず" instead of "mizu"). Is there a way for me to use bash to convert the romaji to hiragana (or possibly katakana)?
Basically, how hiragana works is that each letter represents a syllable, such as "da" (だ), with a single vowel, usually preceeded by a single consonant. So any word in romaji must be able to be broken down by the consonants, so "anata" must be separated into "a na ta", with the break at the start of the consonant, and then turned into あなた. However there are a few problems that make this more difficult. Hiragana like "shi" (し) have two consonant letters, but only one consonant sound, and the bash script must be able to recognize that. Even worse, in some situations the hiragana are pronounced differently depending on context, such as "wa" (わ) being spelled as "ha" (は) if it's being used as a particle, such as the "wa" in the sentence "anata wa shin setsu desu" (あなたはしんせつです) being spelled with "ha" (は instead of わ), as if it were "anata ha shin setsu desu".
Is there any (relatively easy) way to use bash to do this, or is it just too complicated for a simple script and I should just suck it up and create a third array for hiragana?
Code:#!/bin/bash # ローマ字array nihongo[1]='Itadakimasu' nihongo[2]='Oishii' nihongo[3]='Mama' nihongo[4]='Natto' nihongo[5]='Kanpai' nihongo[6]='Hajimemasite' nihongo[7]='Hajimeru' (etc...) # English array english[1]='Always used before eating, roughly means "I am about to receive".' english[2]='Delicious' english[3]='I don'\''t like this food (it'\''s so-so).' english[4]='Fermented soy beans with a very strong smell.' english[5]='Equivalent to the English "Cheers!" said before a drink.' english[6]='Roughly "Nice to meet you." (lit. "for the first time").' english[7]='To begin' (etc...) # There must be the same number of Japanese and English translations if [ ${#nihongo[*]} -ne ${#english[*]} ]; then echo 'Error, length of array variables "nihongo" and "english" do not match.' exit 1 fi correct=0 incorrect=0 # Display stats and exit function finished() { if ! [ $correct -eq 0 -a $incorrect -eq 0 ]; then clear let total=$correct+$incorrect percent=$(echo "scale=2; ($correct/$total)*100" | bc) echo -e "Correct: $correct\nIncorrect: $incorrect\nTotal: $total\nPercent: $percent%" else echo fi tput cnorm normal exit 0 } trap finished SIGHUP SIGINT SIGTERM while true; do clear last_rand=$rand # Make sure the same card isn't done twice in a row until [ "$rand" != "$last_rand" ]; do rand=$(shuf -i 1-${#nihongo[*]} -n 1) done echo "Nihongo: ${nihongo[$rand]}" read tput cup 1 # Dirty workaround because I'm too stupid to not echo a new line when enter is pressed echo "English: ${english[$rand]}" unset yn until [ "$yn" = y -o "$yn" = n ]; do echo read -p "Did you get it right? [y/n] " yn if [ "$yn" = y ]; then ((correct++)) echo 'Good job!' sleep 1 elif [ "$yn" = n ]; then ((incorrect++)) echo 'Oh well, maybe next time.' sleep 1 fi done done
Bookmarks