oldforest2edges.pl
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%
% wariant birnam_dumpedges konwertujący z forest() na edge()
% trudnostka: w pewnym momencie zmieniliśmy sposób zapisywania terminali, tutaj stary.
%
% edge(Id, NT, Od, Do, [NReg/[EId1, Przez1, EId2, …], …], NumTrees)
%
%
% Copyright (C) 1997–2009 Marcin Woliński
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA
%
% In addition, as a special exception, the copyright holder gives
% permission to link the code of this program with the Morfeusz library
% (see http://www.nlp.ipipan.waw.pl/~wolinski/morfeusz), and distribute
% linked combinations including the two. You must obey the GNU General
% Public License in all respects for all of the code used other than
% Morfeusz. If you modify this file, you may extend this exception to
% your version of the file, but you are not obligated to do so. If you
% do not wish to do so, delete this exception statement from your
% version.
dumpterm(T) :-
% write(':-'),
numbervars(T,0,_),
% writeq(T),
write_term(T,[portray(true),quoted(true)]),
% print(T),
write('.'),
nl.
% Pozyskiwanie drzew analizy:
process_parses(NT,Od,Do) :-
write(':-style_check(-singleton).'), nl,
statprint(nonterminal,NT),
statprint(startnode,Od),
statprint(endnode,Do),
flag(useful_edge_number,_,0),
flag(number_of_trees,_,0),
getforest(NT,Od,Do, _Children,TrId),
counttrees(NT,Od,Do,TrId,NumTrees,_EId),
flag(number_of_trees,N,N+NumTrees),
fail.
process_parses(_NT,_Od,_Do) :-
flag(number_of_trees,NumTrees,NumTrees),
statprint(trees,NumTrees),
flag(useful_edge_number,N,N),
statprint(useful_edges,N).
statprint(X,Y) :-
dumpterm(info(X,Y)).
koniec :-
dumpterm(sukces).
porazka :-
dumpterm(porazka).
counttrees(_NT,_Od,_Do,TrId,_NumTrees,_EId) :-
var(TrId), !, throw('Variable ID in a tree?!!').
counttrees(NT,_Od,_Do,TrId,NumTrees,EId) :-
nonvar(TrId), recorded(TrId,t(NT1,NumTrees,EId)), NT=@=NT1, !.
counttrees(terminal(T),Od,Do,TrId,1,EId) :-
!,
flag(useful_edge_number,EId,EId+1),
recordz(TrId,t(terminal(T),1,EId)),
dumpterm(edge(EId,Od,Do,terminal(T),[],1)).
counttrees(NT,Od,Do,TrId,NumTrees,EId) :-
flag(useful_edge_number,EId,EId+1),
getforest(NT,Od,Do, Children,TrId),
sumchildren(Children, NewChildren, Od, Do, NumTrees),
recordz(TrId,t(NT,NumTrees,EId)),
dumpterm(edge(EId,Od,Do,NT,NewChildren,NumTrees)).
%sumchildren([_NReg/[]], [], _Od, _Do, 1) :- !. % to był terminal
sumchildren([NReg/Children | CC], [NReg/NewChildren|NCC], Od, Do, NumTrees) :-
countchildren(Children,NewChildren,Od,Do,ChildTrees),
sumchildren(CC, NCC, Od, Do, OtherTrees),
NumTrees is ChildTrees + OtherTrees.
sumchildren([], [], _Od, _Do, 0).
countchildren([],[],_,_,1) :-!.
countchildren([TrId/NT],[EId],Od,Do,NumT) :- !,
counttrees(NT,Od,Do,TrId,NumT,EId).
countchildren([TrId/NT,Przez | CC], [EId,Przez | NCC], Od, Do, NumTrees) :- !,
counttrees(NT,Od,Przez,TrId,NumT,EId),
countchildren(CC, NCC, Przez, Do, NumTT),
NumTrees is NumT * NumTT.
countchildren(C,_,Od,Do,0) :-
format(user_error,"~NInvalid children (~p-~p): ~p~n",[Od,Do,C]), fail.
run(SentId) :-
info(tekst, Tekst),
info(nonterminal, NT),
info(startnode, Od),
info(endnode, Do),
% info(trees, Trees),
% info(useful_edges, Edges),
info(parse_inferences, Infer),
info(parse_cputime, CPU),
statprint(sent_id, SentId),
statprint(tekst, Tekst),
% statprint(trees, Trees),
% statprint(useful_edges, Edges),
statprint(parse_inferences, Infer),
statprint(parse_cputime, CPU),
process_parses(NT,Od,Do),
write('sukces.'), nl.
%%% Local Variables:
%%% coding: utf-8
%%% mode: prolog
%%% End: