File size: 9,386 Bytes
557a17a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
%% lang_db_generator(Target_language)
%% New repository, with dbs in each rep

%:- include('../culturaltranslationtool/ctt2.pl'). % leave these commented out
%:- include('../culturaltranslationtool/edit.pl'). % "
%:- include('../listprologinterpreter/la_strings'). % leave off

:- dynamic lang_db/1.

%% List_of_words may be list of words to translate and add or an original language code

% lang_db_generator([["a","a"],["b","b exp"]],["fr"]). - create the db
% lang_db_generator("fr",["de"]). - add de with same entries as fr
% lang_db_generator("fr",["ab"]). - add ab "
% Alternatively, lang_db_generator("fr",["de","ab"]). instead of last two lines

load_lang_db1(Entry3) :-
	
	% if l.o.w. is orig lang, read from it instead
	
	directory_files("../listprologinterpreter/languages/",F),
	delete_invisibles_etc(F,G),
	findall(Entry2,(member(Filex1,G),
	string_concat("../listprologinterpreter/languages/",Filex1,Filex),
		phrase_from_file_s(string(String00a), Filex),
		string_codes(String02b,String00a),
		atom_to_term(String02b,String02a,[]),
		string_concat(Target_language3,".txt",Filex),
		string_concat("../listprologinterpreter/languages/",Target_language2,Target_language3),

findall([En_word,En_word2,Target_language2,T_word],(member(Entry,String02a),((Entry=[En_word,En_word2,Target_language2,T_word],string(En_word),string(En_word2),string(T_word))->true;(concat_list(["Error: File: ",Filex,", Entry: ",Entry," not in format [\"<En_word>\",\"<En_word2>\",\"<Target_language2>\",\"<T_word>\"]."],_Notification1)%,writeln(Notification1)
,fail))),Entry2)
),Entry2a),
	maplist(append,[Entry2a],[Entry3]).
	
