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
|
/*
* wordtree.c - Routines to manage word tree and synonimous list
*
* Copyright (C) 2003 Giuseppe Modugno
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "wordtree.h"
/* Internal function prototypes. */
void write_bigend( FILE *f, unsigned int n );
void wordtree_output_idx( WTelem *tree, FILE *idx );
void wordtree_output_dat( WTelem *tree, FILE *dat );
WTelem *
wordtree_add( char *word, WTelem **tree )
{
/* Add an element in the tree, maintaining alphabetic order of words.
* Return the new allocated element in the tree.
* If word exists, return the tree element present in tree without
* allocating another element. */
int cmp;
if( *tree==NULL ) {
/* Create a new element in the tree. */
if( (*tree=(WTelem *)malloc(sizeof(WTelem)))==NULL )
return(NULL);
/* Copy string. */
(*tree)->word = strdup( word );
/* Set default status for the word. */
(*tree)->synlist = NULL;
(*tree)->num_syn = 0;
(*tree)->left = NULL;
(*tree)->right = NULL;
(*tree)->isword = 0;
return(*tree);
}
if( (cmp=strcmp(word,(*tree)->word))>0 )
return wordtree_add(word,&(*tree)->right);
else if( cmp<0 )
return wordtree_add(word,&(*tree)->left);
else
return(*tree); /* Word is present in the tree. */
}
SLelem *
synlist_add( WTelem *word, WTelem *syn )
{
/* Add synonimous syn to the word word. */
SLelem **list=&(word->synlist);
SLelem *new;
int cmp;
/* Check if synonimous <syn> is the first of the list. */
if( (*list==NULL) || (cmp=strcmp( syn->word, (*list)->syn->word ))<0 ) {
/* Add synonimous <syn> as the first element of the list. */
if( (new=(SLelem *)malloc(sizeof(SLelem)))==NULL )
return(NULL);
new->syn = syn;
new->next = *list;
word->num_syn++;
return( *list=new );
}
if( !cmp ) {
/* First element of synonimous list is equal to the new synonimous. */
fprintf( stderr, "Warning: synonimous %s yet added to word %s\n", syn->word, word->word );
return( *list );
}
/* Go down to the list up to the element alphabetically previous of new
* synonimous <syn>. */
while( ((*list)->next!=NULL) && (cmp=strcmp(syn->word,(*list)->next->syn->word))>0 )
list = &((*list)->next);
if( !cmp ) {
/* Found the same synonimous in the list. */
fprintf( stderr, "Warning: synonimous %s yet added to word %s\n", syn->word, word->word );
return( (*list)->next );
}
/* Allocate new element list. */
if( (new=(SLelem *)malloc(sizeof(SLelem)))==NULL )
return(NULL);
new->syn = syn;
new->next = (*list)->next;
word->num_syn++;
/* Add new element to the list. */
return( (*list)->next=new );
}
void
wordtree_output_idx( WTelem *tree, FILE *idx )
{
/* Write output .idx files. */
static unsigned int index=0;
static unsigned int dat_offset=0;
if( tree->left!=NULL )
wordtree_output_idx( tree->left, idx );
tree->index = index++;
/* Write word and offset of dat file to idx file. */
fprintf( idx, "%s,%u\n", tree->word, dat_offset );
dat_offset += 2+(tree->num_syn<<1);
if( !tree->isword )
fprintf( stderr, "Warning: Word %s is only a synonimous\n", tree->word );
if( !tree->num_syn )
fprintf( stderr, "Warning: Word %s has no synonimous\n", tree->word );
if( tree->right!=NULL )
wordtree_output_idx( tree->right, idx );
}
void
wordtree_output_dat( WTelem *tree, FILE *dat )
{
/* Write output .dat files. */
SLelem *sle;
static unsigned int dat_offset = 0;
if( tree->left!=NULL )
wordtree_output_dat( tree->left, dat );
write_bigend( dat, tree->num_syn );
dat_offset += 2;
sle = tree->synlist;
while( sle!=NULL ) {
write_bigend( dat, sle->syn->index );
dat_offset += 2;
sle = sle->next;
}
if( tree->right!=NULL )
wordtree_output_dat( tree->right, dat );
}
void
wordtree_output( WTelem *tree, FILE *idx, FILE *dat )
{
/* Write output .idx and .dat files. */
/* It's important to write .idx file first because
* write_output_idx() enumerate words too and index
* is used by write_output_dat() routine. */
wordtree_output_idx( tree, idx );
wordtree_output_dat( tree, dat );
}
void
write_bigend( FILE *f, unsigned int n )
{
/* Write number n to file f as a 16-bit Big Endian format. */
fprintf( f, "%c", (unsigned char)((n&0xFF00)>>8) );
fprintf( f, "%c", (unsigned char)(n&0x00FF) );
}
void
wordtree_free( WTelem *tree )
{
/* Free tree memory. */
SLelem *sle, *stmp;
if( tree->left!=NULL )
wordtree_free( tree->left );
if( tree->right!=NULL )
wordtree_free( tree->right );
/* Free synonimous list of that word. */
sle = tree->synlist;
while( sle!=NULL ) {
stmp = sle->next;
free(sle);
sle = stmp;
}
/* Free tree element. */
free(tree);
}
|