1:-module(bot_fillin,
2 [
3 test_fillin/0,
4 test_fillin/1,
5 test_fillin/2,
6 test_fillin_parse1/0,
7 test_fillin_parse2/0,
8 foc_fillin_stream/2,
9 text_to_fillin_pos/2,
10 text_to_fillin_sents/2,
11 text_to_fillin_segs/2,
12 fillin_parse/2]). 15:- set_module(class(library)). 16:- set_module(base(system)). 17:- use_module(library(logicmoo_utils)). 18:- use_module(library(logicmoo_nlu/parser_penn_trees)). 19:- use_module(library(logicmoo_nlu/parser_tokenize)). 20
21:- dynamic(lmconfig:bot_py_dir/1). 22:- ignore(( \+ lmconfig:bot_py_dir(Dir), prolog_load_context(directory,Dir), assert(lmconfig:bot_py_dir(Dir)))). 23
24read_fillin_lines(In, Result):- fillin_to_dict(In, Result),!.
25
26fillin_completion(Text,LExpr):-
27 fillin_parse(Text, String),
28 nop(dmsg(fillin_parse=String)),
29 fillin_to_txt(String,LExpr),
30 nop(print_tree_nl(fillin=LExpr)),!.
31
32fillin_to_w2(Text,Result):- fillin_to_dict(Text,M),fillin_dict_to_txt(M, Result).
36
37fillin_to_dict(Text,Result):- is_dict(Text),!,Result=Text.
38fillin_to_dict(In, Result):- is_stream(In),!,fillin_stream_to_dict(In,_, Term),fillin_to_dict(Term, Result).
39fillin_to_dict(fillin(_In,Text),Result):- !, fillin_to_dict(Text,Result).
40fillin_to_dict(Compound,Result):- \+ atomic(Compound),!, any_to_json_dict(Compound,Result),!.
41fillin_to_dict(Text,Result):- notrace(on_x_fail(atom_json_dict(Text,Term,[]))),!,fillin_to_dict(Term,Result).
42fillin_to_dict(Text,Result):- notrace(on_x_fail(atom_json_term(Text,Term,[]))),!,fillin_to_dict(Term,Result).
43fillin_to_dict(Text,Result):- notrace(on_x_fail(atom_to_term(Text,Term,_))),!,fillin_to_dict(Term,Result).
44
45fillin_dict_to_txt(Dict,Result):- fillin_kv_name_value(Dict,choices,E),!,fillin_to_txt(E,Result).
46fillin_dict_to_txt(Dict,Result):- fillin_kv_name_value(Dict,text,Result).
47
48fillin_to_txt(In, Result):- is_stream(In),!,fillin_stream_to_dict(In,_, Term),!,fillin_to_txt(Term, Result).
49fillin_to_txt(E,V):- is_list(E),!,member(S,E),fillin_to_txt(S,V).
50fillin_to_txt(Text,Result):- is_dict(Text),!,fillin_dict_to_txt(Text,Result).
51fillin_to_txt(Text,Result):- compound(Text),!,fillin_kv_name_value(Text,text,Result).
52fillin_to_txt(Text,Text):-!.
53
54fillin_kv_name_value(E,_,_):- \+ compound(E),!,fail.
55fillin_kv_name_value(E,K,V):- is_list(E),!,member(S,E),fillin_kv_name_value(S,K,V).
56fillin_kv_name_value(E,K,V):- compound_name_arity(E,_,2),E=..[_,N,V],atomic(N),\+ number(N),name(K,N),!.
57fillin_kv_name_value(E,K,V):- is_dict(E),get_dict(K,E,V),!.
58fillin_kv_name_value(E,K,V):- arg(_,E,S),compound(S),fillin_kv_name_value(S,K,V).
59
60fillin_lexical_segs(I,O):-
61 old_into_lexical_segs(I,M),!,
62 fillin_parse_or_skip(I,S),!,
63 merge_fillin(S,M,O),!.
64
66fillin_parse_or_skip(_,[]).
67
68merge_fillin([],O,O):-!.
69merge_fillin([H|T],I,O):- !, merge_fillin(H,I,M), merge_fillin(T,M,O).
70merge_fillin(w(W,L),O,O):- member(w(W,OL),O), \+ member(fillin,OL),!,
71 ignore((member(spos(Pos),L), downcase_atom(Pos,DPos), set_pos(2,DPos,OL))),
72 nb_set_add(OL,[fillin|L]), !.
73merge_fillin(span(List),I,O):- member(span(_),List),!,
74 merge_fillin(List,I,O),!.
75merge_fillin(span(List),O,O):-
76 member(seg(S,E),List), member(span(Other),O), member(seg(S,E),Other),!,
77 nb_set_add(Other,[fillin|List]).
78merge_fillin(dep_tree(Type,R,Arg),O,O):-
79 member(w(_,Other),O),member(node(R),Other),
80 nb_set_add(Other,dep_tree(Type,R,Arg)).
81merge_fillin(_,I,I):-!.
82merge_fillin(S,I,O):- append(I,[S],O).
83
84fillin_stream_to_dict(In,_, Result):- peek_string(In,10,S),atom_contains(S,"fillin("),!,read_term(In,Term,[]),fillin_to_dict(Term, Result).
85fillin_stream_to_dict(In,S, Result):- atomic(S),atom_contains(S,"fillin("),!,read_term_from_atom_rest(In,S,Term),fillin_to_dict(Term, Result).
86fillin_stream_to_dict(In,S, Result):- atomic(S),at_end_of_stream(In),!,fillin_to_dict(S, Result).
87fillin_stream_to_dict(In,_, Result):- repeat, read_pending_codes(In,Codes,[]),
88 (Codes==[]->(sleep(0.1),fail);true),sformat(S,'~s',[Codes]),
89 fillin_stream_to_dict(In,S, Result).
90
91
92:- dynamic(tmp:existing_fillin_stream/4). 93:- volatile(tmp:existing_fillin_stream/4). 94foc_fillin_stream(Out,In):- thread_self(Self),tmp:existing_fillin_stream(Self,_,Out,In),!,clear_fillin_pending(In).
101foc_fillin_stream(Out,In):-
102 user:network_service_info(fillin,port,P4083),
103 thread_self(Self),
104 tcp_socket(Socket),
105 catch((tcp_connect(Socket, 'logicmoo.org':P4083),
106 tcp_open_socket(Socket, StreamPair)),_,fail),!,
107 StreamPair = In, StreamPair = Out,
108 set_stream(In,close_on_exec(false)),
109 set_stream(In,close_on_abort(false)),
110 set_stream(In,eof_action(eof_code)),
111 assert(tmp:existing_fillin_stream(Self,_,Out,In)),!.
112
113foc_fillin_stream(Out,In):- current_prolog_flag(python_local,true),
114 lmconfig:bot_py_dir(Dir),
115 thread_self(Self),
116 sformat(S,'python bot_fillin.py -nc -cmdloop ',[]),
117 nop(writeln(S)),
118 process_create(path(bash), ['-c', S], [ cwd(Dir), stdin(pipe(Out)),stdout(pipe(In)), stderr(null), process(FFid)]),!,
119 set_stream(In,close_on_exec(false)),
120 set_stream(Out,close_on_exec(false)),
121 set_stream(In,close_on_abort(false)),
122 set_stream(Out,close_on_abort(false)),
123 set_stream(In,eof_action(eof_code)),
124 set_stream(Out,eof_action(eof_code)),
125 sleep(1.0),
126 read_until_fillin_notice(In,"cmdloop_Ready."),!,
127 assert(tmp:existing_fillin_stream(Self,FFid,Out,In)).
128
129read_until_fillin_notice(In,Txt):- repeat,read_line_to_string(In,Str),(Str==end_of_file;atom_contains(Str,Txt)),!.
130
131current_fillin_stream(In):- thread_self(Self),tmp:existing_fillin_stream(Self,_FFid,_Out,In).
132
133clear_fillin_pending:- current_fillin_stream(In), clear_fillin_pending0(In),!.
134clear_fillin_pending(In):- nop(clear_fillin_pending0(In)).
135
136clear_fillin_pending0(In):- at_end_of_stream(In),!,dmsg(clear_fillin_pending=at_end_of_stream).
137clear_fillin_pending0(In):- read_pending_codes(In,Codes,[]),dmsg(clear_fillin_pending=Codes).
138
139tokenize_fillin_string([Str|Text],AtomO):- atomic(Str), is_list(Text), !, text_to_fillin_string([Str|Text],AtomO).
140tokenize_fillin_string(Atom,AtomO):- atom(Atom),!,Atom=AtomO.
141tokenize_fillin_string(JSON,AtomO):- compound(JSON),!,any_to_json_dict(JSON,Dict),atom_json_dict(AtomO,Dict,[]).
142tokenize_fillin_string(Text,AtomO):- text_to_fillin_string(Text,AtomO).
143text_to_fillin_string(Text,StrO):- any_to_string(Text,Str), replace_in_string('\n',' ',Str,StrO).
148
149fillin_parse(Text, Lines) :-
150 tokenize_fillin_string(Text,Fillin),
151 fillin_parse2(Fillin, Lines).
152
153fillin_parse2(String, Lines) :-
154 once(fillin_parse3(String, Lines)
155 ;fillin_parse4(String, Lines)).
156
157try_fillin_stream(Out,Write):- once(catch((format(Out,'~w',[Write])),_,
158 (retract(tmp:existing_fillin_stream(_,_,Out,_)),fail))).
159
161fillin_parse3(_String, _Lines) :- fail,
162 foc_fillin_stream(Out,_In),
163 try_fillin_stream(Out,''),fail.
165fillin_parse3(String, Lines) :-
166 foc_fillin_stream(Out,In),
167 try_fillin_stream(Out,String),
168 try_fillin_stream(Out,'\n'),
169 flush_output(Out),
170 read_fillin_lines(In, Lines).
171
173fillin_parse4(String, Lines) :- current_prolog_flag(python_local,true),
174 lmconfig:bot_py_dir(Dir),
175 sformat(S,'python bot_fillin.py -nc ~q ',[String]),
176 nop(writeln(S)),
177 process_create(path(bash), ['-c', S], [ cwd(Dir), stdout(pipe(In))]),!,
178 read_until_fillin_notice(In,"cmdloop_Ready."),!,
179 read_fillin_lines(In, Lines).
180
181test_fillin_parse1 :-
182 String = "Can the can do the Can Can?",
183 fillin_parse3(String, Lines),
184 pprint_ecp_cmt(yellow,test_fillin_parse1=Lines).
185
186test_fillin_parse2 :-
187 Text = "Can the can do the Can Can?",
188 fillin_parse4(Text,Lines),
189 pprint_ecp_cmt(yellow,test_fillin_parse2=Lines).
190
191test_fillin_parse3 :-
192 Text = "Can the can do the Can Can?",
193 fillin_parse2(Text,Lines),
194 pprint_ecp_cmt(yellow,test_fillin_parse3=Lines).
195
196
197fillin_pos_info(Text,PosW2s,Info,LExpr):-
198 text_to_fillin_sents(Text,LExpr),
199 tree_to_lexical_segs(LExpr,SegsF),
200 segs_retain_w2(SegsF,Info,PosW2s),!.
201
202text_to_fillin_pos(Text,PosW2s):- fillin_parse(Text,PosW2s),!.
203text_to_fillin_pos(Text,PosW2s):- fillin_pos_info(Text,PosW2s0,_Info,_LExpr),guess_pretty(PosW2s0),!,PosW2s=PosW2s0.
204
205text_to_fillin_segs(Text,Segs):-
206 fillin_completion(Text,LExpr),
207 tree_to_lexical_segs(LExpr,Segs).
208
209text_to_fillin_sents(Text,Sent):-
210 text_to_fillin_segs(Text,Segs),!,
211 fillin_segs_to_sentences(Segs,Sent),!.
212
213fillin_segs_to_sentences(Segs,sentence(0,W2,Info)):-
214 segs_retain_w2(Segs,Info,W2).
215
216
217:- if( \+ getenv('keep_going','-k')). 218:- use_module(library(editline)). 219:- add_history((call(make),call(test_fillin1))). 220:- endif. 221
222baseKB:regression_test:- test_fillin(1,X),!,test_fillin(X).
223baseKB:sanity_test:- make, forall(test_fillin(1,X),test_fillin(X)).
224baseKB:feature_test:- test_fillin.
225
226test_fillin0:-
227 Txt = "PERSON1 asks : Hey , what 's going on XVAR. < p >. PERSON2 said : Not a whole lot . . < p >. PERSON2 said : I 'm looking forward to the weekend , though . . < p >. PERSON1 asks : Do you have any big plans XVAR. < p >. PERSON2 said : Yes . . < p >. PERSON2 said : I 'm going to Wrigley Field on Saturday . . < p >. PERSON1 asks : Aren 't the Cubs out of town XVAR. < p >. PERSON2 said : Yes , but there 's a big concert at Wrigley this weekend . . < p >. PERSON1 said : Oh nice . . < p >. PERSON1 asks : Who 's playing XVAR. < p >. PERSON2 said : Pearl Jam is headlining the Saturday night show . . < p >. PERSON1 said : Wow , Pearl Jam . . < p >. PERSON1 said : I remeber when I got their first CD , Ten , at the record store at Harlem and Irving Plaza . . < p >. PERSON2 said : Oh right . . < p >. PERSON2 said : I remember that record store . . < p >. PERSON1 said : It was called Rolling Stone , and they went out of business many years ago . . < p >. PERSON2 said : Oh that 's too bad . . < p >. PERSON2 said : I really loved taking the bus to Harlem and Irving and visiting that store . . < p >. PERSON1 said : Me too . . < p >. PERSON1 said : We did n't have the internet back then and had to discover new music the hard way . . < p >. PERSON2 said : Haha yes . . < p >. PERSON2 said : I remember discovering ' ' Nirvana before they got famous . . < p >. PERSON1 said : Those were the good old days . . < p >. PERSON2 said : Yes they were . . < p >. PERSON2 said : I need to dig up my old Sony disc player and pop in an old CD . . < p >. PERSON1 asks : Where did the time go XVAR. < p >. PERSON1 said : Pearl Jam is 25 years old already . . < p >. PERSON2 said : It seems like only yesterday that the grunge music movement took over . . < p >. PERSON1 said : Right . . < p >. PERSON1 said : I bet everyone at the concert will be in their forty 's . . < p >. PERSON2 said : No doubt . . < p >. PERSON2 said : Well , I hope you have a great time at the concert . . < p > .",
228 test_fillin(Txt),
229 ttyflush,writeln(('\n test_fillin0.')),!.
230
231test_fillin1:-
232 233 Txt = "The Norwegian dude lives happily in the first house.",
234 test_fillin(Txt),
235 ttyflush,writeln(('\n test_fillin1.')),!.
236test_fillin2:-
237 Txt = "Rydell used his straw to stir the foam and ice remaining at the bottom of his tall plastic cup, as though he were hoping to find a secret prize.",
238 239 test_fillin(Txt),
240 ttyflush,writeln(('\n test_fillin2.')),!.
241
242test_fillin:-
243 Txt = "Rydell was a big quiet Tennessean with a sad shy grin, cheap sunglasses, and a walkie-talkie screwed permanently into one ear.",
244 test_fillin(Txt),
245 ttyflush,writeln(('\n test_fillin.')),
246 fail.
247test_fillin:- forall(test_fillin(X),test_fillin(X)).
248
249test_1fillin(Text):-
250 format('~N?- ~p.~n',[test_fillin(Text)]),
251 fillin_completion(Text,W),
252 print_tree_nl(W),
253 !.
254test_1fillin(Text):- wdmsg(failed(test_1fillin(Text))).
255
256test_fillin(N):- number(N),!, forall(test_fillin(N,X),test_1fillin(X)).
257test_fillin(X):- test_fillin(_,X),nop(lex_info(X)).
258
259test_fillin(In,Out):- nonvar(In),\+ number(In),var(Out),!,fillin_completion(In,Out).
260test_fillin(_,X):- nonvar(X), !, once(test_1fillin(X)).
261
262test_fillin(1,".\nThe Norwegian lives in the first house.\n.").
263test_fillin(1,"").
264test_fillin(1,".").
265test_fillin(1,"\n").
266
267test_fillin(1,"Rydell used his straw to stir the foam and ice remaining at the bottom of his tall plastic cup, as though he were hoping to find a secret prize.").
268
269test_fillin(2,Each):- test_fillin(3,Atom),atomic_list_concat(List,'\n',Atom), member(Each,List).
270
271test_fillin(3,
272'There are 5 houses with five different owners.
273 These five owners drink a certain type of beverage, smoke a certain brand of cigar and keep a certain pet.
274 No owners have the same pet, smoke the same brand of cigar or drink the same beverage.
275 The man who smokes Blends has a neighbor who drinks water.
276 A red cat fastly jumped onto the table which is in the kitchen of the house.
277 After Slitscan, Laney heard about another job from Rydell, the night security man at the Chateau.
278 Rydell was a big quiet Tennessean with a sad shy grin, cheap sunglasses, and a walkie-talkie screwed permanently into one ear.
279 Concrete beams overhead had been hand-painted to vaguely resemble blond oak.
280 The chairs, like the rest of the furniture in the Chateau\'s lobby, were oversized to the extent that whoever sat in them seemed built to a smaller scale.
281 Rydell used his straw to stir the foam and ice remaining at the bottom of his tall plastic cup, as though he were hoping to find a secret prize.
282 A book called, "A little tribute to Gibson".
283 "You look like the cat that swallowed the canary, " he said, giving her a puzzled look.').
284
285
286test_fillin(4,".
287The Brit lives in the red house.
288The Swede keeps dogs as pets.
289The Dane drinks tea.
290The green house is on the immediate left of the white house.
291The green house's owner drinks coffee.
292The owner who smokes Pall Mall rears birds.
293The owner of the yellow house smokes Dunhill.
294The owner living in the center house drinks milk.
295The Norwegian lives in the first house.
296The owner who smokes Blends lives next to the one who keeps cats.
297The owner who keeps the horse lives next to the one who smokes Dunhills.
298The owner who smokes Bluemasters drinks beer.
299The German smokes Prince.
300The Norwegian lives next to the blue house.
301The owner who smokes Blends lives next to the one who drinks water.").
302
303:- add_history(test_fillin). 304:- fixup_exports. 305
306