整数を英語に変換

整数を英語に変換 - みずぴー日記 にインスパイヤされて整数を英語表記にしてみます。 元になっているのは anarchy golf - 100 ですが、私はゴルフが苦手なので、自分が脳内で行っている思考をダンプしてみました。

つまり、以下のようなものです。

  • 0 から 20 は覚える必要があるので、覚える
  • 21 から 29 までは twenty に 1 桁目の整数を英語にしたものを付ける
  • 30 以上は、30 なら 2 桁目の 3 に 10 を足した 13 (thirteen) の "een" を "ty" にしたものに、1 桁目の整数を英語にしたものを付ける

これを素直に書き下しただけです。

#! /usr/local/bin/nawk -f
# int2eng.awk
# 整数を英語に変換
# usage: nawk -f int2eng.awk

BEGIN {

    for (i = 0; i <= 99; i++) {

        print i " = " int2eng(i);

    }

}

# int2eng():    整数を英語に変換する
#   input:      数値 num
#   output:     整数 num を英語表記にした文字列
function int2eng(num,    table, arr_num, second_digit, first_digit) {

    table[0]    = "zero";
    table[1]    = "one";
    table[2]    = "two";
    table[3]    = "three";
    table[4]    = "four";
    table[5]    = "five";
    table[6]    = "six";
    table[7]    = "seven";
    table[8]    = "eight";
    table[9]    = "nine";
    table[10]   = "ten";
    table[11]   = "eleven";
    table[12]   = "twelve";
    table[13]   = "thirteen";
    table[14]   = "fourteen";
    table[15]   = "fifteen";
    table[16]   = "sixteen";
    table[17]   = "seventeen";
    table[18]   = "eighteen";
    table[19]   = "nineteen";
    table[20]   = "twenty";

    # 20 以下の場合はそのまま table を使用
    if (num <= 20) {

        return table[num];

    # # 29 以下は十の位だけ table[20] を使用
    } else if (num <= 29) {

        split(i, arr_num, "");

        second_digit    = table[20];
        first_digit     = arr_num[2] ? table[arr_num[2]] : "";

        return second_digit " " first_digit;

    # 30 以上は "een" を "ty" に変換
    } else {

        split(i, arr_num, "");

        second_digit    = substr(table[arr_num[1] + 10], 1, \
                            length(table[arr_num[1] + 10]) - 4) "ty";
        first_digit     = arr_num[2] ? table[arr_num[2]] : "";

        return second_digit " " first_digit;

    }
}

では実行してみます。

$ nawk -f int2eng.awk
0 = zero
1 = one
2 = two
3 = three
4 = four
5 = five
6 = six
7 = seven
8 = eight
9 = nine
10 = ten
11 = eleven
12 = twelve
13 = thirteen
14 = fourteen
15 = fifteen
16 = sixteen
17 = seventeen
18 = eighteen
19 = nineteen
20 = twenty
21 = twenty one
22 = twenty two
23 = twenty three
24 = twenty four
25 = twenty five
26 = twenty six
27 = twenty seven
28 = twenty eight
29 = twenty nine
30 = thirty 
31 = thirty one
32 = thirty two
33 = thirty three
34 = thirty four
35 = thirty five
36 = thirty six
37 = thirty seven
38 = thirty eight
39 = thirty nine
40 = fourty 
41 = fourty one
42 = fourty two
43 = fourty three
44 = fourty four
45 = fourty five
46 = fourty six
47 = fourty seven
48 = fourty eight
49 = fourty nine
50 = fifty 
51 = fifty one
52 = fifty two
53 = fifty three
54 = fifty four
55 = fifty five
56 = fifty six
57 = fifty seven
58 = fifty eight
59 = fifty nine
60 = sixty 
61 = sixty one
62 = sixty two
63 = sixty three
64 = sixty four
65 = sixty five
66 = sixty six
67 = sixty seven
68 = sixty eight
69 = sixty nine
70 = seventy 
71 = seventy one
72 = seventy two
73 = seventy three
74 = seventy four
75 = seventy five
76 = seventy six
77 = seventy seven
78 = seventy eight
79 = seventy nine
80 = eighty 
81 = eighty one
82 = eighty two
83 = eighty three
84 = eighty four
85 = eighty five
86 = eighty six
87 = eighty seven
88 = eighty eight
89 = eighty nine
90 = ninety 
91 = ninety one
92 = ninety two
93 = ninety three
94 = ninety four
95 = ninety five
96 = ninety six
97 = ninety seven
98 = ninety eight
99 = ninety nine

行末にスペースがあるのは多めに見てください。

上のプログラムには間違いがあります (2010-06-14)

40 は fourty ではなく forty という指摘をいただきました。 その通りなのですが、ここではプログラムの簡略性を生かすため、故意にこのまま放置しておきます。

tag_nawk.png tag_nawk.png tag_nawk.png tag_nawk.png