#!/usr/local/bin/prologcgi

:- s_charset(_,sjis).
:- kanji_mode(_,off).
:- dlib_require(cabocha).

top_call:-
	get_params(A),
	html_call([
	"Content-Type: text/html; charset=shift-jis

	<html lang='ja'><head><meta http-equiv='Content-Type' content='text/html; charset=shift-jis'>
	<title>係り受け解析</title></head>
	<body><center>
	<H1> Hello Prolog World!!　係り受け解析Tool　cabocha＋AZ(準備中)</H1>
	<a href='puttxt.exe?cabochatest.cgi' target='source'>このページのソース表示</a><br>
	<INPUT TYPE='button' VALUE='RETURN' onClick='history.back()'><br><br>",
	"<font size=8>
	<h3><table border='1' cellpadding='3' cellspacing='0' width='80%'>
	<tr>
	<form action='cabochatest.cgi' method='POST'> 
		 <td>
		 <input type='text' size='100' name = 'cabocha' value='",A,"'>
		 <input type='submit' name='sub' value='係り受け解析'>
		</td>
		</form>
	</tr>
	<tr><td>",
		call(go(A)),
	"<br></td></tr></table></font></center></body></html>" ]).

%%%%%%%%%%%%%%%%
get_params(A):-get_param(cabocha,A),!.
get_params('なにか文章を入れて下さい').

%%%%%%%%%%%%%%%%
go(A):-
	(atom(A) ->name(A,STATEMENT);A=STATEMENT),!,
	cabocha_new(CABO, []), 
		write('<pre>'),nl,
			cabocha_sparse_tostr(CABO, STATEMENT, RESULT),
				puts(RESULT),
/*		write('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'),nl,
			cabocha_sparse_totree(CABO, STATEMENT, TREE),
			cabocha_tree_size(TREE, SIZE),
			cabocha_tree_get_chunk_list(TREE, CHUNK_LIST),
				write(size=SIZE),nl,nl,
				write_chunk_list(CHUNK_LIST),
*/		write('</pre>'),nl,
	cabocha_destroy(CABO).

go(_).

%%%%%%%%%%%%%
puts([]):- !,nl.
puts([C|T]):- puta([C]), puts(T).

puta("<"):-write('&lt;'),!.
puta(">"):-write('&gt;'),!.
puta([X]):- put(X),!.

%%%%%%%%%%%%%
write_chunk_list([]):- !.
write_chunk_list([[A,B,C,D,E|F]|T]):- 
	write([A,B,C,D,E]),nl,write(F),nl,nl,
	write_chunk_list(T).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  子プロセス方式のCabochaアクセス
/*
cabocha_new([In,Out,ID],Param):-  s_child(cabocha,Param,In,Out,ID).
cabocha_destroy([_,_,ID]) :- s_kill(ID,9).

cabocha_sparse_tostr([I,O|_],X,K):-
	launch_put(I,X), 
	launch_get_full(O,K).   % StopAtom is 'EOS' Delemita is 32(Space),Tab(9),Comma(44)

%% Put
launch_put(I,[A|L]) :-put(I,A),launch_put(I,L).
launch_put(I,[]) :- !,nl(I).

%% Get 
launch_get_full(IN,K):- get0(IN,M),name('EOS',[E,O,S]),END=[S,O,E|_],launch_get_full(IN,M,END,K,[M]).

launch_get_full(IN,S,End,[S],End):-!.
launch_get_full(IN,S,End,[S|R],D):- get0(IN,N),launch_get_full(IN,N,End,R,[N|D]).

*/
