forked from isislovecruft/discern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
502 lines (433 loc) · 16.1 KB
/
lex.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* File: lex.c
*
* The lexicon of DISCERN
*
* Copyright (C) 1994 Risto Miikkulainen
*
* This software can be copied, modified and distributed freely for
* educational and research purposes, provided that this notice is included
* in the code, and the author is acknowledged in any materials and reports
* that result from its use. It may not be used for commercial purposes
* without expressed permission from the author.
*
* $Id: lex.c,v 1.37 1994/09/20 10:46:59 risto Exp $
*/
#include <stdio.h>
#include <math.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "defs.h"
#include "globals.c"
/************ lexicon constants *************/
/* propagation */
#define ANC 1 /* response bubble radius */
#define PROPNC 4 /* number of units included in propagation */
/* keywords expected in lexsimu */
#define LEXSIMU_NLNET "lmapsize" /* lexical map size */
#define LEXSIMU_NSNET "smapsize" /* semantic map size */
#define LEXSIMU_WEIGHTS "network-weights" /* weights */
/* list of units with highest activation;
propagation takes place through these units */
typedef struct LEXPROPUNIT
{
int i, j; /* index of the unit */
float value; /* activation value */
}
LEXPROPUNIT;
/******************** Function prototypes ************************/
/* global functions */
#include "prototypes.h"
/* functions local to this file */
static void map_input __P((int taski, int modi,
FMUNIT inpunits[MAXLSNET][MAXLSNET], int ninpnet,
LEXPROPUNIT prop[], int *nprop));
static void associate __P((int taski, int modi,
FMUNIT inpunits[MAXLSNET][MAXLSNET],
LEXPROPUNIT prop[], int nprop,
FMUNIT outunits[MAXLSNET][MAXLSNET], int noutnet,
double assoc[MAXLSNET][MAXLSNET]
[MAXLSNET][MAXLSNET]));
static void assocresponse __P((FMUNIT *unit, int i, int j,
FMUNIT inpunits[MAXLSNET][MAXLSNET],
double assoc[MAXLSNET][MAXLSNET]
[MAXLSNET][MAXLSNET],
LEXPROPUNIT prop[], int nprop));
static int propunit __P((int i, int j, LEXPROPUNIT prop[]));
static void findprop __P((int lowi, int lowj, int highi, int highj,
FMUNIT inpunits[MAXLSNET][MAXLSNET],
double *worst, LEXPROPUNIT prop[], int *nprop));
static void lex_iterate_weights __P((void (*dofun) (FILE *, double *,
double, double),
FILE *fp, double par1, double par2));
/********************* lexicon translation ******************************/
void
input_lexicon (taski, index)
/* translate a lexical input word into its semantic counterpart */
int taski; /* para or qa */
int index; /* index of the input word */
{
int i;
LEXPROPUNIT prop[PROPNC]; /* propagation through these units */
int nprop; /* number of propunits */
/* form the input and target */
for (i = 0; i < ninprep[LINPMOD]; i++)
inprep[LINPMOD][i] = tgtrep[LINPMOD][i] = lwords[index].rep[i];
targets[LINPMOD][0] = index;
/* find the image of the lexical input word on the lexical map */
map_input(taski, LINPMOD, lunits, nlnet, prop, &nprop);
/* form the input and target */
for (i = 0; i < ninprep[SOUTMOD]; i++)
tgtrep[SOUTMOD][i] = swords[index].rep[i];
targets[SOUTMOD][0] = index;
/* propagate lex map activation to the semantic map and find the image */
associate (taski, SOUTMOD, lunits, prop, nprop, sunits, nsnet, lsassoc);
/* form the input vector to sentpars */
if (chain)
for (i = 0; i < nsrep; i++)
swordrep[i] = outrep[SOUTMOD][i];
}
void
output_lexicon (taski, index)
/* translate a semantic output word into its lexical counterpart */
int taski; /* para or qa */
int index; /* index of the output word */
{
int i;
LEXPROPUNIT prop[PROPNC]; /* propagation through these units */
int nprop; /* number of propunits */
/* form the input and target */
if (chain) /* take input from sentgen output */
for (i = 0; i < ninprep[SINPMOD]; i++)
{
inprep[SINPMOD][i] = swordrep[i];
tgtrep[SINPMOD][i] = swords[index].rep[i];
}
else /* otherwise take it from lexicon */
for (i = 0; i < ninprep[SINPMOD]; i++)
inprep[SINPMOD][i] = tgtrep[SINPMOD][i] = swords[index].rep[i];
targets[SINPMOD][0] = index;
/* find the image of the semantic word on the semantic map */
map_input(taski, SINPMOD, sunits, nsnet, prop, &nprop);
/* form the input and target */
for (i = 0; i < ninprep[LOUTMOD]; i++)
tgtrep[LOUTMOD][i] = lwords[index].rep[i];
targets[LOUTMOD][0] = index;
/* propagate sem map activation to the lex map and find the image */
associate (taski, LOUTMOD, sunits, prop, nprop, lunits, nlnet, slassoc);
}
static void
map_input (taski, modi, inpunits, ninpnet, prop, nprop)
/* present a word representation to the input map, find the image unit,
define the propagation neighborhood, collect statistics, and display
the map activation */
int taski, /* para or qa */
modi; /* module number */
FMUNIT inpunits[MAXLSNET][MAXLSNET];/* map units */
int ninpnet; /* map size */
LEXPROPUNIT prop[]; /* propagation through these units */
int *nprop; /* number of propunits */
{
int i, j; /* indices to the map */
double best, /* activation of the image unit */
worst; /* act. of the min responding unit
in neighborhood of the image unit */
int besti, bestj; /* indices of the image unit */
/* we don't allow mouse events during propagation */
nolexmouseevents = TRUE;
/* compute responses of the units to the external input
and find max and min responding units */
best = LARGEFLOAT;
worst = (-1.0);
for (i = 0; i < ninpnet; i++)
for (j = 0; j < ninpnet; j++)
{
/* response proportional to the distance of weight and input vectors */
distanceresponse (&inpunits[i][j], inprep[modi], ninprep[modi],
distance, NULL);
/* find best and worst and best indices */
updatebestworst (&best, &worst, &besti, &bestj, &inpunits[i][j], i, j,
fsmaller, fgreater);
}
/* determine the propagation neighborhood: the PROPNC most highly active
units within radius ANC from the image, clipped to the map boundaries;
also record the weakest response in the neighborhood, count npropunits */
findprop (clip(besti - ANC, 0, ige),
clip(bestj - ANC, 0, ige),
clip(besti + ANC, ninpnet - 1, ile),
clip(bestj + ANC, ninpnet - 1, ile),
inpunits, &worst, prop, nprop);
/* calculate the actual activation values */
for (i = 0; i < ninpnet; i++)
for (j = 0; j < ninpnet; j++)
/* only the propagation units should be active */
if (propunit(i, j, prop))
if (worst > best)
/* scale the values between max and min activation
in the entire neighborhood of radius ANC
(which may be larger than propagation neighborhood) */
inpunits[i][j].value = 1.0 - (inpunits[i][j].value - best)
/ (worst - best);
else
inpunits[i][j].value = 1.0;
else
/* make everything else 0 */
inpunits[i][j].value = 0.0;
/* collect statistics about accuracy of the input map component,
print log output */
for (i = 0; i < noutrep[modi]; i++)
outrep[modi][i] = inpunits[besti][bestj].comp[i];
collect_stats (taski, modi);
/* display map activation */
if (displaying)
{
/* display map activity */
display_lex (modi, inpunits, ninpnet);
/* display the log line */
display_lex_error (modi);
wait_and_handle_events (); /* stop if stepping, check for events */
}
}
static void
associate (taski, modi, inpunits, prop, nprop, outunits, noutnet, assoc)
/* propagate through the neighborhood to the output map, find max output unit,
collect stats, and display the map activation */
int taski; /* para or qa */
int modi; /* module number */
FMUNIT inpunits[MAXLSNET][MAXLSNET]; /* input map */
FMUNIT outunits[MAXLSNET][MAXLSNET]; /* output map */
int noutnet; /* output map size */
double assoc[MAXLSNET][MAXLSNET][MAXLSNET][MAXLSNET]; /* assoc weights */
LEXPROPUNIT prop[]; /* propagation through these units */
int nprop; /* number of propunits */
{
int i, j; /* output map indices */
double abest, /* best activation on output map */
foo = (-1.0); /* worst activation (not used) */
int associ, assocj; /* coordinates of associative image */
/* propagate activation from the input map to the output map
and find max and min responding units */
abest = (-1.0);
for (i = 0; i < noutnet; i++)
for (j = 0; j < noutnet; j++)
{
/* propagate activation from input to output map */
assocresponse (&outunits[i][j], i, j, inpunits, assoc, prop, nprop);
/* find best and worst and best indices */
updatebestworst (&abest, &foo, &associ, &assocj, &outunits[i][j],
i, j, fgreater, fsmaller);
}
/* scale the activation in the output map within 0 and 1 */
for (i = 0; i < noutnet; i++)
for (j = 0; j < noutnet; j++)
{
outunits[i][j].value = outunits[i][j].value /abest;
/* avoid unnecessary display update */
if (outunits[i][j].value < 0.1)
outunits[i][j].value = 0.0;
}
/* collect statistics about accuracy of the input map component,
print log output */
for (i = 0; i < noutrep[modi]; i++)
outrep[modi][i] = outunits[associ][assocj].comp[i];
collect_stats (taski, modi);
/* allow mouse events again */
nolexmouseevents = FALSE;
/* display map activation */
if (displaying)
{
/* display map activity */
display_lex (modi, outunits, noutnet);
/* display the log line */
display_lex_error (modi);
wait_and_handle_events (); /* stop if stepping, check for events */
}
}
static void
assocresponse (unit, i, j, inpunits, assoc, prop, nprop)
/* propagate activation from the input map to this output map unit */
FMUNIT *unit; /* unit on the map */
int i, j;
FMUNIT inpunits[MAXLSNET][MAXLSNET]; /* input map */
double assoc[MAXLSNET][MAXLSNET][MAXLSNET][MAXLSNET];
/* associative weights */
LEXPROPUNIT prop[]; /* propage through these units */
int nprop; /* number of propunits */
{
int p;
/* save previous value to avoid redisplay if it does not change */
unit->prevvalue = unit->value;
/* propagate activity in the input map through the associative weights
to the output unit */
unit->value = 0.0;
for (p = 0; p < nprop; p++)
unit->value += inpunits[prop[p].i][prop[p].j].value
* assoc[prop[p].i][prop[p].j][i][j];
}
/****************** propagation neighborhood *******************/
static void
findprop (lowi, lowj, highi, highj, inpunits, worst, prop, nprop)
/* find the PROPNC units in the neighborhood that should be included
in the propagation and place them in the global array prop;
as a side effect, determine the lowest activity in the neighborhood
(used for scaling the activations for display) */
int lowi, lowj, highi, highj; /* boundaries of the neighborhood */
FMUNIT inpunits[MAXLSNET][MAXLSNET]; /* the map */
double *worst; /* lowest activity in the neighborhd */
LEXPROPUNIT prop[]; /* propagation through these units */
int *nprop; /* number of propunits */
{
int i, j, /* map indices */
p, k; /* indices to activation list */
prop[0].value = LARGEFLOAT; /* initialize the list for sorting */
*worst = (-1.0); /* initialize the weakest value */
for (i = lowi; i <= highi; i++) /* look at all units in the */
for (j = lowj; j <= highj; j++) /* neighborhood */
{
/* see if the activation of the current value is within
the PROPNC highest activations found so far
(note: low value means high activation) */
for (p = 0; p < PROPNC; p++)
if (inpunits[i][j].value < prop[p].value)
{
/* found the right place; move the rest of the list down */
for (k = PROPNC - 1; k > p; k--)
{
prop[k].value = prop[k - 1].value;
prop[k].i = prop[k - 1].i;
prop[k].j = prop[k - 1].j;
}
/* insert the current value in the list */
prop[p].value = inpunits[i][j].value;
prop[p].i = i;
prop[p].j = j;
break;
}
/* update the lowest activation (highest value) in the neighborhood */
if (inpunits[i][j].value > *worst)
*worst = inpunits[i][j].value;
}
/* set up the number of units in the neighborhood for propagation */
*nprop = (highi - lowi + 1) * (highj - lowj + 1);
if (PROPNC < *nprop)
*nprop = PROPNC;
}
static int
propunit (i, j, prop)
/* check if unit i, j is in the list of units included in the propagation */
int i, j; /* unit indices */
LEXPROPUNIT prop[]; /* propagation through these units */
{
int p;
for (p = 0; p < PROPNC; p++)
/* prop[p].value would be LARGEFLOAT if there are only p < PROPNC units
in the neighborhood of radius ANC */
if (prop[p].value < LARGEFLOAT)
{
if (prop[p].i == i && prop[p].j == j)
return(TRUE);
}
else break;
return (FALSE);
}
/********************* initializations ******************************/
void
lex_init ()
/* initialize the lexicon */
{
FILE *fp;
/* set up numbers of input/output words and reps for convenience */
ninputs[LINPMOD] = noutputs[LINPMOD] = 1;
ninputs[LOUTMOD] = noutputs[LOUTMOD] = 1;
ninputs[SINPMOD] = noutputs[SINPMOD] = 1;
ninputs[SOUTMOD] = noutputs[SOUTMOD] = 1;
ninprep[LINPMOD] = noutrep[LINPMOD] = nlrep;
ninprep[LOUTMOD] = noutrep[LOUTMOD] = nlrep;
ninprep[SINPMOD] = noutrep[SINPMOD] = nsrep;
ninprep[SOUTMOD] = noutrep[SOUTMOD] = nsrep;
/* open the weight file */
printf ("Reading lexicon from %s...", lexfile);
open_file (lexfile, "r", &fp, REQUIRED);
/* ignore everything until the lnet size */
read_till_keyword (fp, LEXSIMU_NLNET, REQUIRED);
fscanf (fp, "%d", &nlnet);
if (nlnet > MAXLSNET || nlnet <= 0)
{
fprintf (stderr, "%s %s exceeds array size\n", lexfile, LEXSIMU_NLNET);
exit (EXIT_SIZE_ERROR);
}
read_till_keyword (fp, LEXSIMU_NSNET, REQUIRED);
fscanf (fp, "%d", &nsnet);
if (nsnet > MAXLSNET || nsnet <= 0)
{
fprintf (stderr, "%s %s exceeds array size\n", lexfile, LEXSIMU_NSNET);
exit (EXIT_SIZE_ERROR);
}
/* then ignore everything until the weights */
read_till_keyword (fp, LEXSIMU_WEIGHTS, REQUIRED);
/* read the weights */
lex_iterate_weights (readfun, fp, NOT_USED, NOT_USED);
fclose (fp);
printf ("Done.\n");
}
static void
lex_iterate_weights (dofun, fp, par1, par2)
/* read the weights from the file */
void (*dofun) __P((FILE *, double *, double, double)); /* read function */
FILE *fp;
double par1, par2;
{
int i, j, k, ii, jj;
/* lexical map input weights */
for (i = 0; i < nlnet; i++)
for (j = 0; j < nlnet; j++)
for (k = 0; k < nlrep; k++)
(*dofun) (fp, &lunits[i][j].comp[k], par1, par2);
/* semantic map input weights */
for (i = 0; i < nsnet; i++)
for (j = 0; j < nsnet; j++)
for (k = 0; k < nsrep; k++)
(*dofun) (fp, &sunits[i][j].comp[k], par1, par2);
/* associative connections */
for (i = 0; i < nlnet; i++)
for (j = 0; j < nlnet; j++)
for (ii = 0; ii < nsnet; ii++)
for (jj = 0; jj < nsnet; jj++)
{
(*dofun) (fp, &lsassoc[i][j][ii][jj], par1, par2);
(*dofun) (fp, &slassoc[ii][jj][i][j], par1, par2);
}
}
void
lex_clear_values (units, nnet)
/* clear map activations */
FMUNIT units[MAXLSNET][MAXLSNET]; /* map */
int nnet; /* map size */
{
int i, j;
for (i = 0; i < nnet; i++)
for (j = 0; j < nnet; j++)
units[i][j].value = 0.0;
}
void
lex_clear_prevvalues (units, nnet)
/* reset the previous activations to NOPREV, so that the whole map
will be displayed (because prevvalues are different to current values) */
FMUNIT units[MAXLSNET][MAXLSNET]; /* map */
int nnet; /* map size */
{
int i, j;
for (i = 0; i < nnet; i++)
for (j = 0; j < nnet; j++)
units[i][j].prevvalue = NOPREV;
}
void
lex_clear_labels (units, nnet)
/* clear map activations */
FMUNIT units[MAXLSNET][MAXLSNET]; /* map */
int nnet; /* map size */
{
int i, j;
for (i = 0; i < nnet; i++)
for (j = 0; j < nnet; j++)
units[i][j].labelcount = 0;
}