# hepburn3.pl
# Converts Japanese text in "official" Monbushoo (Kunrei-shiki) romanization
#  to more common "Hepburn"-style romanization
# A list of differences can be found at:
#   http://en.wikipedia.org/wiki/Romaji
# This version tries to diagnose errors as under- vs over-application.

$input_file = "Japanese-ToConvert.txt";
open (INFILE, $input_file) or die "Warning! Can't open input file: $!\n";
$check_file = "Japanese-Converted.txt";
open (CHECKFILE, $check_file) or die "Warning! Can't open check file: $!\n";

while ($line = <INFILE>) {
    chomp($line);      
    $original = $line;        
    # Crucial rule ordering: this needs to go first
    $line =~ s/hu/fu/g;

    # The major difference is use of <y> after t,s,z
    $line =~ s/ty/ch/g;    
    $line =~ s/sy/sh/g;    
    $line =~ s/zy/j/g;    
    # Also, palatalization before i
    $line =~ s/ti/chi/g;    
    $line =~ s/si/shi/g;    
    $line =~ s/zi/ji/g;    
    # And assibilation of t before u
    $line =~ s/tu/tsu/g;        

    print "$line"; 

    # Now check answer against the "real" answer in the checkfile
    $check_line = <CHECKFILE>;    
    chomp($check_line);        
    if ($line eq $check_line) {
	print "\t(correct)\n";	
    } else {
	if ($check_line eq $original) {
	    # We changed something that we shouldn't have
	    print "\t(incorrect - accidentally modified <$original> when we shouldn't have\n";	    
	} else {
	    print "\t(incorrect - need to learn something to change <$original> to <$check_line> \n";	    
	}
	
    }
    
}