File: eval.c

package info (click to toggle)
rel 1.3-3
  • links: PTS
  • area: non-free
  • in suites: hamm, potato, slink
  • size: 496 kB
  • ctags: 216
  • sloc: ansic: 1,868; sh: 254; makefile: 142
file content (475 lines) | stat: -rw-r--r-- 12,292 bytes parent folder | download
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
/*

------------------------------------------------------------------------------

A license is hereby granted to reproduce this software source code and
to create executable versions from this source code for personal,
non-commercial use.  The copyright notice included with the software
must be maintained in all copies produced.

THIS PROGRAM IS PROVIDED "AS IS". THE AUTHOR PROVIDES NO WARRANTIES
WHATSOEVER, EXPRESSED OR IMPLIED, INCLUDING WARRANTIES OF
MERCHANTABILITY, TITLE, OR FITNESS FOR ANY PARTICULAR PURPOSE.  THE
AUTHOR DOES NOT WARRANT THAT USE OF THIS PROGRAM DOES NOT INFRINGE THE
INTELLECTUAL PROPERTY RIGHTS OF ANY THIRD PARTY IN ANY COUNTRY.

Copyright (c) 1995, John Conover, All Rights Reserved.

Comments and/or bug reports should be addressed to:

    john@johncon.com (John Conover)

------------------------------------------------------------------------------

eval.c, evaluation for postfix notation

To evaluate the postfix stack, the postfix stack is scanned; if an
identifier is found the EVAL element that is referenced by the POSTFIX
element is pushed on the evaluation stack (using the "next" element to
establish the links;) if an operand is found, two EVAL elements are
pop'ed from the stack, the result of the boolean operation of the
number of matches found in the two elements is computed, and pushed
back on the stack-syntax for this operation is verified in postfix (),
in postfix.c

The heuristic logical boolean operations are defined as follows:

    if the operation is logical or, the sum of the matches are pushed
    on the stack

    if the operation is logical and, the sum of the matches are pushed
    on the stack

    if the operation is logical not, and the number of matches is
    non-zero, zero is pushed on the stack, otherwise, the number of
    matches found so far is pushed back on the stack

Note that this "relevance weighting" method describes a set theoretic
situation that could be considered counterintuitive between the
logical or and logical and, (depending on your point of view,) since
the intersection of a set of words in multiple documents, (ie.,
logical and) would seem to present a "stronger relevance" than the
union (ie., logical or) of a set of words in multiple
documents-however they have identical relevance values with this
methodology

There is one other comment that may be significant regarding relevance
determination of documents that are composed in one of the
standardized general markup languages, like TeX/LaTeX, or SGML-it may
be possible to "weight" the relevance of search matches on where words
are found in the structure of the document, for example, if the search
was for "numerical" and "methods," \chapter{Numerical Methods} would
be weighted "stronger" than if the words were found in
\section{Numerical Methods}, which in turn would be weighted
"stronger" than if the words were found in a paragraph. This would
permit relevance of a document to be determined by how author
structured the document.

For a detailed description of evaluating postfix notation, see, "Data
Structures Using C," Aaron M. Tenenbaum, Yedidyah Langsam, Moshe
J. Augenstein, Prentice Hall, Englewood Cliffs, New Jersey, 1990, ISBN
0-13-199746-7, pp 86.

To test this module, compile with -DTEST_SEARCHFILE

$Revision: 1.0 $
$Date: 1995/04/22 05:13:18 $
$Id: eval.c,v 1.0 1995/04/22 05:13:18 john Exp $
$Log: eval.c,v $
 * Revision 1.0  1995/04/22  05:13:18  john
 * Initial revision
 *

*/

#include "rel.h"

#ifndef LINT /* include rcsid only if not running lint */

static char rcsid[] = "$Id: eval.c,v 1.0 1995/04/22 05:13:18 john Exp $"; /* module version */
static char rcsid_h[] = EVAL_H_ID; /* module include version */

#endif

static EVAL *eval_stack = (EVAL *) 0; /* reference to the postfix evaluation stack */

#ifdef __STDC__

static void or (EVAL *element);
static void and (EVAL *element);
static void not (EVAL *element);
static void identifier (EVAL *element);

#else

static void or ();
static void and ();
static void not ();
static void identifier ();

#endif

/*

int allocate_eval (ELEMENT *element);

    allocate an EVAL element, and reference the function to call to
    handle the evaluation for this token

The algorithm is as follows:

    allocate the EVAL element

    set the the function to correspond to the precedence of the token
    (the precedence is also a unique identifier for the token type)

Usage is a call with a reference to the element from the postfix
stack, for example:

    if ((postfix_error = allocate_eval (new_element)) != NO_ERROR)
    {
        handle_error (postfix_error);
    }

The single argument, element, is a reference to the element from the
postfix stack

Returns zero if successful, non-zero if error

*/

#ifdef __STDC__

int allocate_eval (ELEMENT *element)

#else

int allocate_eval (element)
    ELEMENT *element;

#endif

{
    int retval = URMEM_ERR; /* return value, assume error allocating memory */

    if ((element->eval = (EVAL *) memalloc (sizeof (EVAL))) != (EVAL *) 0) /* allocate a new EVAL element for the token */
    {
        retval = NO_ERROR; /* assume no errors */
    }

    switch (element->precedence)
    {

        case LEFT: /* '(' grouping operator */

            retval = URSYN_ERR; /* can't be, syntax error */
            break;

        case OR: /* "or" operator */

            element->eval->function = (PTF) or; /* or operator function */
            break;

        case AND: /* "and" operator */

            element->eval->function = (PTF) and; /* and operator function */
            break;

        case NOT: /* "not" operator */

            element->eval->function = (PTF) not; /* not operator function */
            break;

        case RIGHT: /* ')' grouping operator */

            retval = URSYN_ERR; /* can't be, syntax error */
            break;

        case IDENTIFIER: /* a word */

            element->eval->function = (PTF) identifier; /* identifier operator function */
            break;

        case NONE: /* no token */

            retval = URSYN_ERR; /* can't be, syntax error */
            break;

        default:

            retval = URSYN_ERR; /* can't be, syntax error */
            break;

    }

    return (retval); /* return any errors */
}

