2:- module(xdrs2html,[drs2html/2,
    3                     xfdrs2html/2,
    4                     xdrs2html/2]).    5
    6:- use_module(semlib(options),[option/2]).    7:- use_module(semlib(errors),[warning/2]).    8
    9
   10/*========================================================================
   11   Converting DRSs to XML
   12========================================================================*/
   13
   14drs2html(DRS,Stream):-
   15   drs2html(DRS,Stream,3).
   16
   17xdrs2html(XDRS,Stream):-
   18   XDRS=xdrs(_Tags,DRS),
   19   drs2html(DRS,Stream).
   20
   21xfdrs2html(XDRS,Stream):-
   22   XDRS=xdrs(Tags,Cons),
   23   write(Stream,' <tags>'), nl(Stream),
   24   tags2html(Tags,Stream),
   25   write(Stream,' </tags>'), nl(Stream),
   26   write(Stream,' <cons>'), nl(Stream),
   27   cons2html(Cons,Stream),
   28   write(Stream,' </cons>'), nl(Stream).
   29
   30
   31/*========================================================================
   32   Converting DRSs to XML (with tab insertion)
   33========================================================================*/
   34
   35drs2html(drs(D,C),Stream,Tab):-
   36   tab(Stream,Tab), format(Stream,'<table>~n',[]),
   37   tab(Stream,Tab), format(Stream,'<tr id="dom"><td>~n',[]),
   38   NewTab is Tab + 2,
   39   dom2html(D,Stream,NewTab),
   40   tab(Stream,Tab), format(Stream,'</td></tr>~n',[]),
   41   tab(Stream,Tab), format(Stream,'<tr id="con"><td>~n',[]),
   42   conds2html(C,Stream,NewTab),
   43   tab(Stream,Tab), format(Stream,'</td></tr>~n',[]),
   44   tab(Stream,Tab), format(Stream,'</table>~n',[]).
   45
   46drs2html(alfa(_Type,B1,B2),Stream,Tab):- !,
   47   NewTab is Tab + 1,
   48   tab(Stream,Tab), format(Stream,'<table><tr><td>(</td><td>',[]),
   49   drs2html(B1,Stream,NewTab),
   50   tab(Stream,Tab), format(Stream,'</td><td>&alpha;</td><td>',[]),
   51   drs2html(B2,Stream,NewTab),
   52   tab(Stream,Tab), format(Stream,'</td><td>)</td></tr></table>~n',[]).
   53
   54drs2html(merge(B1,B2),Stream,Tab):- !,
   55   NewTab is Tab + 1,
   56   tab(Stream,Tab), format(Stream,'<table><tr><td>(</td><td>',[]),
   57   drs2html(B1,Stream,NewTab),
   58   tab(Stream,Tab), format(Stream,'</td><td>;</td><td>',[]),
   59   drs2html(B2,Stream,NewTab),
   60   tab(Stream,Tab), format(Stream,'</td><td>)</td></tr></table>~n',[]).
   61
   62
   63/*========================================================================
   64   Converting DRS-domains to XML (with tab insertion)
   65========================================================================*/
   66
   67dom2html([],_,_).
   68
   69dom2html([_Index:X|L],Stream,Tab):- !,
   70   tab(Stream,Tab),   
   71%   format(Stream,'<dr name="~p">~n',[X]),
   72   format(Stream,'~p~n',[X]),
   73%   index2html(Index,Stream,Tab),
   74%   tab(Stream,Tab),
   75%   format(Stream,'</dr>~n',[]),
   76   dom2html(L,Stream,Tab).
   77
   78dom2html([X|L],Stream,Tab):-
   79   option('--warnings',true), !,
   80   warning('cannot print DR ~p',[X]),
   81   dom2html(L,Stream,Tab).
   82
   83dom2html([_|L],Stream,Tab):-
   84   dom2html(L,Stream,Tab).
   85
   86
   87/*========================================================================
   88   Converting DRS-conditions to XML (with tab insertion)
   89========================================================================*/
   90
   91conds2html([],_,_).
   92
   93conds2html([_Index:not(B)|L],Stream,Tab):- !,
   94   tab(Stream,Tab), format(Stream,'<table><tr><td>&not;</td><td>~n',[]),
   95%   index2html(Index,Stream,Tab),
   96   NewTab is Tab + 1,
   97   drs2html(B,Stream,NewTab),
   98   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
   99   conds2html(L,Stream,Tab).
  100
  101conds2html([_Index:nec(B)|L],Stream,Tab):- !,
  102   tab(Stream,Tab), format(Stream,'<table><tr><td>[]</td><td>~n',[]),
  103%   index2html(Index,Stream,Tab),
  104   NewTab is Tab + 1,
  105   drs2html(B,Stream,NewTab),
  106   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
  107   conds2html(L,Stream,Tab).
  108
  109conds2html([_Index:pos(B)|L],Stream,Tab):- !,
  110   tab(Stream,Tab), format(Stream,'<table><tr><td>&loz;</td><td>~n',[]),
  111%   index2html(Index,Stream,Tab),
  112   NewTab is Tab + 1,
  113   drs2html(B,Stream,NewTab),
  114   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
  115   conds2html(L,Stream,Tab).
  116
  117conds2html([_Index:prop(X,B)|L],Stream,Tab):- !,
  118   tab(Stream,Tab), format(Stream,'<table><tr><td>~p:</td><td>~n',[X]),
  119%   index2html(Index,Stream,Tab),
  120   NewTab is Tab + 1,
  121   drs2html(B,Stream,NewTab),
  122   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
  123   conds2html(L,Stream,Tab).
  124
  125conds2html([_Index:or(B1,B2)|L],Stream,Tab):- !,
  126   NewTab is Tab + 1,
  127   tab(Stream,Tab), format(Stream,'<table><tr><td>~n',[]),
  128%  index2html(Index,Stream,Tab),
  129   drs2html(B1,Stream,NewTab),
  130   tab(Stream,Tab), format(Stream,'</td><td>V</td><td>~n',[]),
  131   drs2html(B2,Stream,NewTab),
  132   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
  133   conds2html(L,Stream,Tab).
  134
  135conds2html([_Index:imp(B1,B2)|L],Stream,Tab):- !,
  136   NewTab is Tab + 1,
  137   tab(Stream,Tab), format(Stream,'<table><tr><td>~n',[]),
  138%  index2html(Index,Stream,Tab),
  139   drs2html(B1,Stream,NewTab),
  140   tab(Stream,Tab), format(Stream,'</td><td>&rArr;</td><td>~n',[]),
  141   drs2html(B2,Stream,NewTab),
  142   tab(Stream,Tab), format(Stream,'</td></tr></table>~n',[]),
  143   conds2html(L,Stream,Tab).
  144
  145conds2html([Index:duplex(_,B1,_,B2)|L],Stream,Tab):- !,
  146   conds2html([Index:imp(B1,B2)|L],Stream,Tab).
  147
  148conds2html([_Index:pred(Arg,X,_Type,_Sense)|L],Stream,Tab):- !,
  149   tab(Stream,Tab),   
  150   symbol(X,Y),
  151   format(Stream,'~w(~p)<br>~n',[Y,Arg]),
  152%  index2html(Index,Stream,Tab),
  153   conds2html(L,Stream,Tab).
  154
  155conds2html([_Index:rel(Arg1,Arg2,X,_Sense)|L],Stream,Tab):- !,
  156   tab(Stream,Tab),   
  157   symbol(X,Y),
  158   format(Stream,'~w(~p,~p)<br>~n',[Y,Arg1,Arg2]),
  159%   index2html(Index,Stream,Tab),
  160   conds2html(L,Stream,Tab).
  161
  162conds2html([_Index:role(Arg1,Arg2,X,1)|L],Stream,Tab):- !,
  163   tab(Stream,Tab),   
  164   symbol(X,Y),
  165   format(Stream,'~w(~p,~p)<br>~n',[Y,Arg1,Arg2]),
  166%   index2html(Index,Stream,Tab),
  167   conds2html(L,Stream,Tab).
  168
  169conds2html([_Index:role(Arg1,Arg2,X,-1)|L],Stream,Tab):- !,
  170   tab(Stream,Tab),   
  171   symbol(X,Y),
  172   format(Stream,'~w(~p,~p)<br>~n',[Y,Arg2,Arg1]),
  173%   index2html(Index,Stream,Tab),
  174   conds2html(L,Stream,Tab).
  175
  176conds2html([_Index:named(Arg,X,Type,_)|L],Stream,Tab):- !,
  177   tab(Stream,Tab),   
  178   symbol(X,Y),
  179   format(Stream,'named(~w,~p,~p)<br>~n',[Y,Arg,Type]),
  180%  index2html(Index,Stream,Tab),
  181   conds2html(L,Stream,Tab).
  182
  183conds2html([_Index:card(X,Y,_Type)|L],Stream,Tab):- !,
  184   tab(Stream,Tab),   
  185   format(Stream,'|~p| = ~p<br>~n',[X,Y]),
  186%   index2html(Index,Stream,Tab),
  187   conds2html(L,Stream,Tab).
  188
  189conds2html([_Index:timex(X,Y)|L],Stream,Tab):- !,
  190   tab(Stream,Tab),   
  191%   index2html(Index,Stream,Tab),
  192   format(Stream,'timex(~p,',[X]),
  193   timex2html(Y,Stream),
  194   format(Stream,')<br>~n',[]),
  195   conds2html(L,Stream,Tab).
  196
  197conds2html([_Index:eq(X,Y)|L],Stream,Tab):- !,
  198   tab(Stream,Tab),   
  199   format(Stream,'~p=~p<br>~n',[X,Y]),
  200%   index2html(Index,Stream,Tab),
  201   conds2html(L,Stream,Tab).
  202
  203conds2html([X|L],Stream,Tab):-
  204   option('--warnings',true), !,
  205   warning('cannot print DR-Condition ~p',[X]),
  206   conds2html(L,Stream,Tab).
  207
  208conds2html([_|L],Stream,Tab):-
  209   conds2html(L,Stream,Tab).
  210
  211
  212/*========================================================================
  213   Timex
  214========================================================================*/
  215
  216timex2html(date(_:A,_:B,_:C),Stream):- !,
  217   format(Stream,'~w~w~w',[A,B,C]).
  218
  219timex2html(date(_:Z,_:A,_:B,_:C),Stream):- !,
  220   format(Stream,'~w~w~w~w',[Z,A,B,C]).
  221
  222timex2html(time(_:A,_:B,_:C),Stream):- !,
  223   format(Stream,'~w~w~w',[A,B,C]).
  224
  225timex2html(X,Stream):- !,
  226   format(Stream,'timex',[X]).
  227
  228
  229/*========================================================================
  230   Words
  231========================================================================*/
  232
  233words2html([],_).
  234
  235words2html([word(Index,Word)|L],Stream):-
  236   symbol(Word,Word1),
  237   format(Stream,'  <word xml:id="i~p">~w</word>~n',[Index,Word1]),
  238   words2html(L,Stream).
  239
  240
  241/*========================================================================
  242   POS tags
  243========================================================================*/
  244
  245tags2html([],_).
  246
  247tags2html([pos(Index,POS)|L],Stream):-
  248   format(Stream,'  <postag index="i~p">~w</postag>~n',[Index,POS]),
  249   tags2html(L,Stream).
  250
  251tags2html([ne(Index,NE)|L],Stream):-
  252   format(Stream,'  <netag index="i~p">~w</netag>~n',[Index,NE]),
  253   tags2html(L,Stream).
  254
  255
  256/*========================================================================
  257   Flat DRSs
  258========================================================================*/
  259
  260cons2html([],_).
  261
  262cons2html([Label:alfa(Type,L1,L2)|Cons],Stream):- !,
  263   format(Stream,'  <alfa label="~p" type="~p"><label>~p</label><label>~p</label></alfa>~n',[Label,Type,L1,L2]),
  264   cons2html(Cons,Stream).
  265
  266cons2html([Label:merge(L1,L2)|Cons],Stream):- !,
  267   format(Stream,'  <merge label="~p"><label>~p</label><label>~p</label></merge>~n',[Label,L1,L2]),
  268   cons2html(Cons,Stream).
  269
  270cons2html([Label:drs(D,Labels)|Cons],Stream):- !,
  271   format(Stream,'  <drs label="~p">~n',[Label]),
  272   dom2html(D,Stream,3),
  273   labels2html(Labels,Stream),
  274   format(Stream,'  </drs>~n',[]),
  275   cons2html(Cons,Stream).
  276
  277cons2html([Label:Index:named(Arg,X,Type,_)|Cons],Stream):- !,
  278   symbol(X,Y),
  279   format(Stream,'  <named label="~p" arg="~p" symbol="~w" type="~p">~n',[Label,Arg,Y,Type]),
  280   index2html(Index,Stream,2),
  281   format(Stream,'  </named>~n',[]),
  282   cons2html(Cons,Stream).
  283
  284cons2html([Label:Index:pred(Arg,X,Type,Sense)|Cons],Stream):- !,
  285   symbol(X,Y),
  286   format(Stream,'  <pred label="~p" arg="~p" symbol="~w" type="~p" sense="~p">~n',[Label,Arg,Y,Type,Sense]),
  287   index2html(Index,Stream,2),
  288   format(Stream,'  </pred>~n',[]), 
  289   cons2html(Cons,Stream).
  290
  291cons2html([Label:Index:rel(Arg1,Arg2,X,Sense)|Cons],Stream):- !,
  292   symbol(X,Y),
  293   format(Stream,'  <rel label="~p" arg1="~p" arg2="~p" symbol="~w" sense="~p">~n',[Label,Arg1,Arg2,Y,Sense]),
  294   index2html(Index,Stream,2),
  295   format(Stream,'  </rel>~n',[]), 
  296   cons2html(Cons,Stream).
  297
  298cons2html([Label:Index:role(Arg1,Arg2,X,1)|Cons],Stream):- !,
  299   symbol(X,Y),
  300   format(Stream,'  <rel label="~p" arg1="~p" arg2="~p" symbol="~w" sense="~p">~n',[Label,Arg1,Arg2,Y,1]),
  301   index2html(Index,Stream,2),
  302   format(Stream,'  </rel>~n',[]), 
  303   cons2html(Cons,Stream).
  304
  305cons2html([Label:Index:role(Arg2,Arg2,1,-1)|Cons],Stream):- !,
  306   symbol(X,Y),
  307   format(Stream,'  <rel label="~p" arg1="~p" arg2="~p" symbol="~w" sense="~p">~n',[Label,Arg1,Arg2,Y,1]),
  308   index2html(Index,Stream,2),
  309   format(Stream,'  </rel>~n',[]), 
  310   cons2html(Cons,Stream).
  311
  312cons2html([Label:Index:card(X,Y,Type)|Cons],Stream):- !,
  313   format(Stream,'  <card label="~p" arg="~p" value="~p" type="~p">~n',[Label,X,Y,Type]),
  314   index2html(Index,Stream,2),
  315   format(Stream,'  </card>~n',[]), 
  316   cons2html(Cons,Stream).
  317
  318cons2html([Label:Index:timex(X,Y)|Cons],Stream):- !,
  319   format(Stream,'  <timex label="~p" arg="~p">~n',[Label,X]),
  320   timex2html(Y,Stream),
  321   index2html(Index,Stream,2),
  322   format(Stream,'  </timex>~n',[]),
  323   cons2html(Cons,Stream).
  324
  325cons2html([Label:Index:eq(X,Y)|Cons],Stream):- !,
  326   format(Stream,'  <eq label="~p" arg1="~p" arg2="~p">~n',[Label,X,Y]),
  327   index2html(Index,Stream,2),
  328   format(Stream,'  </eq>~n',[]),
  329   cons2html(Cons,Stream).
  330
  331cons2html([Label:Index:not(L)|Cons],Stream):- !,
  332   format(Stream,'  <not label="~p">~n',[Label]),
  333   format(Stream,'  <label>~p</label>~n',[L]),
  334   index2html(Index,Stream,2),
  335   format(Stream,'  </not>~n',[]),
  336   cons2html(Cons,Stream).
  337
  338cons2html([Label:Index:nec(L)|Cons],Stream):- !,
  339   format(Stream,'  <nec label="~p">~n',[Label]),
  340   format(Stream,'  <label>~p</label>~n',[L]),
  341   index2html(Index,Stream,2),
  342   format(Stream,'  </nec>~n',[]),
  343   cons2html(Cons,Stream).
  344
  345cons2html([Label:Index:pos(L)|Cons],Stream):- !,
  346   format(Stream,'  <pos label="~p">~n',[Label]),
  347   format(Stream,'  <label>~p</label>~n',[L]),
  348   index2html(Index,Stream,2),
  349   format(Stream,'  </pos>~n',[]),
  350   cons2html(Cons,Stream).
  351
  352cons2html([Label:Index:prop(X,L)|Cons],Stream):- !,
  353   format(Stream,'  <prop label="~p" argument="~p">~n',[Label,X]),
  354   format(Stream,'  <label>~p</label>~n',[L]),
  355   index2html(Index,Stream,2),
  356   format(Stream,'  </prop>~n',[]),
  357   cons2html(Cons,Stream).
  358
  359cons2html([Label:Index:or(L1,L2)|Cons],Stream):- !,
  360   format(Stream,'  <or label="~p">~n',[Label]),
  361   format(Stream,'  <label>~p</label>~n  <label>~p</label>~n',[L1,L2]),
  362   index2html(Index,Stream,2),
  363   format(Stream,'  </or>~n',[]),
  364   cons2html(Cons,Stream).
  365
  366cons2html([Label:Index:imp(L1,L2)|Cons],Stream):- !,
  367   format(Stream,'  <imp label="~p">~n',[Label]),
  368   format(Stream,'  <label>~p</label>~n  <label>~p</label>~n',[L1,L2]),
  369   index2html(Index,Stream,2),
  370   format(Stream,'  </imp>~n',[]),
  371   cons2html(Cons,Stream).
  372
  373cons2html([Label:Index:duplex(_,L1,_,L2)|Cons],Stream):- !,
  374   cons2html([Label:Index:imp(L1,L2)|Cons],Stream).
  375
  376
  377/*========================================================================
  378   Labels
  379========================================================================*/
  380
  381labels2html([],_).
  382
  383labels2html([Label|L],Stream):-
  384   format(Stream,'   <label>~w</label>~n',[Label]),
  385   labels2html(L,Stream).
  386
  387
  388/*========================================================================
  389   Indexes
  390========================================================================*/
  391
  392index2html([],_,_):- !.
  393
  394index2html([X|L],Stream,Tab):-
  395   number(X), !,
  396   Pos is mod(X,1000),
  397   tab(Stream,Tab), 
  398   format(Stream,'<index pos="~p">i~p</index>~n',[Pos,X]),
  399   index2html(L,Stream,Tab).
  400
  401index2html([_|L],Stream,Tab):-
  402   index2html(L,Stream,Tab).
  403
  404
  405/*========================================================================
  406   Deal with special symbols
  407========================================================================*/
  408
  409symbol(S1,S2):-
  410   name(S1,C1),
  411   check(C1,C2),
  412   name(S2,C2).
  413
  414check([],[]).
  415
  416%%% Special character &
  417%%%
  418check([38|L1],[38,97,109,112,59|L2]):- !,
  419   check(L1,L2).
  420
  421%%% Special character <
  422%%%
  423check([60|L1],[38,108,116,59|L2]):- !,
  424   check(L1,L2).
  425
  426%%% Special character >
  427%%%
  428check([62|L1],[38,103,116,59|L2]):- !,
  429   check(L1,L2).
  430
  431%%% Special character '
  432%%%
  433check([62|L1],[38,97,112,111,115,59|L2]):- !,
  434   check(L1,L2).
  435
  436%%% Special character "
  437%%%
  438check([34|L1],[38,113,117,111,116,59|L2]):- !,
  439   check(L1,L2).
  440
  441check([X|L1],[X|L2]):-
  442   check(L1,L2)