-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.awk
More file actions
106 lines (94 loc) · 1.82 KB
/
parse.awk
File metadata and controls
106 lines (94 loc) · 1.82 KB
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
# Example usage:
# awk -f parse.awk JMdict 悪い 良い 国士無双 -- to find entries exactly
# matching input
# awk -f parse.awk -v inexact=1 JMdict 悪い 良い 国士無双 -- to find all
# mentions of provided words
function get_contents() {
split($0, a, ">")
return substr(a[2], 0, index(a[2], "<") - 1)
}
function is_empty(array) {
for (i in array)
return 0
return 1
}
function clear_arrays() {
gn_ru = 0
gn_en = 0
kn = 0
rn = 0
delete gloss_ru
delete gloss_en
delete keb
delete reb
}
BEGIN {
for (i = 2; i < ARGC; i++) {
if (inexact)
Words[ARGV[i]] = i - 1
else
Words[">" ARGV[i] "<"] = i - 1
delete ARGV[i]
}
ARGC = 2
}
/(<keb>|<reb>)/ {
for (w in Words) {
if ($0 ~ w) {
found = 1
Found[w] = ++fn
}
}
}
/<gloss.*lang="rus">/ {
gloss_ru[gn_ru++] = get_contents()
next
}
/<gloss>/ {
gloss_en[gn_en++] = get_contents()
next
}
/<reb>/ {
reb[rn++] = get_contents()
next
}
/<keb>/ {
keb[kn++] = get_contents()
next
}
found && /<\/entry>/ {
printf "%s;%s;\"", keb[0], reb[0]
rest = 0
if (lang == "ru" && ! is_empty(gloss_ru)) {
for (l in gloss_ru) {
if (rest) printf "\n"
printf "%s", gloss_ru[l]
rest = 1
}
} else {
for (l in gloss_en) {
if (rest) printf "\n"
printf "%s", gloss_en[l]
rest = 1
}
}
printf "\"\n"
found = 0
clear_arrays()
next
}
/<\/entry>/ {
clear_arrays()
}
END {
first = 1
for (w in Words) {
if (! (w in Found)) {
if (first) {
print "Can't find following words:"
first = 0
}
print " " w
}
}
}