-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhira2kata
executable file
·394 lines (375 loc) · 12.5 KB
/
hira2kata
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/bin/sh
######################################################################
#
# HIRA2KATA - Convert from Hiragana into Katakana in the Selected Fields
#
# USAGE: hira2kata [+<n>h] <f1> <f2> ... <file>
# hira2kata -d <f1> <f2> ... <string>
#
# <fn> .... Time field number you want to convert
# <file> .. Text file which contains some time field to convert
# <string> It will be explained in -d option
# -d ...... Direct Mode :
# It make this command regard the last argument (<string>)
# as a field formatted string instead of <file>
# +<n>h ... Regards the top <n> lines as comment and Print without
# converting
#
# Designed originally by Nobuaki Tounaka
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-05-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [+<n>h] <f1> <f2> ... <file>
${0##*/} -d <f1> <f2> ... <string>
Options : -d ...... Direct Mode :
It make this command regard the last argument (<string>)
as a field formatted string instead of <file>
+<n>h ... Regards the top <n> lines as comment and Print without
converting
Version : 2020-05-06 22:42:19 JST
(POSIX.1 Bourne Shell/POSIX.1 commands/UTF-8)
USAGE
exit 1
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit $1
}
######################################################################
# Parse Arguments
######################################################################
# === Get the options and the filepath ===============================
# --- initialize option parameters -----------------------------------
directmode=0
directstr=''
file=''
fldnums=''
all_fields=0
use_stdin=0
opt_part=1
opth=0
#
# --- get them -------------------------------------------------------
case "$#${1:-}" in
1-h|1--help|1--version) print_usage_and_exit;;
esac
case $# in [!0]*)
i=0
for arg in ${1+"$@"}; do
i=$((i+1))
fldnum=''
# direct mode processing
if [ $opt_part -ne 0 ] && [ "_$arg" = '_-d' ]; then
directmode=1
continue
elif [ $opt_part -ne 0 ] && [ "_$arg" != "_${arg#+}" ]; then
s=$(printf '%s\n' "$arg" | sed 's/^+\([0-9]*\)h$/\1/')
case "$s" in "$arg") print_usage_and_exit;; esac
case "$s" in '') opth=1; continue ;; esac
opth=$(expr $s + 0)
continue
fi
opt_part=0
case "$i$directmode" in "${#}1") directstr=$arg; break;; esac
# separate arg to arg1, arg2
arg1=${arg%%/*}
arg2=${arg##*/}
if [ "_${arg1}/${arg2}" = "_$arg" ] && [ -n "$arg1" ] && [ -n "$arg2" ]
then
:
else
arg1=$arg
arg2=''
fi
# check both
j=0
for s in $arg1 $arg2; do
if printf '%s\n' "$s" | grep -q '^[0-9]\{1,\}$'; then
j=$((j+1))
case "$s" in 0) all_fields=1;; esac
elif printf '%s\n' "$s" | grep -Eq '^(NF|NF-[0-9]+)$'; then
j=$((j+1))
fi
done
if [ $j -eq 2 ] || ([ $j -eq 1 ] && [ -z "$arg2" ]); then
fldnums="$fldnums $arg"
continue
fi
# perhaps, this argument is a filename
case $# in $i) file=$arg; continue;; esac
# otherwise, it is a invalid argument
print_usage_and_exit
done
;;
esac
# === Validate the arguments =========================================
# (if you want to regard no fldnums at all, set all_fields=1 instead of exit)
case "$fldnums" in '') all_fields=1;; esac
if [ $directmode -ne 0 ]; then
case "$directstr" in '') print_usage_and_exit;; esac
file=''
elif [ "_$file" = '_' ] ||
[ "_$file" = '_-' ] ||
[ "_$file" = '_/dev/stdin' ] ||
[ "_$file" = '_/dev/fd/0' ] ||
[ "_$file" = '_/proc/self/fd/0' ] ; then
file=''
elif [ -f "$file" ] ||
[ -c "$file" ] ||
[ -p "$file" ] ; then
[ -r "$file" ] || error_exit 1 'Cannot open the file: '"$file"
case "$file" in /*|./*|../*) :;; *) file="./$file";; esac
else
print_usage_and_exit
fi
######################################################################
# Prepare for the Main Routine
######################################################################
# === Generate the partial code for AWK ==============================
case $all_fields in
0) awkc0=$(echo "$fldnums" |
sed 's/^0\{1,\}\([0-9]\)/\1/' |
sed 's/\([^0-9]\)0\{1,\}\([0-9]\)/\1\2/g' |
tr ' ' '\n' |
awk '
/^NF-[0-9]+\/NF-[0-9]+$/ {
nfofs1 = substr($0,4,index($0,"/")-4) + 0;
nfofs2 = substr($0,index($0,"/")+4) + 0;
if (nfofs1 > nfofs2) {
i = nfofs1;
nfofs1 = nfofs2;
nfofs2 = i;
}
for (i=nfofs1; i<=nfofs2; i++) {
print "0 NF-" i;
}
}
/^NF\/NF-[0-9]+$/ {
nfofs2 = substr($0,index($0,"/")+4);
print "0 NF";
for (i=1; i<=nfofs2; i++) {
print "0 NF-" i;
}
}
/^NF-[0-9]+\/NF$/ {
nfofs2 = substr($0,4,index($0,"/")-4) + 0;
print "0 NF";
for (i=1; i<=nfofs2; i++) {
print "0 NF-" i;
}
}
/^[0-9]+\/NF-[0-9]+$/ {
printf("0 %s NF-%s\n",
substr($0,1,index($0,"/")-1),
substr($0,index($0,"/")+4) );
}
/^NF-[0-9]+\/[0-9]+$/ {
printf("0 %s NF-%s\n",
substr($0,index($0,"/")+1),
substr($0,4,index($0,"/")-4));
}
/^[0-9]+\/[0-9]+$/ {
pos = index($0, "/");
a = substr($0, 1, pos-1)+0;
b = substr($0, pos+1)+0;
if (a > b) {
swp = a;
a = b;
b = swp;
}
for (i=a; i<=b; i++) {
print 1, i;
}
}
/^[0-9]+\/NF$/ {
print 1, substr($0, 1, length($0)-3), "NF";
}
/^NF\/[0-9]+$/ {
print 1, substr($0, index($0,"/")+1), "NF";
}
/^[0-9]+$/ {
print 1, $0;
}
/^NF-[0-9]+$/ {
print 0, $0;
}
(($0 == "NF") || ($0 == "NF/NF")) {
print 0, "NF";
}
' |
sort -k 1,1 -k 2n,2 -k 3n,3 |
uniq |
sed -n '1,/1 [0-9]\{1,\} NF$/p' |
awk '
BEGIN {
f1_total = 0;
f2_max = 0;
f3_has_nf = 0;
}
{
f1_total += $1;
if ($1 == 1) {
f2_max = ($2 > f2_max) ? $2 : f2_max;
f2_vals[$2] = 1;
}
f3_has_nf = ($3 == "NF") ? 1 : f3_has_nf;
cell[NR,1] = $2;
if (NF == 3) {
cell[NR,2] = $3;
}
}
END {
if ((f1_total == NR) && (f3_has_nf)) {
printf("split(\"\",mark);for(i=1;i<=NF;i++){mark[i]=1}");
for (i=1; i<f2_max; i++) {
if (! (i in f2_vals)) {
printf("delete mark[%d];", i);
}
}
} else {
printf("split(\"\",mark);");
for (i=1; i<=NR; i++) {
if (i SUBSEP 2 in cell) {
printf("if(%s>%s){for(i=%s;i<=%s;i++){mark[i]=1}}else{for(i=%s;i<=%s;i++){mark[i]=1}}",
cell[i,1],cell[i,2],
cell[i,2],cell[i,1],
cell[i,1],cell[i,2]);
} else {
if (match(cell[i,1],/^[0-9]+$/) || (cell[i,1] == "NF")) {
printf("mark[%s]=1;",cell[i,1]);
} else {
printf("if(%s>0){mark[%s]=1}",cell[i,1],cell[i,1]);
}
}
}
}
printf("convert_marked_flds();print;");
}
' )
if echo "$awkc0" | grep -q 'NF'; then
awkc0b=''
else
awkc0b=${awkc0%convert_marked_flds*}
awkc0='convert_marked_flds();print;'
fi
;;
*) awkc0='print utf8hira2kata($0);'
awkc0b=''
;;
esac
# === Generate the AWK code for hira2kata operation ==================
awkcode='
BEGIN {
for (i=0; i<hdr_skip; i++) {
if (getline line) {
print line;
} else {
exit;
}
}
utf8hira2kata_prep();
'"$awkc0b"'
}
{
'"$awkc0"'
}
function convert_marked_flds( fld) {
for (fld in mark) {
$fld = utf8hira2kata($fld);
}
}
function utf8hira2kata_prep( i,l) {
# register all character codes to shift kana code
for(i=1;i<=255;i++){chr[i]=sprintf("%c",i);asc[chr[i]]=i;}
# memorize other values
offset=14909824; # The previous code of "ぁ"
}
function utf8hira2kata(s_in, i,s,s1,s2,utf8c,delta,x,y,z,s_out) {
s_out = "";
for (i=1; i<=length(s_in); i++) {
s = substr(s_in,i,1);
#if (s < "\200") {
# s_out = s_out s;
# continue;
#}
if (s < "\300") {
s_out = s_out s;
} else if (s < "\340") {
i++;
s_out = s_out s substr(s_in,i,1);
} else if (s < "\360") {
s1 = substr(s_in,i+1,1);
s2 = substr(s_in,i+2,1);
utf8c = asc[s]*65536 + asc[s1]*256 + asc[s2];
if (utf8c<(offset+ 1)) { # lower range than "ぁ"
delta= 0;
} else if (utf8c<(offset+ 32)) { # between "ぁ" and "た"
delta= 288;
} else if (utf8c<(offset+ 64)) { # between "だ" and "み"
delta= 480;
} else if (utf8c<(offset+256)) { # out of range
delta= 0;
} else if (utf8c<(offset+279)) { # between "む" and "ゖ"
delta= 288;
} else if (utf8c<(offset+285)) { # out of range
delta= 0;
} else if (utf8c<(offset+287)) { # "ゝ" and "ゞ"
delta= 288;
} else {
delta= 0;
}
if (delta != 0) {
utf8c += delta;
z = utf8c % 256;
y = ((utf8c % 65536) - z)/256;
x = (utf8c - y*256 - z) /65536;
s_out = s_out chr[x] chr[y] chr[z];
} else {
s_out = s_out s s1 s2;
}
i += 2;
} else if (s < "\370") {
s_out = s_out s substr(s_in,i+1,3);
i += 3;
} else if (s < "\374") {
s_out = s_out s substr(s_in,i+1,4);
i += 4;
} else if (s < "\376") {
s_out = s_out s substr(s_in,i+1,5);
i += 5;
} else {
s_out = s_out s;
}
}
return s_out;
}
'
######################################################################
# Main Routine
######################################################################
case $directmode in
0) exec awk -v hdr_skip=$opth "$awkcode" ${file:+"$file"};;
*) printf '%s' "$directstr" |
awk -v hdr_skip=$opth "$awkcode" ;;
esac