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
|
/* A simple N-gram lexer. */
/* Copyright (C) 1997, 1998, 1999 Andrew McCallum
Written by: Andrew Kachites McCallum <mccallum@cs.cmu.edu>
This file is part of the Bag-Of-Words Library, `libbow'.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation, version 2.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */
#include <bow/libbow.h>
#define SELF ((bow_lexer_gram*)self)
#define LEX ((bow_lex_gram*)lex)
bow_lex *
bow_lexer_gram_open_text_fp (bow_lexer *self, FILE *fp,
const char *filename)
{
bow_lex *lex = bow_lexer_next_open_text_fp (self, fp, filename);
if (lex == NULL)
return NULL;
lex = bow_realloc (lex, self->sizeof_lex);
LEX->gram_size_this_time = SELF->gram_size;
return lex;
}
bow_lex *
bow_lexer_gram_open_str (bow_lexer *self, char *buf)
{
bow_lex *lex = bow_lexer_next_open_str (self, buf);
if (lex == NULL)
return NULL;
LEX->gram_size_this_time = SELF->gram_size;
return lex;
}
int
bow_lexer_gram_get_word (bow_lexer *self, bow_lex *lex,
char *buf, int buflen)
{
int i;
char **tokens;
int s;
int len;
#if BOW_MCHECK
mprobe (lex);
#endif /* BOW_MCHECK */
tokens = alloca (sizeof (char*) * LEX->gram_size_this_time);
for (i = 0; i < LEX->gram_size_this_time; i++)
tokens[i] = alloca (BOW_MAX_WORD_LENGTH);
/* Remember where we started. */
s = LEX->lex.document_position;
/* Get the first token. */
if (self->next->get_word (self->next, lex, tokens[0], BOW_MAX_WORD_LENGTH)
== 0)
return 0;
/* Get the next n-1 tokens. */
for (i = 1; i < LEX->gram_size_this_time; i++)
if (self->next->get_word (self->next, lex, tokens[i], BOW_MAX_WORD_LENGTH)
== 0)
*(tokens[i]) = '\0';
/* Make sure it will fit. */
for (i = 0, len = 0; i < LEX->gram_size_this_time; i++)
len += strlen (tokens[i]) + 1;
assert (len < BOW_MAX_WORD_LENGTH);
/* Fill buf with the tokens concatenated. */
strcpy (buf, tokens[0]);
for (i = 1; i < LEX->gram_size_this_time; i++)
{
strcat (buf, ";");
strcat (buf, tokens[i]);
}
/* Put us back to the second token so we can get it with the next call */
if (LEX->gram_size_this_time > 1)
LEX->lex.document_position = s;
if (LEX->gram_size_this_time == 1)
LEX->gram_size_this_time = SELF->gram_size;
else
LEX->gram_size_this_time--;
#if BOW_MCHECK
mprobe (lex);
#endif /* BOW_MCHECK */
return strlen (buf);
}
const bow_lexer_gram _bow_gram_lexer =
{
{
sizeof (bow_lex_gram),
NULL, /* This must be non-NULL at run-time */
bow_lexer_gram_open_text_fp,
bow_lexer_gram_open_str,
bow_lexer_gram_get_word,
NULL,
NULL,
bow_lexer_simple_close
},
1 /* default gram-size is 1 */
};
const bow_lexer_gram *bow_gram_lexer = &_bow_gram_lexer;
|