%:- auto_table.

:- export
	build/2.

:- import transform_file/2 from transform.
:- import append/3 from lists.

build([], []).

build([H|T], [Head|Tail]) :-
	build(H, Head), !,
	build(T, Tail).

build(FileName, Graph) :-
	atom(FileName),
%	write('there'), nl,
	transform_file(FileName, Document), !,
%	write('here'), nl,
	build(Document, Graph), !.

build(files(FileList), GraphSet) :-
	transform_file(files(FileList), Corpus),
%	write('here'), nl,
	build(Corpus, GraphSet), !.

build(corpus(DocumentList), graphSet(GraphList)) :-
	build(DocumentList, GraphList).

build(document(FeatureMap, Content, AnnotationSets), graph(EdgeList)) :-
%	write(Content), nl,
	build(Content, TextEdgeList),
%	write('now'), nl,
	build(AnnotationSets, AnnotationEdgeLists),
	merge([TextEdgeList|AnnotationEdgeLists], EdgeList).

build(content([node(A)]), []).

build(content([node(A),B,node(C)|Nodes]), [txt(A,C,B)|TextEdges]) :-
%	write(node(A)), nl,
	build(content([node(C)|Nodes]), TextEdges).

build(annotationset(Annotations), AnnotationEdgeList) :-
	build(Annotations, AnnotationEdgeList).

build(annotation(Id, Type, Start, End, FeatureMap), ann(Start, End, Type)).

merge([], []).

merge([H|T], L) :-
	merge(T, M),
	append(H, M, L), !.