lang_db_generator(List_of_words,Target_language1) :-

	load_lang_db1(Entry3),

	%%(not(member([_,Target_language1,_],Entry3))->
	
		(string(List_of_words)->
	(findall([Word1,Word2],(member([Word1,Word2,List_of_words,_],Entry3)),List_of_words2),List_of_words3=[]);
	
	(findall([Word1,Word2,List_of_words,Item_a],(member([Word1,Word2,List_of_words,Item_a],Entry3)),List_of_words3), %%** test
		findall(_,(member([Word,Word2],List_of_words),(string(Word),string(Word2))->true;(concat_list(["Error: Word1: ",Word," or Word2: ",Word2," in list of words not in format [\"<Word1>\",\"<Word2>\"]."],_Notification1)%,writeln(Notification1)
		,fail)),_)),List_of_words=List_of_words2),

%% list of langs to put words into
	
	findall(_,(member(Target_language10,Target_language1),
	(string(Target_language10)->true;(concat_list(["Error: Target_language1: ",Target_language1," not in format \"<Target_language1>\"."],_Notification11)%,writeln(Notification1)
	,fail)),

findall([Input,Input1,Target_language10,Output3],(member([Input,Input1],List_of_words2),translate4(Input1,"en",Target_language10,Output3)),Output4),

	%% save file
	%% ** add to prev file
	%% ** keep metaphorical translations (x bt xx with simplified ctt (no bt-trans dict) and customised output)
	%% numbers after command names
	%%	- check words on google translate, ctt speeds bt
	
	append(List_of_words3,Output4,Output4a), %% ** test
	
	concat_list(["../listprologinterpreter/languages/",Target_language10,".txt"],File1),
	term_to_atom(Output4a,File_contents),open_s(File1,write,Stream),write(Stream,File_contents),close(Stream)),_).
	
delete_invisibles_etc(F,G) :-
	findall(J,(member(H,F),atom_string(H,J),not(J="."),not(J=".."),not(string_concat(".",_,J))),G).

% test with t=w+1

%translate1(Input,From_l,Target_language1,Output3) :-
%concat_list([Input," ",From_l," ",Target_language1],Output3).

translate1("v","en","fr","v").
translate1("true","en","fr","vrai").
translate1("v","en","de","v").
translate1("true","en","de","wahr").
translate1(A,_,_,A).

load_lang_db :-
%% error if lang not found
	load_lang_db1(Entry3),
	(not(lang(Lang))->(Lang="en");lang(Lang)),
	(Lang="en"->true;
	(member([_,_,Lang,_],Entry3)->true;
	(concat_list(["Error: Language: ",Lang," not in listprologinterpreter/languages folder."],_Notification1)%,writeln(Notification1)
	,fail))),
	
	retractall(lang_db(_)),
 	assertz(lang_db(Entry3)),!.

get_lang_word3(Input,From_lang,To_lang,Output) :-
	From_lang="en",
	To_lang="en",
	atom_string(Output,Input),!.
get_lang_word3(Input,From_lang,To_lang,Output) :-
	not(From_lang="en"),
	To_lang="en",
	
	%(From_lang="en2"->Epsilon="";Epsilon=" "),
	Epsilon="",
	
	%(string(Input)->true;(number(Input))->true;fail),
	%lang_db(Lang_db),
	(not(lang_db(Lang_db))->(load_lang_db,lang_db(Lang_db));lang_db(Lang_db)),

split_on_number(Input,Input1,Input10),((member([Output2,_Input101,From_lang,Input1],Lang_db),
	%notrace,
	(Input10="" -> Output3=Output2;
	concat_list([Output2,Epsilon,Input10],Output3)))->true;
	(translate4(Input1,From_lang,To_lang,Output2),
	(Input10="" -> Output3=Output2;
	concat_list([Output2,Epsilon,Input10],Output3)))
	),
	replace(Output3," ","_",Output4),
	atom_string(Output,Output4),!.
	
get_lang_word3(Input0,From_lang,To_lang,Output) :-
	From_lang="en",
	not(To_lang="en"),
	
	%(To_lang="en2"->Epsilon=" ";Epsilon=" "),
	Epsilon=" ",
	
	%lang_db(Lang_db),
	(not(lang_db(Lang_db))->(load_lang_db,lang_db(Lang_db));lang_db(Lang_db)),
	replace(Input0,"_"," ",Input),
	%(atom(Input)->true;(number(Input))->true;fail),
	split_on_number(Input,Input1,Input10),((member([Input1,_Input102,To_lang,Output2],Lang_db),
	%notrace,
	(Input10="" -> Output=Output2;
	concat_list([Output2,Epsilon,Input10],Output)))->true;
	(translate4(Input1,From_lang,To_lang,Output2),
	(Input10="" -> Output=Output2;
	concat_list([Output2,Epsilon,Input10],Output)))
	),
	%atom_string(Input,Output),!.
	!.
	
get_lang_word3(Input,From_lang,To_lang,Output) :-
%% error if word not found
	not(From_lang="en"),
	not(To_lang="en"),

	%lang(To_lang),
	%lang_db(Lang_db),
	(not(lang_db(Lang_db))->(load_lang_db,lang_db(Lang_db));lang_db(Lang_db)),
	%((From_lang="en",To_lang="en")->%Input=Output1,
	%atom_string(Output,Input);
	%((%(%((Input="member2"->trace;true),
	split_on_number(Input,Input1,Input10),((member([Input2,_Input101,From_lang,Input1],Lang_db),member([Input2,_Input102,To_lang,Output2],Lang_db),
	%notrace,
	(Input10="" -> Output=Output2;
	concat_list([Output2," ",Input10],Output)))->true;
	(translate4(Input1,From_lang,To_lang,Output2),
	(Input10="" -> Output=Output2;
	concat_list([Output2," ",Input10],Output)))
	), %-> true;
	
	%concat_list(["Error: Word: ",Input," not in Language: ",Lang," in lang_db."],_Notification1),%writeln(Notification1),
	%fail
	%)))),
	%Output=Output1,
	!.
	%atom_string(Output,Output1),!.

%split_on_number(Input,Input1,Input10) :-
%findall([Input1,Input10],split_on_number1(Input,Input1,Input10),Output1),
%reverse(Output1,[[Input1,Input10]|_]).

/**

[debug]  ?- split_on_number("en 2",A,B).
A = "en",
B = "2".

[debug]  ?- split_on_number("en22",A,B).
A = "en",
B = "22".

[debug]  ?- split_on_number("en a 22",A,B).
A = "en a",
B = "22".

[debug]  ?- split_on_number("en a",A,B).
A = "en a",
B = "".

**/

split_on_number(Input,Input1,Input10) :-
	string_concat(A,B,Input),
	string_concat(C,D,B),
	string_length(C,1),
	((C=" ",
	string_concat(E,F,D),
	string_concat(J,K,E),
	string_length(K,1),
	K=" ",

	string_concat(G,_H,F),
	%trace,
	string_length(G,1),
	(number_string(_,G)),%->true;not(G=" ")),
	%((
	%C=" ",
	%)->(
	concat_list([A,C,J],Input1),
	%Input1=A,
	Input10=F))%->true;
	%(number_string(_,C),
	%Input1=A,Input10=B))
	,!.%);
split_on_number(Input,Input1,Input10) :-
	string_concat(A,B,Input),
	string_concat(C,D,B),
	string_length(C,1),
	((C=" ",
	string_concat(_E,F,D),

	string_concat(G,_H,F),
	%trace,
	string_length(G,1),

	(number_string(_,G)),%->true;not(G=" ")),
	%((
	%C=" ",
	%)->(
	%concat_list([A,C,J],Input1),
	Input1=A,
	Input10=F)->true;
	(number_string(_,C),
	Input1=A,Input10=B))
	,!.%);

split_on_number(Input,Input1,Input10) :-
	Input1=Input,Input10="".
	
replace(A,Find,Replace,F) :- 	
	split_string(A,Find,Find,B),findall([C,Replace],(member(C,B)),D),maplist(append,[D],[E]),concat_list(E,F1),string_concat(F,G,F1),string_length(G,1),!.

% stringconcat-> in both algs above
%	concatenate strings v

% membre 2->member2
get_en_lang_word(Input,Output) :-
%% error if word not found
	lang(Lang),
	lang_db(Lang_db),
	(Lang="en"->%Input=Output1,
	atom_string(Input,Output);
	((%(%((Input="member2"->trace;true),
	%split_on_number(Input,Input1,Input10),
	member([Output1,_Input101,Lang,Input],Lang_db),
	%atom_string(
	Output=Output1)->true;
	%notrace,
	%(%Input10="" -> Output=Output2;
	%concat_list([Output2," ",Input10],Output))) -> true;
	(concat_list(["Error: Word: ",Input," not in Language: ",Lang," in lang_db."],_Notification1)%,writeln(Notification1)
	,fail))),
	%Output=Output1,
	!.
	
get_lang_word(I,O) :-
	(not(lang(OL))->(OL="en");lang(OL)),
	get_lang_word3(I,"en",OL,O),!.
	
reserved_word(Word) :-
	lang(Lang)	,
	lang_db(Lang_db), 
	(Lang="en"->(member([Word1,_,_,_],Lang_db),
	atom_string(Word,Word1));
	member([_,_,_,Word],Lang_db)).
	
/*
reserved_word2(Word) :-
	lang(Lang)	,
	lang_db(Lang_db), 
 split_on_number(Word,Output21,_),
	atom_string(Output21,Output2),
 member([Output2,_Input101,_Lang,_Input1],Lang_db),
 not(Output21="query_box"),
 !.
*/

% reserved word - membre2 -> member2 (in list)
% (translate to en, check list)

reserved_word2(Word) :-
 lang(Lang)	,
 %lang_db(Lang_db), 
 get_lang_word3(Word,Lang,"en",Word2),
 atom_string(Word2,Word3),
 reserved_words2(Reserved_words),
 (member(Word3,Reserved_words)->true;
 (fail%
 %writeln1([not_reserved_word,Word3]),fail
 )),!.

% not query_box