Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Also handle small tsu + small ya/yu/yo
Browse files Browse the repository at this point in the history
  • Loading branch information
kinbiko committed Jun 1, 2019
1 parent bdda7cd commit bc32a09
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
46 changes: 37 additions & 9 deletions r2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,39 @@ var r2k = map[string]kana{
"ryu": {val: "りゅ", skip: 3},
"ryo": {val: "りょ", skip: 3},

"t": {val: "っ", skip: 1},
"t": {val: "っ", skip: 1},
"kka": {val: "っか", skip: 3},
"kki": {val: "っき", skip: 3},
"kku": {val: "っく", skip: 3},
"kke": {val: "っけ", skip: 3},
"kko": {val: "っこ", skip: 3},
"kkya": {val: "っきゃ", skip: 4},
"kkyu": {val: "っきゅ", skip: 4},
"kkyo": {val: "っきょ", skip: 4},
"ssa": {val: "っさ", skip: 3},
"sshi": {val: "っし", skip: 4},
"ssu": {val: "っす", skip: 3},
"sse": {val: "っせ", skip: 3},
"sso": {val: "っそ", skip: 3},
"ssha": {val: "っしゃ", skip: 4},
"sshu": {val: "っしゅ", skip: 4},
"ssho": {val: "っしょ", skip: 4},
"tta": {val: "った", skip: 3},
"cchi": {val: "っち", skip: 4},
"ttsu": {val: "っつ", skip: 4},
"tte": {val: "って", skip: 3},
"tto": {val: "っと", skip: 3},
"ccha": {val: "っちゃ", skip: 4},
"cchu": {val: "っちゅ", skip: 4},
"ccho": {val: "っちょ", skip: 4},
"ppa": {val: "っぱ", skip: 3},
"ppi": {val: "っぴ", skip: 3},
"ppu": {val: "っぷ", skip: 3},
"ppe": {val: "っぺ", skip: 3},
"ppo": {val: "っぽ", skip: 3},
"ppya": {val: "っぴゃ", skip: 4},
"ppyu": {val: "っぴゅ", skip: 4},
"ppyo": {val: "っぴょ", skip: 4},
}

//ToKana returns the kana equivalent of the given hepburn romaji string.
Expand All @@ -151,14 +183,10 @@ func ToKana(in string) (string, error) {

func sub(in string) (kana, error) {
var k kana
if len(in) >= 3 {
k = r2k[in[len(in)-3:]]
}
if k.val == "" && len(in) >= 2 {
k = r2k[in[len(in)-2:]]
}
if k.val == "" {
k = r2k[in[len(in)-1:]]
for l := 4; k.val == "" && l > 0; l-- {
if len(in) >= l {
k = r2k[in[len(in)-l:len(in)]]
}
}
if k.val == "" {
return kana{}, fmt.Errorf("could not find right-most kana for '%s'", in)
Expand Down
7 changes: 6 additions & 1 deletion r2k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func TestSuccessCases(t *testing.T) {
{in: "nandeyanen", exp: "なんでやねん"},
{in: "chuushajou", exp: "ちゅうしゃじょう"},
{in: "chottomatte", exp: "ちょっとまって"},
{in: "sakki", exp: "さっき"},
{in: "massugu", exp: "まっすぐ"},
{in: "mettani", exp: "めったに"},
{in: "happyou", exp: "はっぴょう"},
}
for _, tc := range tt {
t.Run(tc.in, func(st *testing.T) {
Expand All @@ -37,7 +41,8 @@ func TestSuccessCases(t *testing.T) {

func TestNegativeCases(t *testing.T) {
tt := []struct{ exp, in string }{
{in: "fish", exp: "parse error: parsed up until '' when getting 'could not find right-most kana for 'fish''"},
{in: "fishu", exp: "parse error: parsed up until 'しゅい' when getting 'could not find right-most kana for 'f''"},
{in: "-", exp: "parse error: parsed up until '' when getting 'could not find right-most kana for '-''"},
}
for _, tc := range tt {
t.Run(tc.in, func(st *testing.T) {
Expand Down

0 comments on commit bc32a09

Please sign in to comment.