/***************************************
	ܸ켭񻲾ȥץ

	ICOT ʸˡ

			by TOKUNAGA, T.
			90/6/1
***************************************/

/***********************************
 **index ȤʤǤؤѹ

		ICOT SANO,H
		August 2, 1992

dictionary/5
'**in'dex 򥳥ȥ
assertz('**wf_dict'(Cat,Arg,Xx,Yx,[Cat|Tree]))
  ==>  assertz('**wf_dict'(Cat,Arg,Xs,Ys,[Cat|Tree]))

fetch_dict/7
'**in'dex 򥳥ȥ
goal(Goal,Info,T,Xs,Ys)
  ==  > goal(Goal,Info,T,Xx,Yx)
***********************************/

% dictionary
dictionary(Cat,Arg,T,X,Y) :-
    '**wf_dict'(_,_,X,_,_),!,
    '**wf_dict'(Cat,Arg,X,Y,T).
dictionary(_,_,_,Xs,Ys) :-
%    '**index'(Xx,Xs),
    '*dict'(trie,Cat,Arg,T,Xs,Ys),
%    '**index'(Yx,Ys),
    '*makeword'(T,Tree),
    assertz('**wf_dict'(Cat,Arg,Xs,Ys,[Cat|Tree])),
    fail.
dictionary(Cat,Arg,T,X,Y) :-
    '**wf_dict'(Cat,Arg,X,Y,T).

% dict
'*dict'(TRIE,Cat,Info,T0,Xs,Zs) :-
   '*fetch_dict'(TRIE,ARGs,Tries,T0,T1,Xs,Ys),
  ( '*one_of'(Tries,Trie),
    '*dict'(Trie,Cat,Info,T1,Ys,Zs)
    ;
    '*one_of'(ARGs,[Cat,Info,_]),
    Ys = Zs,
    T1 = []
  ).

% fetch_dict
'*fetch_dict'(trie,ARGs,TRIEs,[W|Ts],Ts,[W|Ws],Ws) :- !,
    trie(W,ARGs,TRIEs).
'*fetch_dict'([W,ARGs,TRIEs],ARGs,TRIEs,[W|Ts],Ts,[W|Ws],Ws) :- !.
'*fetch_dict'([[Goal|Info],ARGs,TRIEs],ARGs,TRIEs,[T|Ts],Ts,Xs,Ys) :-
%    '**index'(Xx,Xs),
    goal(Goal,Info,T,Xs,Ys).
%    '**index'(Yx,Ys).
'*fetch_dict'([program(PRGs),ARGs,TRIEs],ARGs,TRIEs,Ts,Ts,Ws,Ws) :- !,
    '*calls'(PRGs).

% calls
'*calls'([]) :- !.
'*calls'([X|R]) :-
    call(X),
    '*calls'(R).

% makeword
'*makeword'([],[]) :- !.
'*makeword'(Xs,[Word|Zs]) :-
    '*extract_word'(Xs,Ws,Ys),
  ( Ws == [] ->
	Xs = [Word|_]			% ʸˡƥιʸڤξ
	;
	'*listToAtom'(Ws,Word) ),	% ñξ
    '*makeword'(Ys,Zs).

% extract_word
'*extract_word'([],[],[]):- !.
'*extract_word'([[_|_]|Ys],[],Ys) :- !.
'*extract_word'([X|Xs],[X|Ys],Zs) :- !,
    '*extract_word'(Xs,Ys,Zs).
