aboutsummaryrefslogtreecommitdiff
path: root/utils/bushu.awk
blob: 558a2bc785507dbed9143ba166de1ecc09e7633b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# $Id$

function add(num, code) {
    bushu[bushu_num] = num
    if (key[num]) {
	if (0+key[num] > 0) {
	    vkey[vkey_num++] = 0;
	    vkey[vkey_num] = key[num]
       	    key[num] = -vkey_num;
	    vkey_num++;
	}
	vkey[vkey_num++] = numstrokes;
    } else
	key[num] = numstrokes
    charcode[bushu_num] = code
    numkeys[numstrokes]++
    bushu_num++
}

BEGIN {
	FS="( +)|[ \t-]"
	first = 10000
	last = 0
	bushu_num = 1
}

/^#.*$/ {next}
       
/\=[0-9][0-9]*/ {
	numstrokes = substr($1, 2)
	if (0+numstrokes > 0+maxstrokes) 
		maxstrokes = numstrokes;
	next
}

/[0-9][0-9]*[ \t][ \t]*[0-9][0-9]*/ {
        add($1, $2)
	if (first > $1)
		first = $1
	if (last < $1)
		last = $1
	next
}

/[0-9][0-9]*/ {
        add($1, bushu_num)
	if (first > $1)
		first = $1
	if (last < $1)
		last = $1
	next
}

/^[ \t]*$/ {next}

{
	printf "%d: ignored\n", NR > "/dev/stderr"
}

END {
    print "/* This file is automatically generated."
    print " * To change it edit file `bushu.dat' and run"
    print " * awk -f bushu.awk bushu.dat > bushu.c"
    print " */"	
    print
    print "#ifdef HAVE_CONFIG_H"
    print "# include <config.h>"
    print "#endif"
    print "#include <stdlib.h>"
    print "#include <stdio.h>"
    print "#include <dict.h>"
    print "#include <dico.h>"
    print "#include <bushu.h>"
    print
    printf "int bushu_last = %d;\n", last
    printf "int max_bushu_strokes = %d;\n", maxstrokes
    printf "int bushu_cnt = %d;\n", bushu_num
    print

    print "/* Bushu numbers. Indexed with the internal bushu number */"
    printf "int bushu_num[] = {"
    col = 10
    for (i = 0; i < bushu_num; i++) {
	if (col == 10) {
	    printf "\n\t"
	    col = 0
	}
	printf "%3d, ", bushu[i] 
	col++
    }
    printf "\n};\n"


    print "/* Number of strokes for each bushu */"
    print "/* NOTE: i. This table is indexed with the real bushu number */"
    print "/*       ii.Negative entry means the radical has several */"
    print "/*          forms/stroke counts. The absolute value of the entry */"
    print "/*          serves as index to bushu_var_strokes table */"
    printf "int bushu_stroke[] = {"
    col = 10
    for (i = 0; i <= last; i++) {
	if (col == 10) {
	    printf "\n\t"
	    col = 0
	}
	printf "%3d, ", key[i] 
	col++
    }
    printf "\n};\n"

    print "/* Number of strokes for v bushu */"
    printf "int bushu_var_stroke[] = {"
    col = 10
    for (i = 0; i <= vkey_num; i++) {
	if (col == 10) {
	    printf "\n\t"
	    col = 0
	}
	printf "%3d, ", vkey[i] 
	col++
    }
    printf "\n};\n"

# Compute startpoints
    num = 1
    for (i = 1; i <= 0+maxstrokes; i++) {
	start[i] = num
	num += numkeys[i]
    }
    start[i] = num

    print "/* bushu_index[i] contains the internal index of the first bushu"
    print " * that has i strokes. "
    print " */"
    printf "int bushu_index[] = {"
    col = 10
    for (i = 0; i <= maxstrokes+1; i++) {
	if (col == 10) {
	    printf "\n\t"
	    col = 0
	}
	printf "%3d, ", start[i] 
	col++
    }
    print "\n};\n"

    print "/* Number of radicals that have given number of strokes"
    print " */"
    printf "int bushu_count[] = {"
    col = 10
    maxcount = 0
    for (i = 0; i <= maxstrokes+0; i++) {
	if (col == 10) {
	    printf "\n\t"
	    col = 0
	}
	printf "%3d, ", start[i+1] - start[i]
	if (start[i+1] - start[i] > maxcount)
	    maxcount = start[i+1] - start[i];
	col++
    }
    print "\n};\n"
    printf "int max_bushu_count = %d;\n\n", maxcount
	
    printf "/* Strings representing each radical (for use with bushu.pcf) */\n"
    printf "char bushu_string[%d][2] = {", bushu_num+1
    col = 5	
    for (i = 0; i <= bushu_num; i++) {
	if (col == 5) {
	    printf "\n\t"
	    col = 0
	}
	printf "{ %4d, 0 },", 0+charcode[i]
	col++
    }
    printf "\n};\n"
}

Return to:

Send suggestions and report system problems to the System administrator.