1/* *
    2 module
    3% Writer NPC_Interface  for supporting inforation based actions
    4%
    5%
    6% Douglas Miles
    7% Dec 13, 2035
    8%
    9%
   10*/
   11% :-swi_module(world_text,[]).
   12%      default_repl_obj_to_string/3,default_repl_writer/4,show_kb_via_pred/3,show_kb_preds/2,show_kb_preds/3,wasSuccess/2
   13% :- include(prologmud(mud_header)).
   14
   15% :-export(eng_fully_expand/2).
   16
   17%:- file_begin(prolog).
   18
   19when_command_show(Agent,ActionType):- 
   20  findall(Show,on_command_show(Agent,ActionType,Show),MORELOOK),
   21  (MORELOOK==[] -> true;
   22 (must(mudAtLoc(Agent,LOC)),show_kb_preds(Agent,LOC,MORELOOK))).
   23 
   24
   25% ===========================================
   26% generatePhrase_local(+Term,-English).
   27% Generate english version of a message
   28% ===========================================
   29:-export(generatePhrase_local/2).   30
   31:- multifile lmconf:term_to_message_string/2.   32:- dynamic lmconf:term_to_message_string/2.   33
   34lmconf:term_to_message_string(T,T):-var(T),!.
   35lmconf:term_to_message_string(T,T):-!.
   36lmconf:term_to_message_string(txtConcatFn(T),M):-on_x_debug(generatePhrase_local(T,M)),!.
   37lmconf:term_to_message_string(fmt(T),M):-on_x_debug(generatePhrase_local(T,M)),!.
   38lmconf:term_to_message_string(C,C):-compound(C),functor(C,F,_),is_leave_alone(F),!.
   39lmconf:term_to_message_string((T),M):-on_x_fail(generatePhrase_local(T,M)),!.
   40lmconf:term_to_message_string(T,T):-!.
   41
   42is_leave_alone(exact_message).
   43is_leave_alone(todo).
   44is_leave_alone((error)).
   45is_leave_alone(parserm).
   46% is_leave_alone(F):- is_db_prop(F,_,_),!,fail.
   47is_leave_alone(A):-on_x_fail((sub_atom(A,_,1,0,S),atom_number(S,_))),!.
   48
   49:- discontiguous(bakeKB:mudTermAnglify/2).   50
   51prologDynamic(mudTermAnglify/2).
   52prologBuiltin(term_anglify_args/6).
   53prologBuiltin(term_anglify_last/2).
   54:-export(term_anglify_args/6).   55% :-export(term_anglify_last/2).
   56
   57term_anglify_last(Head,English):-compound(Head),
   58   functor(Head,F,A),A>1,
   59   not(ends_with_icase(F,"Fn")),not(starts_with_icase(F,"SKF-")),
   60   atom_codes(F,[C|_]),code_type(C,lower),
   61   Head=..[F|ARGS],
   62   term_anglify_args(Head,F,A,ARGS,prologSingleValued,English).
   63
   64mudTermAnglify(A,B):-local_term_anglify(A,B).
   65
   66mudTermAnglify(Head,EnglishO):- compound(Head), 
   67   Head=..[F|ARGS],mpred_prop(F,_,Info),
   68   member(Info,[prologSingleValued,predArgMulti(_)]),   
   69   term_anglify_args(Head,F,1,ARGS,Info,English),eng_fully_expand(English,EnglishO),!.
   70
   71mudTermAnglify(verbFn(isa),[is,a]):-!.
   72mudTermAnglify(verbFn(F),[is|UL]):-not(string_lower(F,F)),unCamelCase(F,U),atomics_to_string(UL,"_",U).
   73mudTermAnglify(verbFn(F),[is,F]):-atom_concat(_,'ing',F).
   74mudTermAnglify(verbFn(F),[F,is]).
   75% term_anglify(ftCallable(Term),String):-term_to_atom(Term,Atom),any_to_string(Atom,String).
   76mudTermAnglify(determinerString(Obj,Text),[posNP(Obj),is,uses,ftString(Text),as,a,determiner]).
   77mudTermAnglify(nameString(Obj,Text),[posNP(Obj),is,refered,to,as,ftString(Text)]).
   78mudTermAnglify(mudTermAnglify(Term,Text),[ftCallable(Term),is,converted,to,english,using,ftCallable(Text)]).
   79
   80
   81
   82term_anglify_args(Head,F,A,ARGS,predArgMulti(Which),English):- !,replace_nth_arglist(ARGS,Which,NewVar,NEWARGS),!,
   83   NewHead=..[F|NEWARGS], findall(NewVar,req1(NewHead),ListNewVar),list_to_set_safe(ListNewVar,SetNewVar),NewVar=ftListFn(SetNewVar),
   84   term_anglify_args(Head,F,A,NewHead,prologSingleValued,English).
   85
   86
   87/*
   88
   89term_expansion((term_anglify_args(_Head,F,A,ARGS0,prologSingleValued,English):- add_arg_parts_of_speech(F,1,ARGS0,ARGS),verb_after_arg(F,A,After),
   90   insert_into(ARGS,After,verbFn(F),NEWARGS),
   91   eng_fully_expand(NEWARGS,English),X),O).
   92
   93*/
   94term_anglify_args(_Head,F,A,ARGS0,prologSingleValued,English):- add_arg_parts_of_speech(F,1,ARGS0,ARGS),verb_after_arg(F,A,After),
   95               insert_into(ARGS,After,verbFn(F),NEWARGS),
   96               eng_fully_expand(NEWARGS,English),!.
   97
   98unCamelCase(S,String):-any_to_string(S,Str),S\=Str,!,unCamelCase(Str,String),!.
   99unCamelCase("",""):-!.
  100unCamelCase(S,String):-sub_string(S,0,1,_,Char),sub_string(S,1,_,0,Rest),unCamelCase(Rest,RestString),string_lower(Char,NewChar),
  101(Char\=NewChar->atomics_to_string(['_',NewChar,RestString],String);atomics_to_string([Char,RestString],String)),!.
  102
  103term_anglify_np_last(Obj,T,String):- local_term_anglify_np_last(Obj,T,String).
  104
  105generatePhrase_local(Term,String):- on_x_debug(( eng_fully_expand(Term,EnglishM),!,
  106          % fmt('FR0=~q~n',[eng_fully_expand(Term,EnglishM)]),
  107          eng_fully_expand(EnglishM,EnglishG),fix_grammar(EnglishG,English) , join_for_string(English,String))),!.
  108
  109local_grammar_correction([are,is,"here"],[are,"here"]).
  110local_grammar_correction(["you",is],["you",are]).
  111local_grammar_correction(["you",Verb,is],[your,Verb,is]).
  112
  113local_grammar_correction([at,"right"],["right"]).
  114local_grammar_correction([tRoom,are],[tRoom,is]).
  115local_grammar_correction([in,tRegion,"here"],[is,"here"]).
  116local_grammar_correction([X,X],[X]):-member(X,[is,are]).
  117 
  118get_grammar_correction(C1,C2):-
  119   local_grammar_correction(W1,W2),to_word_list(W1,C1),to_word_list(W2,C2).
  120
  121
  122fix_grammar(String,WordsO):-to_word_list(String,Ws),fix_grammar_0(Ws,Words),(Words\=Ws->fix_grammar_0(Words,WordsO);Words=WordsO),!.
  123
  124fix_grammar_0([],[]).
  125fix_grammar_0(EnglishG,English):-
  126   get_grammar_correction(Before,After),
  127   append_ci(Before,Rest,EnglishG),
  128   append_ci(After,Rest,EnglishNew),
  129   fix_grammar_0(EnglishNew,English),!.
  130fix_grammar_0([Carry|EnglishG],[Carry|English]):-
  131   fix_grammar_0(EnglishG,English),!.
  132
  133join_for_string(English,EnglishS):-on_x_fail(( flatten([English],EnglishF),list_to_atomics_list(EnglishF,EnglishA),atomics_to_string(EnglishA," ",EnglishS))),!.
  134join_for_string(English,English).
  135
  136list_to_atomics_list(L,AL):-list_to_atomics_list0(L,AL),forall(member(E,AL),must(atomic(E))).
  137
  138list_to_atomics_list0(Var,A):-var(Var),!,any_to_string(Var,A),!.
  139list_to_atomics_list0([E|EnglishF],[A|EnglishA]):-
  140   any_to_string(E,A),
  141   list_to_atomics_list0(EnglishF,EnglishA),!.
  142list_to_atomics_list0([],[]):-!.
  143
  144
  145eng_fully_expand(I,O):-loop_check(transitive(eng_fully_expand_ilc,I,O),I=O).
  146eng_fully_expand_ilc(I,O):-copy_term(I,C),flatten([C],FC),eng_fully_expand_0(FC,O).
  147
  148eng_fully_expand_0(FC,O):-catch(eng_fully_expand_1(FC,O),E,(dtrace,dmsg(exact_message(error_m(E,eng_fully_expand_1(FC,O)))),fail)),!.
  149%eng_fully_expand_0(FC,O):-catch((trace,eng_fully_expand_1(FC,O)),_,fail).
  150
  151eng_fully_expand_1(A,B):-loop_check(eng_fully_expand_1_ilc(A,B),A=B).
  152
  153eng_fully_expand_1_ilc(Var,Var):-var(Var),!.
  154eng_fully_expand_1_ilc([],[]):-!.
  155% eng_fully_expand_1(StringIsError,_Out):-string(StringIsError),!,dtrace,fail.
  156eng_fully_expand_1_ilc([T|TT],FTAO):-local_term_anglify_first([T|TT],TA),flatten([TA],FTA),eng_fully_expand_1(FTA,FTAO),!.
  157eng_fully_expand_1_ilc([T|Term],Out):-!,
  158   eng_fully_expand_2(T,E),
  159   eng_fully_expand_1(Term,English),
  160   flatten_append(E,English,Out),!.
  161
  162eng_fully_expand_1_ilc(T,E):-loop_check(eng_fully_expand_2(T,E),(T=E)),!.
  163
  164eng_fully_expand_2(Var,Var):-var(Var),!.
  165eng_fully_expand_2([],[]):-!.
  166eng_fully_expand_2(Var,Var):-not(compound(Var)),!.
  167eng_fully_expand_2(T,FTAO):-local_term_anglify_first(T,TA),flatten([TA],FTA),eng_fully_expand_1(FTA,FTAO).
  168eng_fully_expand_2(StringIsOK,StringIsOK):-string(StringIsOK),!.
  169eng_fully_expand_2([T|Term],Out):-!,
  170   eng_fully_expand_2(T,E),
  171   eng_fully_expand_1(Term,English),
  172   flatten_append(E,English,Out),!.
  173eng_fully_expand_2(Pred,Pred):-!.
  174eng_fully_expand_2(Pred,Out):-
  175   safe_univ(Pred,[F|ARGS]),
  176   eng_fully_expand_1_l(ARGS,NEWARGS),
  177   safe_univ(Out,[F|NEWARGS]),!.
  178
  179eng_fully_expand_1_l([],[]):-!.
  180eng_fully_expand_1_l([T|Term],[E|English]):-!,
  181   eng_fully_expand_1(T,E),
  182   eng_fully_expand_1_l(Term,English).
  183
  184
  185best_nl_phrase(List,Sorted):-predsort(best_nl_phrase,List,Sorted).
  186
  187% longest_string(?Order, @Term1, @Term2)
  188best_nl_phrase(Order,TStr1,TStr2):-
  189   any_to_string(TStr1,Str1),string_length(Str1,L1),
  190   any_to_string(TStr2,Str2),string_length(Str2,L2),
  191   compare(Order,L1-Str1,L2-Str2).
  192
  193is_phrase_type(posNP).
  194
  195prologBuiltin(local_term_anglify/2).
  196prologBuiltin(local_term_anglify_first/2).
  197prologBuiltin(local_term_anglify_last/2).
  198% prologBuiltin(local_term_anglify_np/2).
  199prologBuiltin(enter_term_anglify/2).
  200prologBuiltin(term_anglify_np_last/2).
  201
  202% ========================================
  203% enter_term_anglify(MASK)
  204% ========================================
  205
  206enter_term_anglify(X,Y):-var(X),copy_term(X,Y),!.
  207enter_term_anglify(X,Y):-findall(X-Y-Body,clause( mudTermAnglify(X,Y),Body),List),member(X-Y-Body,List),call(Body),!.
  208enter_term_anglify(X,Y):-findall(X-Y-Body,clause( term_anglify_np(X,Y),Body),List),member(X-Y-Body,List),call(Body),!.
  209enter_term_anglify(X,Y):-findall(X-Y-Body,clause( term_anglify_last(X,Y),Body),List),member(X-Y-Body,List),call(Body),!.
  210enter_term_anglify(X,Y):-findall(X-Y-Body,clause( term_anglify_np_last(X,Y),Body),List),member(X-Y-Body,List),call(Body),!.
  211enter_term_anglify(X,X).
  212
  213
  214local_term_anglify_first(T,TA):-compound(T),loop_check(local_term_anglify(T,TA)),!.
  215% local_term_anglify_first(FmtObj,String):-compound(FmtObj),functor(FmtObj,Fmt,_),corece(FmtObj,Fmt,String),!.
  216local_term_anglify_first(T,TA):-must(enter_term_anglify(T,TA)),!.
  217
  218
  219
  220% :-export(local_term_anglify/2).
  221local_term_anglify(Var,O):- var(Var),!,O=[ftCallable(Var)].
  222% local_term_anglify([Var],O):- var(Var),!,O=[ftCallable([Var])].
  223
  224local_term_anglify(posNP(P),English):- local_term_anglify_np(P,English).
  225local_term_anglify(noun_phrase(P),English):- local_term_anglify_np(P,English).
  226
  227local_term_anglify(actNotice(Who,What),[posNP(Who),notices,What]).
  228local_term_anglify(fN(Region,tRegion),[(String)]):- call_u(nameString(Region,String)),!.
  229local_term_anglify(fN(Region,tRegion),[nameString1(String)]):- holds_t(nameString,Region,String),!.
  230
  231
  232local_term_anglify([P|L],English):-!, local_term_anglify(P,PE),local_term_anglify(L,LE),!,flatten_append(PE,LE,English),!.
  233
  234local_term_anglify(HOLDS,English):-HOLDS=..[H,Pred,A|MORE],atom(Pred),is_holds_true(H),HOLDS2=..[Pred,A|MORE],!,local_term_anglify(HOLDS2,English).
  235local_term_anglify(HOLDS,[A,verbFn(Pred)|MORE]):-HOLDS=..[H,Pred,A|MORE],is_holds_true(H),!.
  236local_term_anglify(HOLDS,English):-HOLDS=..[H,Pred,A|MORE],is_holds_false(H),atom(Pred),HOLDS2=..[Pred,A|MORE],!,local_term_anglify(not(HOLDS2),English).
  237local_term_anglify(HOLDS,[false,that,A,verbFn(Pred)|MORE]):-HOLDS=..[H,Pred,A|MORE],is_holds_false(H),!.
  238local_term_anglify(not(HOLDS),[false,that,English]):-!,local_term_anglify(HOLDS,English).
  239local_term_anglify(notFound(FNum,F,Type),[no,FNum,TypeC,'-s',for,FC]):-copy_term(notFound(F,Type),notFound(FC,TypeC)),ignore(TypeC=tCol),ignore(FC='whatever').
  240local_term_anglify(NPO,String):-NPO=..[NP,Obj],is_phrase_type(NP),!,enter_term_anglify(fN(Obj,NP),String).
  241
  242local_term_anglify(fN(Obj,argIsaFn(_PathName,_NumTwo)),String):- enter_term_anglify(Obj,String),!.
  243local_term_anglify(cmdresult(Cmd,Whatnot),["the","command","result","of",Cmd,"is",Whatnot]):-!.
  244local_term_anglify(string(Obj),[String]):-on_x_fail(any_to_string(Obj,StringUQ)),atomics_to_string(['"',StringUQ,'"'],"",String).
  245% enter_term_anglify(ftCallable(Obj),string(String)):-on_x_fail(any_to_string(Obj,StringUQ)),atomics_to_string(['(',StringUQ,')'],"",String).
  246local_term_anglify(mudAtLoc(Obj,LOC),String):-eng_fully_expand( [fN(Obj,posNP),is,at,fN(LOC,posNP)],String).
  247local_term_anglify(mudDescription(Obj,Term),[fN(Obj,posNP),"description","contains",":",string(Term)]).
  248local_term_anglify(fN(Obj,X),String):- locationToRegion(Obj,Region), Obj \= Region, enter_term_anglify(fN(Region,X),String),!.
  249% should not have searched nouns yet
  250local_term_anglify(fN(Obj,T),String):- local_term_anglify_np(Obj,T,String),!.
  251
  252local_term_anglify(done(Obj,Term),[fN(Obj,posNP),did,:,Term]).
  253local_term_anglify(failed(Obj,Term),[fN(Obj,posNP),didnt,:,Term]).
  254local_term_anglify(do(Obj,Term),[fN(Obj,posNP),begun,:,Term]).
  255
  256
  257% almost all else failed
  258local_term_anglify(fN(Obj,T),String):- anglify_noun_known(Obj,T,String),!.
  259
  260% totally all else failed
  261% %enter_term_anglify(ftCallable(Obj),String):- any_to_string(Obj,StringUQ),!,atomics_to_string(['',StringUQ,''],"",String),!.
  262% %enter_term_anglify(Obj,Obj):-!.
  263
  264
  265term_anglify_np(Obj,Hint,String):-local_term_anglify_np(Obj,Hint,String).
  266
  267local_term_anglify_np(Obj,String):-isa(Obj,Isa),local_term_anglify_np(Obj,Isa,String),!.
  268local_term_anglify_np(Obj,String):-local_term_anglify_np(Obj,ftTerm,String).
  269
  270% specific noun searching
  271local_term_anglify_np(Obj,Hint,String):- anglify_noun_known(Obj,Hint,String),!.
  272local_term_anglify_np(Obj,vtDirection,Obj):- !.
  273local_term_anglify_np(string(Obj),string,Obj):- !.
  274local_term_anglify_np(Obj,string,Obj):- !.
  275
  276local_term_anglify_np_last(Obj,Hint,String):- anglify_noun_known(Obj,Hint,String),!.
  277local_term_anglify_np_last(Obj,FT,String):- ttExpressionType(FT),correctFormatType(change(assert,_),Obj,FT,String),!.
  278local_term_anglify_np_last(Obj,Type,[prolog(Obj)]):-ttExpressionType(Type),!.
  279local_term_anglify_np_last(Obj,Type,["the",Type,ftCallable(Obj)]):-!.
  280local_term_anglify_np_last(apathFn(Region,Dir),_,["a",fN(Dir,vtDirection),"-ern","way","from",fN(Region,posNP)]):-!.
  281local_term_anglify_np_last(Obj,Type,[prolog(Obj),fN,Type]):-!.
  282local_term_anglify_np_last(Obj,_,["the",noun,with,token,Obj]):-!.
  283
  284:-ain_expanded(==>prologBuiltin(anglify_noun_known,3)).
  285
  286% anglify_noun_known(Self,_Hint,["you"]):- current_agent(Self),!.
  287anglify_noun_known(Obj,FT,String):- ttExpressionType(FT),correctFormatType(change(assert,_),Obj,FT,String),!.
  288anglify_noun_known(StringO,_Hint, [StringO]).
  289anglify_noun_known(Obj,_Hint,["right","here"]):- current_agent(Self),mudAtLoc(Self,Obj),!.
  290anglify_noun_known(Obj,_Hint,["here"]):- current_agent(Self),req1(localityOfObject(Self,Obj)),!.
  291anglify_noun_known(Obj,_Hint,StringO):- findall(String,holds_t(nameString,Obj,String),List),List\=[],sort_by_strlen(List,[StringO|_]),!.
  292%anglify_noun_known(Obj,_Hint,String):-
  293%nameString(X,Y,_,_)
  294
  295%:- file_begin(prolog).
  296
  297
  298
  299detWithSpace(WithSpace,String):-ddeterminer0(String),atom_concat(String,' ',WithSpace).
  300detWithSpace(WithSpace,String):-ddeterminer1(String),atom_concat(String,' ',WithSpace).
  301
  302:-export(determinerRemoved/3).  303determinerRemoved(S0,Det,S):- fail,detWithSpace(WithSpace,String),string_concat(WithSpace,S,S0),string_lower(String,Det).
  304
  305:-export(query_description/1).  306query_description(mudDescription(I,S)):-  is_asserted(mudDescription(I,S)).
  307query_description(t(mudDescription,I,S)):- is_asserted(mudDescription(I,S));is_asserted(mudKeyword(I,S)).
  308
  309
  310:-export(remove_description/1).  311remove_description(mudDescription(I,S)):- dmsg(trace_or_throw(remove_description(mudDescription(I,S)))).
  312
  313:-export(add_description/1).  314add_description(mudDescription(I,S)):-add_description(I,S).
  315
  316:-export(add_description/2).  317add_description(A,S0):-ainz(mudDescription(A,S0)),fail.
  318add_description(A,S0):- atomic(S0),string_concat('actPunchingSomething ',S,S0),!,add_description(A,S).
  319% add_description(A,S0):-determinerRemoved(S0,String,S),!,add_description(A,S),ainz(determinerString(A,String)).
  320add_description(A,S0):-
  321   any_to_string(S0,S),
  322   atomic_list_concat(Words,' ',S),
  323   atomic_list_concat(Sents,'.',S),!,
  324   length(Words,Ws),
  325   must(add_description(A,S,S0,Ws,Sents,Words)),!.
  326
  327% mudBareHandDamage: 10d10+75
  328add_description(A,S,_S0,Ws,_Sents,_Words):- Ws<3,  
  329   atomic_list_concat([K,V],': ',S),!,add_description_kv(A,K,V).
  330
  331add_description(A,S,_S0,Ws,_Sents,_Words):- Ws<3,
  332   atomic_list_concat([K,V],'=',S),!,add_description_kv(A,K,V).
  333
  334% "NOBACKSTAB","ACT_STAY_ZONE","MEMORY"
  335add_description(A,_S,_S0,1,_,[Word]):-add_description_word(A,Word),!.
  336
  337%actPunchingSomething ..
  338add_description(A,S,S0,Ws,Sents,['actPunchingSomething',B|C]):-add_description(A,S,S0,Ws,Sents,[B|C]).
  339add_description(A,S,S0,Ws,Sents,[Det,B|C]):-ddeterminer(Det,L),add_description(A,S,S0,Ws,Sents,[B|C]),ainz(determinerString(A,L)).
  340add_description(A,S,S0,Ws,_Sents,_Words):-Ws>3,is_here_String(S),text_to_string(S0,String),!,ainz(descriptionHere(A,String)).
  341add_description(A,_S,S0,_Ws,_Sents,_Words):- any_to_string(S0,String),ainz(mudDescription(A,String)).
  342
  343is_here_String(S):- atomic_list_concat_safe([_,is,_,"here",_],S).
  344is_here_String(S):- atomic_list_concat_safe([_,"here"],S).
  345is_here_String(S):- atomic_list_concat_safe([_,is,"here",_],S).
  346
  347
  348ddeterminer1("A").
  349ddeterminer1("An").
  350ddeterminer1("The").
  351
  352ddeterminer0("a").
  353ddeterminer0("an").
  354ddeterminer0("the").
  355
  356ddeterminer(L,L):-ddeterminer0(L).
  357ddeterminer(U,L):-string_lower(U,L),U\=L,!,ddeterminer0(L).
  358
  359add_description_word(A,Word):- string_upper(Word,Word),string_lower(Word,Flag),string_to_atom(Flag,Atom),atom_concat(flagged_,Atom,FAtom),ainz((isa(A,FAtom))).
  360add_description_word(A,Word):- string_lower(Word,Word),ainz((mudKeyword(A,Word))).
  361add_description_word(A,Word):- string_lower(Word,Lower),ainz((mudKeyword(A,Lower))).
  362
  363
  364add_description_kv(A,K,V):- atom_concat('actPunchingSomething ',Key,K),!,add_description_kv(A,Key,V).
  365add_description_kv(A,K,V):- atom_concat('+',Key,K),!,add_description_kv(A,Key,V).
  366add_description_kv(A,K,V):-atom_to_value(V,Term),C=..[K,A,Term],show_load_call(ainz(C))