forked from MycroftAI/mimic1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcst_item.c
382 lines (325 loc) · 11.2 KB
/
cst_item.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*************************************************************************/
/* */
/* Language Technologies Institute */
/* Carnegie Mellon University */
/* Copyright (c) 1999 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author: Alan W Black ([email protected]) */
/* Date: December 1999 */
/*************************************************************************/
/* */
/* Items (and Item Contents) */
/* */
/*************************************************************************/
#include "cst_alloc.h"
#include "cst_item.h"
#include "cst_relation.h"
#include "cst_utterance.h"
/* Define functions for using items, rels and utts as vals */
CST_VAL_REGISTER_TYPE_NODEL(relation, cst_relation);
CST_VAL_REGISTER_TYPE_NODEL(item, cst_item);
CST_VAL_REGISTER_TYPE(utterance, cst_utterance);
CST_VAL_REGISTER_FUNCPTR(itemfunc, cst_itemfunc);
cst_item *new_item_relation(cst_relation *r, cst_item *i)
{
cst_item *ni;
ni = cst_utt_alloc(r->utterance, cst_item, 1);
ni->contents = 0;
ni->n = ni->p = ni->u = ni->d = 0;
ni->relation = r;
item_contents_set(ni, i);
return ni;
}
void item_contents_set(cst_item *current, cst_item *i)
{
cst_item_contents *c = 0;
cst_item *nn_item;
if (i == 0)
c = new_item_contents(current);
else
c = i->contents;
if (c != current->contents)
{
item_unref_contents(current);
current->contents = c;
/* If this contents is already in this relation */
/* empty the other reference */
if (feat_present
(current->contents->relations, current->relation->name))
{ /* oops this is already in this relation */
nn_item = val_item(feat_val(current->contents->relations,
current->relation->name));
feat_set(nn_item->contents->relations,
current->relation->name, item_val(nn_item));
}
/* Add back reference */
feat_set(current->contents->relations,
current->relation->name, item_val(current));
}
}
void delete_item(cst_item *item)
{
cst_item *ds, *nds;
if (item->n != NULL)
{
item->n->p = item->p;
item->n->u = item->u; /* in trees if this is first daughter */
}
if (item->p != NULL)
item->p->n = item->n;
if (item->u != NULL)
item->u->d = item->n; /* when first daughter */
if (item->relation)
{
if (item->relation->head == item)
item->relation->head = item->n;
if (item->relation->tail == item)
item->relation->tail = item->p;
}
/* Delete all the daughters of item */
for (ds = item->d; ds; ds = nds)
{
nds = ds->n;
delete_item(ds);
}
item_unref_contents(item);
cst_utt_free(item->relation->utterance, item);
}
void item_unref_contents(cst_item *item)
{
/* unreference this item from contents, and delete contents */
/* if no one else is referencing it */
if (item && item->contents)
{
feat_remove(item->contents->relations, item->relation->name);
if (feat_length(item->contents->relations) == 0)
{
delete_features(item->contents->relations);
delete_features(item->contents->features);
cst_utt_free(item->relation->utterance, item->contents);
}
item->contents = NULL;
}
}
cst_item_contents *new_item_contents(cst_item *i)
{
cst_item_contents *ic;
ic = cst_utt_alloc(i->relation->utterance, cst_item_contents, 1);
ic->features = new_features_local(i->relation->utterance->ctx);
ic->relations = new_features_local(i->relation->utterance->ctx);
return ic;
}
cst_item *item_as(const cst_item *i, const char *rname)
{
/* return i as relation rname or null */
const cst_val *v;
if (i == NULL)
return NULL;
else
{
v = feat_val(i->contents->relations, rname);
if (v != NULL)
return val_item(v);
else
return NULL;
}
}
/********************************************************************/
/* List relation related functions */
/********************************************************************/
cst_item *item_next(const cst_item *i)
{
if (i == NULL)
return NULL;
else
return i->n;
}
cst_item *item_prev(const cst_item *i)
{
if (i == NULL)
return NULL;
else
return i->p;
}
cst_item *item_append(cst_item *current, cst_item *ni)
{
cst_item *rni = 0;
if (ni && (ni->relation == current->relation))
{
/* got to delete it first as an item can't be in a relation twice */
}
else
rni = new_item_relation(current->relation, ni);
rni->n = current->n;
if (current->n != NULL)
current->n->p = rni;
rni->p = current;
current->n = rni;
if (current->relation->tail == current)
current->relation->tail = rni;
return rni;
}
cst_item *item_prepend(cst_item *current, cst_item *ni)
{
cst_item *rni = 0;
if (ni && (ni->relation == current->relation))
{
/* got to delete it first as an item can't be in a relation twice */
}
else
rni = new_item_relation(current->relation, ni);
rni->p = current->p;
if (current->p != NULL)
current->p->n = rni;
rni->n = current;
current->p = rni;
if (current->u) /* in a tree */
{
current->u->d = rni;
rni->u = current->u;
current->u = NULL;
}
if (current->relation->head == current)
current->relation->head = rni;
return rni;
}
/********************************************************************/
/* Tree relation related functions */
/********************************************************************/
cst_item *item_parent(const cst_item *i)
{
const cst_item *n;
for (n = i; item_prev(n); n = item_prev(n));
if (n == NULL)
return NULL;
else
return n->u;
}
cst_item *item_daughter(const cst_item *i)
{
if (i == NULL)
return NULL;
else
return i->d;
}
cst_item *item_nth_daughter(const cst_item *i, int n)
{
int d;
cst_item *p;
for (d = 0, p = item_daughter(i); p && (d < n); p = item_next(p), d++);
return p;
}
cst_item *item_last_daughter(const cst_item *i)
{
cst_item *p;
for (p = item_daughter(i); item_next(p); p = item_next(p));
return p;
}
cst_item *item_add_daughter(cst_item *i, cst_item *nd)
{
cst_item *p, *rnd;
p = item_last_daughter(i);
if (p)
rnd = item_append(p, nd);
else
{ /* first new daughter */
if (nd && (nd->relation == i->relation))
{
/* got to delete it first as nd can't be in a relation twice */
cst_errmsg("item_add_daughter: already in relation\n");
return 0;
}
else
rnd = new_item_relation(i->relation, nd);
rnd->u = i;
i->d = rnd;
}
return rnd;
}
/********************************************************************/
/* Feature functions */
/********************************************************************/
int item_feat_present(const cst_item *i, const char *name)
{
return feat_present(item_feats(i), name);
}
int item_feat_remove(const cst_item *i, const char *name)
{
return feat_remove(item_feats(i), name);
}
cst_features *item_feats(const cst_item *i)
{
return (i ? i->contents->features : NULL);
}
const cst_val *item_feat(const cst_item *i, const char *name)
{
return feat_val(item_feats(i), name);
}
int item_feat_int(const cst_item *i, const char *name)
{
return feat_int(item_feats(i), name);
}
float item_feat_float(const cst_item *i, const char *name)
{
return feat_float(item_feats(i), name);
}
const char *item_feat_string(const cst_item *i, const char *name)
{
return feat_string(item_feats(i), name);
}
void item_set(const cst_item *i, const char *name, const cst_val *val)
{
feat_set(item_feats(i), name, val);
}
void item_set_int(const cst_item *i, const char *name, int val)
{
feat_set_int(item_feats(i), name, val);
}
void item_set_float(const cst_item *i, const char *name, float val)
{
feat_set_float(item_feats(i), name, val);
}
void item_set_string(const cst_item *i, const char *name, const char *val)
{
feat_set_string(item_feats(i), name, val);
}
cst_utterance *item_utt(const cst_item *i)
{
if (i && i->relation)
return i->relation->utterance;
else
return NULL;
}
int item_equal(const cst_item *a, const cst_item *b)
{
if ((a == b) || (a && b && (a->contents == b->contents)))
return TRUE;
else
return FALSE;
}