/*

int postfix_eval (ELEMENT *postfix_stack);

    evaluate the postfix stack, referenced by postfix_stack

The algorithm is as follows:

    for each element on the postfix stack

    if the element is a token IDENTIFIER

        save the count of matches of the token

    call the functional operation for the element on the postfix stack

    the first element on the evaluation stack contains the relevance
    of the search, return it

Usage is a call with a reference to the element from the postfix
stack, for example:

    if ((value = postfix_eval (postfix_stack)) > 0)
    {
        handle_relevance (value);
    }

The single argument, postfix_stack, is a reference to the postfix
stack

Returns the computed relevance value

*/

#ifdef __STDC__

int postfix_eval (ELEMENT *postfix_stack)

#else

int postfix_eval (postfix_stack)
    ELEMENT *postfix_stack;

#endif

{
    ELEMENT *element = postfix_stack; /* reference to element on the postfix stack */

    eval_stack = (EVAL *) 0; /* null the postfix eval stack */

    while (element != (ELEMENT *) 0) /* for each element on the postfix stack */
    {

        if (element->precedence == IDENTIFIER) /* element represent an identifier? */
        {
            element->eval->value = element->pattern->count; /* yes, get the count of matches for this identifier */
        }

        (*element->eval->function) (element->eval); /* call the element's corresponding function operation */
        element = element->next; /* next element on the postfix stack */
    }

    return (eval_stack->value); /* the first element on the evaluation stack contains the computed relevance value */
}


/*

static void or (EVAL *element);

    compute the value for the "or" boolean operation

The algorithm is as follows:

    pop two evaluation elements off of the evaluation stack

    if either, or both, of the elements have search matches

        push the the sum of the matches in both elements back on the
        stack

    else

        push zero back on the stack

The single argument, element, is a reference to the element from the
postfix stack

Returns nothing

*/

#ifdef __STDC__

static void or (EVAL *element)

#else

static void or (element)
    EVAL *element;

#endif

{
    EVAL *operand1, /* top evaluation structure on the evaluation stack */
         *operand2; /* next evaluation structure on the evaluation stack */

    operand1 = POP (eval_stack); /* pop the top evaluation structure off the evaluation stack */
    operand2 = POP (eval_stack); /* pop the next evaluation structure off the evaluation stack */

    if (operand1->value != 0 || operand2->value != 0) /* either evaluation structure have matches? */
    {
        element->value = operand1->value + operand2->value; /* yes, the result is the sum of the matches */
    }

    else
    {
        element->value = 0; /* no, no matches */
    }

    PUSH (eval_stack, element); /* push the result back on the evaluation stack */
}


/*

static void and (EVAL *element);

    compute the value for the "and" boolean operation

The algorithm is as follows:

    pop two evaluation elements off of the evaluation stack

    if both of the elements have search matches

        push the the sum of the matches in both elements back on the
        stack

    else

        push zero back on the stack

The single argument, element, is a reference to the element from the
postfix stack

Returns nothing

*/

#ifdef __STDC__

static void and (EVAL *element)

#else

static void and (element)
    EVAL *element;

#endif

{
    EVAL *operand1, /* top evaluation structure on the evaluation stack */
         *operand2; /* next evaluation structure on the evaluation stack */

    operand1 = POP (eval_stack); /* pop the top evaluation structure off the evaluation stack */
    operand2 = POP (eval_stack); /* pop the next evaluation structure off the evaluation stack */

    if (operand1->value == 0 || operand2->value == 0) /* either evaluation structure no matches? */
    {
        element->value = 0; /* yes, no matches */
    }

    else
    {
        element->value = operand1->value + operand2->value; /* no, the result is the sum of the matches */
    }

    PUSH (eval_stack, element); /* push the result back on the evaluation stack */
}

/*

static void not (EVAL *element);

    compute the value for the "not" boolean operation

The algorithm is as follows:

    pop two evaluation elements off of the evaluation stack

    if the last element has matches

        puch zero on the stack

    else

        push the the matches of the first element on the stack stack

The single argument, element, is a reference to the element from the
postfix stack

Returns nothing

*/

#ifdef __STDC__

static void not (EVAL *element)

#else

static void not (element)
    EVAL *element;

#endif

{
    EVAL *operand1, /* top evaluation structure on the evaluation stack */
         *operand2; /* next evaluation structure on the evaluation stack */

    operand1 = POP (eval_stack); /* pop the top evaluation structure off the evaluation stack */
    operand2 = POP (eval_stack); /* pop the next evaluation structure off the evaluation stack */

    if (operand1->value != 0) /* first evaluation structure have matches? */
    {
        element->value = 0; /* yes, no matches */
    }

    else
    {
        element->value = operand2->value; /* no, matches are the same as the second element structure */
    }

    PUSH (eval_stack, element); /* push the result back on the evaluation stack */
}

/*

static void identifier (EVAL *element);

    compute the value for the "identifier" operation

The algorithm is as follows:

    puch the element on the evaluation stack

The single argument, element, is a reference to the element from the
postfix stack

Returns nothing

*/

#ifdef __STDC__

static void identifier (EVAL *element)

#else

static void identifier (element)
    EVAL *element;

#endif

{
    PUSH (eval_stack, element); /* push the eval struture on the eval stack */
}