-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdagprinter.cpp
261 lines (221 loc) · 7.53 KB
/
dagprinter.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* PET
* Platform for Experimentation with efficient HPSG processing Techniques
* (C) 1999 - 2002 Ulrich Callmeier [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** Generic printer for arced dags */
#include "dagprinter.h"
#include "utility.h"
#include <iomanip>
#include <algorithm>
#include <list>
using namespace std;
// ============================================================================
// AbstractDagPrinter implementation
// ============================================================================
inline int AbstractDagPrinter::visited(const dag_node *dag) {
return _dags_visited[dag];
}
inline int AbstractDagPrinter::set_visited(const dag_node *dag, int val) {
return _dags_visited[dag] = val;
}
inline void
AbstractDagPrinter::mark_arc_targets(const dag_arc *arcs, bool temp) {
while(arcs != 0) {
mark_coreferences(arcs->val, temp);
arcs = arcs->next;
}
}
void AbstractDagPrinter::mark_coreferences(const dag_node *dag, bool temp) {
if (temp) dag = dag_deref(dag);
int vis_nr = visited(dag);
if(vis_nr == 0) {
set_visited(dag, 1);
// not yet visited
mark_arc_targets(dag->arcs, temp);
if(temp) mark_arc_targets(dag_get_comp_arcs(dag), temp);
} else {
if (vis_nr == 1) { // met for the second time: assign a nr > 1
set_visited(dag, 2);
}
}
}
void
AbstractDagPrinter::init_coreference_marks(const dag_node *dag, bool temp) {
_dags_visited.clear();
_coref_nr = 0;
mark_coreferences(dag, temp);
}
// ============================================================================
// ReadableDagPrinter implementation
// ============================================================================
void ReadableDagPrinter::
print_arcs(ostream &out, const dag_arc *arc, bool temp, int indent) {
if(arc == 0) return;
int maxlen = 0, i, maxatt = 0;
const dag_node *print_attrs[nattrs];
for(i = 0; i < nattrs; ++i)
print_attrs[i] = 0;
while(arc) {
i = attrnamelen[arc->attr];
maxlen = maxlen > i ? maxlen : i;
print_attrs[arc->attr]=arc->val;
maxatt = arc->attr > maxatt ? arc->attr : maxatt;
arc=arc->next;
}
++maxlen; // space between label and argument
indent += 2; // width of "[ "
out << endl << setw(indent) << "[ ";
bool nonfirst = false;
int newindent = indent + maxlen;
for(int j = 0; j <= maxatt; ++j) {
if (print_attrs[j]) {
if (nonfirst)
out << "," << endl << setw(indent) << "";
else
nonfirst = true;
out << attrname[j] << setw(maxlen - attrnamelen[j]) << "";
print_dag_rec(out, print_attrs[j], temp, newindent);
}
}
out << " ]";
}
/** this is incomplete, but fast and should suffice in almost all cases */
static int width(int nr) {
if (nr < 10) return 1;
else if (nr < 100) return 2;
else if (nr < 1000) return 3;
else if (nr < 10000) return 4;
else if (nr < 100000) return 5;
return 6;
}
void ReadableDagPrinter::
print_dag_rec(ostream &out, const dag_node *dag, bool temp, int indent) {
// das sollte in print_dag_node_start von traditional rein
const dag_node *orig = dag;
if(temp) {
orig = dag_deref(dag);
if(orig != dag) out << "~";
}
int coref = get_coref_nr(dag);
if(coref != 0) {
if (coref < 0) { // dag is coreferenced, already printed
out << "#" << - coref ;
return;
} else { // dag is coreferenced, not printed yet
indent += 2 + width(coref);
out << '#' << coref << ':' ;
}
}
out << type_name(dag->type);
if (_print_pointer) {
out << "(" << hex;
if(dag != orig) out << (size_t) orig << "->";
out << (size_t) dag << ")" << dec ;
}
print_arcs(out, dag->arcs, temp, indent);
if(temp) print_arcs(out, dag_get_comp_arcs(dag), temp, indent);
}
/** default printing for chart items: use a ReadableDagPrinter */
std::ostream &operator<<(std::ostream &out, const dag_node *dag) {
ReadableDagPrinter rdp;
rdp.print(out, dag);
return out;
}
// ============================================================================
// CompactDagPrinter implementation
// ============================================================================
/** All non-human readable formats: LUI, Fegramed, JXCHG */
void CompactDagPrinter::
print_dag_rec(ostream &out, const dag_node *dag, bool temp) {
// recursively print dag. requires `visit' field to be set up by
// mark_coreferences. negative value in `visit' field means that node
// is coreferenced, and already printed
int coref = get_coref_nr(dag);
if (coref != 0) {
if(coref < 0) { // dag is coreferenced, already printed
print_coref_reference(out, - coref) ;
return;
} else { // dag is coreferenced, not printed yet
print_coref_definition(out, coref);
}
}
print_dag_node_start(out, dag);
for(dag_arc *arc = dag->arcs; arc != NULL; arc = arc->next) {
print_arc(out, arc, temp);
}
print_dag_node_end(out, dag);
}
void
ItsdbDagPrinter::print_arc(ostream &out, const dag_arc *arc, bool temp) {
out << " " << attrname[arc->attr] << " ";
print_dag_rec(out, arc->val, temp);
} // ItsdbDagPrinter::print_arc()
void
ItsdbDagPrinter::print_coref_reference(ostream &out, int coref_nr) {
out << "#" << coref_nr;
} // ItsdbDagPrinter::print_coref_reference()
void
ItsdbDagPrinter::print_coref_definition(ostream &out, int coref_nr) {
out << "#" << coref_nr << "=";
} // ItsdbDagPrinter::print_coref_definition()
void
ItsdbDagPrinter::print_dag_node_start(ostream &out, const dag_node *dag) {
const char *type = type_name(dag->type);
int n = strlen(type);
if(n >= 2 && type[0] == '"' && type[n - 1] == '"') {
out << "\"" << escape_string(string(type).substr(1, n - 2)) << "\"";
} // if
else
out << type;
if (dag->arcs != NULL) out << " [";
} // ItsdbDagPrinter::print_dag_node_start()
void
ItsdbDagPrinter::print_dag_node_end(ostream &out, const dag_node *dag) {
if (dag->arcs != NULL) out << " ]";
} // ItsdbDagPrinter::print_dag_node_end()
void
ItsdbDagPrinter::print_dag_rec(std::ostream &out, const struct dag_node *dag, bool temp) {
int coref = get_coref_nr(dag);
if (coref != 0) {
if(coref < 0) { // dag is coreferenced, already printed
print_coref_reference(out, - coref) ;
return;
} else { // dag is coreferenced, not printed yet
print_coref_definition(out, coref);
}
}
print_dag_node_start(out, dag);
print_arcs(out, dag->arcs, temp);
print_dag_node_end(out, dag);
}
bool compare_arcs(const dag_arc *arc1, const dag_arc *arc2) {
return strcmp(attrname[arc1->attr], attrname[arc2->attr]) < 0;
}
void
ItsdbDagPrinter::print_arcs(std::ostream &out, const dag_arc *arc, bool temp) {
if (arc == 0) return;
list<const dag_arc*> arclist;
while (arc) {
arclist.push_back(arc);
arc = arc->next;
}
arclist.sort(compare_arcs);
for (list<const dag_arc*>::iterator iter = arclist.begin();
iter != arclist.end(); iter ++) {
print_arc(out, *iter, temp);
}
}