-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRANS.PAS
268 lines (245 loc) · 5.36 KB
/
TRANS.PAS
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
{ Find a Japanese word, and convert to an English word }
function FindWord(var cur : integer; var cat : integer; var index : integer; var word : Str16) : boolean;
var i, j, s, l, fs, fl : integer;
begin
cat := -1;
index := -1;
word := '';
fs := 0;
fl := 0;
s := cur;
while s < Length(Line) do
begin
{ Numbers or alphabets (as noun) }
if Line[s] < Chr($80) then
begin
cur := s;
cat := 1;
while (Chr($0) < Line[cur]) and (Line[cur] < Chr($80)) do
begin
word := word + Line[cur];
cur := cur + 1;
end;
FindWord := True;
exit;
end;
{ Verb }
for i := 0 to VMax do
begin
for j := 1 to 5 do
begin
l := Length(d[i, j]);
if l > fl then
begin
if Copy(Line, s, l) = d[i, j] then
begin
cat := 0;
index := i;
word := v[i, 1];
fs := s;
fl := l;
end;
end;
end;
end;
{ Noun }
for i := 0 to NMax do
begin
for j := 1 to 5 do
begin
l := Length(m[i, j]);
if l > fl then
begin
if Copy(Line, s, l) = m[i, j] then
begin
cat := 1;
index := i;
word := n[i, 1];
fs := s;
fl := l;
end;
end;
end;
end;
{ Japanese 'NI' (TO, IN, etc.) }
l := Length('に');
if (l > fl) and (Copy(Line, s, l) = 'に') then
begin
cat := 2;
word := '';
fs := s;
fl := l;
end;
{ Janapese 'DE' (WITH) }
l := Length('で');
if (l > fl) and (Copy(Line, s, l) = 'で') then
begin
cat := 2;
word := 'with';
fs := s;
fl := l;
end;
if fs > 0 then
begin
cur := fs + fl;
FindWord := True;
exit;
end;
if Line[s] < Chr($80) then
s := s + 1
else
s := s + 2;
end;
FindWord := False;
exit;
end;
{ Parse a Janapese sentence, and convert to English words }
procedure ParseJ(var verb : Str16; var noun1 : Str16; var prepos : Str16; var noun2 : Str16);
var cur, cat, index, v, n1, n2 : integer;
var tmp : Str16;
var word : Str16;
begin
verb := '';
noun1 := '';
prepos := '';
noun2 := '';
cur := 1;
{ 1st word }
if not FindWord(cur, cat, index, word) then
exit
else if cat = 0 then { verb }
begin
verb := word; v := index;
exit;
end
else if cat = 1 then { noun }
begin
noun1 := word; n1 := index;
end;
{ 2nd word }
if not FindWord(cur, cat, index, word) then
exit
else if cat = 0 then { verb }
begin
verb := word; v := index;
exit;
end
else if cat = 1 then { noun }
begin
noun2 := word; n2 := index;
end
else if cat = 2 then { prep }
begin
prepos := word;
noun2 := noun1; n2 := n1;
noun1 := ''; n1 := 0;
end;
{ 3rd word }
if not FindWord(cur, cat, index, word) then
exit;
if cat = 0 then { verb }
begin
if (noun2 <> '') and (prepos = '') then
begin
prepos := prep[index];
end;
verb := word; v := index;
exit;
end
else if cat = 1 then { noun }
begin
noun1 := word; n1 := index;
end
else if cat = 2 then { prep }
begin
prepos := word;
end;
{ 4th word }
if not FindWord(cur, cat, index, word) then
exit
else if cat = 0 then { verb }
begin
if prepos = '' then
prepos := prep[index];
verb := word; v := index;
if (v = 37{TIE}) and (n2 = 57{ROPE}) then { If 'ROPE NI X WO MUSUBU' }
begin
{ Convert to 'ROPE WO X NI MUSUBU' }
tmp := noun1;
noun1 := noun2;
noun2 := tmp;
end
end;
end;
{ Parse 'X TO IU'(SAY X) }
procedure ParseSayJ(var message : Str16);
var i : integer;
begin
message := '';
i := Pos(d[31, 1]{ TO IU }, Line);
if i <= 0 then
exit;
message := Copy(Line, 1, i - 1);
if (message = 'ケラヒヨチミ') or (message = 'ケラヒ ヨチミ') or (message = 'けらひよちみ') or (message = 'けらひ よちみ') then
message := 'nepo egassap'
else if message = 'こんにちは' then
message := 'hello'
else if message = 'さようなら' then
message := 'bye';
end;
{ Convert full width numbers/alphabets to half width }
const hchrs : Str80 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
procedure FullToHalf;
var i, c : integer;
begin
i := 1;
while i < Length(Line) do
begin
if Line[i] < Chr($80) then
i := i + 1
else
begin
c := Pos(Copy(Line, i, 2), '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') - 1;
if (c < 0) or (c mod 2 > 0) then
i := i + 2
else
begin
Delete(Line, i, 2);
Insert(hchrs[Trunc(c / 2) + 1], Line, i);
i := i + 1;
end;
end;
end;
end;
{ Translate a Japanese sentence to English }
procedure Translate;
var message, verb, noun1, prepos, noun2 : Str16;
begin
if ('A' <= Line[1]) and (Line[1] <= 'z') then
exit;
ParseSayJ(message);
if message <> '' then
begin
Line := 'say ' + message;
exit;
end;
FullToHalf;
ParseJ(verb, noun1, prepos, noun2);
if verb = '' then
begin
if noun1 <> '' then
Line := noun1;
exit;
end;
if noun2 <> '' then
begin
Line := verb + ' ' + noun1 + ' ' + prepos + ' ' + noun2;
exit;
end;
if noun1 <> '' then
begin
Line := verb + ' ' + noun1;
exit;
end;
Line := verb;
end;