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
|
/* A lexer will special features for handling HTML. */
/* Copyright (C) 1997, 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>
#include <ctype.h> /* for tolower() */
static const struct
{
char *entityName;
unsigned char entityChar;
} htmlEntities[] = {
{ "nbsp", 32 }, /* non-breaking whitespace */
{ "quot", 34 }, /* double quote */
{ "amp", 38 }, /* ampersand */
{ "lt", 60 }, /* less than */
{ "gt", 62 }, /* greater than */
{ "iexcl", 161 }, /* inverted exclamation mark */
{ "cent", 161 }, /* cent sign */
{ "pound", 163 }, /* pound sign */
{ "yen", 165 }, /* yen sign */
{ "brvbar", 166 }, /* broken vertical bar */
{ "sect", 167 }, /* section sign */
{ "copy", 169 }, /* copyright sign */
{ "laquo", 171 }, /* angle quotation mark, left */
{ "raquo", 187 }, /* angle quotation mark, right */
{ "not", 172 }, /* negation sign */
{ "reg", 174 }, /* circled r registered sign */
{ "deg", 176 }, /* degree sign */
{ "plusmn", 177 }, /* plus or minus sign */
{ "sup2", 178 }, /* superscript 2 */
{ "sup3", 179 }, /* superscript 3 */
{ "micro", 181 }, /* micro sign */
{ "para", 182 }, /* paragraph sign */
{ "sup1", 185 }, /* superscript 1 */
{ "middot", 183 }, /* center dot */
{ "frac14", 188 }, /* fraction 1/4 */
{ "frac12", 189 }, /* fraction 1/2 */
{ "iquest", 191 }, /* inverted question mark */
{ "frac34", 190 }, /* fraction 3/4 */
{ "aelig", 198 }, /* capital ae diphthong (ligature) */
{ "aacute", 193 }, /* capital a, acute accent */
{ "acirc", 194 }, /* capital a, circumflex accent */
{ "agrave", 192 }, /* capital a, grave accent */
{ "aring", 197 }, /* capital a, ring */
{ "atilde", 195 }, /* capital a, tilde */
{ "auml", 196 }, /* capital a, dieresis or umlaut mark */
{ "ccedil", 199 }, /* capital c, cedilla */
{ "eth", 208 }, /* capital eth, icelandic */
{ "eacute", 201 }, /* capital e, acute accent */
{ "ecirc", 202 }, /* capital e, circumflex accent */
{ "egrave", 200 }, /* capital e, grave accent */
{ "euml", 203 }, /* capital e, dieresis or umlaut mark */
{ "iacute", 205 }, /* capital i, acute accent */
{ "icirc", 206 }, /* capital i, circumflex accent */
{ "igrave", 204 }, /* capital i, grave accent */
{ "iuml", 207 }, /* capital i, dieresis or umlaut mark */
{ "ntilde", 209 }, /* capital n, tilde */
{ "oacute", 211 }, /* capital o, acute accent */
{ "ocirc", 212 }, /* capital o, circumflex accent */
{ "ograve", 210 }, /* capital o, grave accent */
{ "oslash", 216 }, /* capital o, slash */
{ "otilde", 213 }, /* capital o, tilde */
{ "ouml", 214 }, /* capital o, dieresis or umlaut mark */
{ "thorn", 222 }, /* capital thorn, icelandic */
{ "uacute", 218 }, /* capital u, acute accent */
{ "ucirc", 219 }, /* capital u, circumflex accent */
{ "ugrave", 217 }, /* capital u, grave accent */
{ "uuml", 220 }, /* capital u, dieresis or umlaut mark */
{ "yacute", 221 }, /* capital y, acute accent */
{ "aacute", 225 }, /* small a, acute accent */
{ "acirc", 226 }, /* small a, circumflex accent */
{ "aelig", 230 }, /* small ae diphthong (ligature) */
{ "agrave", 224 }, /* small a, grave accent */
{ "aring", 229 }, /* small a, ring */
{ "atilde", 227 }, /* small a, tilde */
{ "auml", 228 }, /* small a, dieresis or umlaut mark */
{ "ccedil", 231 }, /* small c, cedilla */
{ "eacute", 233 }, /* small e, acute accent */
{ "ecirc", 234 }, /* small e, circumflex accent */
{ "egrave", 232 }, /* small e, grave accent */
{ "eth", 240 }, /* small eth, icelandic */
{ "euml", 235 }, /* small e, dieresis or umlaut mark */
{ "iacute", 237 }, /* small i, acute accent */
{ "icirc", 238 }, /* small i, circumflex accent */
{ "igrave", 236 }, /* small i, grave accent */
{ "iuml", 239 }, /* small i, dieresis or umlaut mark */
{ "ntilde", 241 }, /* small n, tilde */
{ "oacute", 243 }, /* small o, acute accent */
{ "ocirc", 244 }, /* small o, circumflex accent */
{ "ograve", 242 }, /* small o, grave accent */
{ "oslash", 248 }, /* small o, slash */
{ "otilde", 245 }, /* small o, tilde */
{ "ouml", 246 }, /* small o, dieresis or umlaut mark */
{ "szlig", 223 }, /* small sharp s, german (sz ligature) */
{ "thorn", 254 }, /* small thorn, icelandic */
{ "uacute", 250 }, /* small u, acute accent */
{ "ucirc", 251 }, /* small u, circumflex accent */
{ "ugrave", 249 }, /* small u, grave accent */
{ "uuml", 252 }, /* small u, dieresis or umlaut mark */
{ "yacute", 253 }, /* small y, acute accent */
{ "yuml", 255 } /* small y, dieresis or umlaut mark */
};
int entityMaxLen;
static bow_int4str *entityMap;
#define PARAMS (bow_default_lexer_parameters)
static void initEntityMap()
{
int i;
if (entityMap != NULL)
return;
entityMap = bow_int4str_new(255);
/* the below must be done from 0-N or logic in get_word below breaks */
for (i = 0; i < sizeof(htmlEntities)/sizeof(htmlEntities[0]); i++) {
bow_str2int (entityMap, htmlEntities[i].entityName);
if (entityMaxLen < strlen(htmlEntities[i].entityName))
entityMaxLen = strlen(htmlEntities[i].entityName);
}
}
int
bow_lexer_html_get_raw_word (bow_lexer *self, bow_lex *lex,
char *buf, int buflen)
{
int byte; /* characters read from the FP */
int wordlen; /* number of characters in the word so far */
int html_bracket_nestings = 0;
assert (lex->document_position <= lex->document_length);
if (entityMap == NULL)
initEntityMap();
/* Ignore characters until we get an beginning character. */
do
{
byte = lex->document[lex->document_position++];
if (byte == 0)
{
if (html_bracket_nestings)
bow_verbosify (bow_verbose,
"Found unterminated `<' in HTML\n");
lex->document_position--;
return 0;
}
if (byte == '&')
{
char *begEntity = &lex->document[lex->document_position];
char *s = strchr(begEntity, ';');
int hashCode;
if (s != NULL)
if ((s - begEntity) <= entityMaxLen) { /* can it be an entity */
*s = '\0';
if (begEntity[0] == '#') {
byte = atoi(&begEntity[1]);
lex->document_position++;
do
{
byte = lex->document[lex->document_position++];
} while (isdigit(byte));
}
else {
hashCode = bow_str2int_no_add(entityMap, begEntity);
if (hashCode != -1) {
byte = htmlEntities[hashCode].entityChar;
lex->document_position += s - begEntity + 1;
}
}
*s = ';';
}
}
if (byte == '<')
{
if (html_bracket_nestings)
bow_verbosify (bow_verbose,
"Found nested '<' in HTML\n");
html_bracket_nestings = 1;
}
else if (byte == '>')
{
if (html_bracket_nestings == 0)
bow_verbosify (bow_verbose,
"Found `>' outside HTML token\n");
html_bracket_nestings = 0;
}
}
while (html_bracket_nestings || !PARAMS->true_to_start (byte));
/* Add the first alphabetic character to the word. */
buf[0] = (bow_lexer_case_sensitive) ? byte : tolower (byte);
/* Add all the satisfying characters to the word - stripping out all HTML
markup. "<FONT SIZE=+2>R</FONT>ainbow " becomes "Rainbow" */
for (wordlen = 1; wordlen < buflen; wordlen++)
{
byte = lex->document[lex->document_position++];
if (byte == 0)
{
lex->document_position--;
break;
}
if (!PARAMS->false_to_end (byte) && html_bracket_nestings == 0)
{
lex->document_position--;
break;
}
if (byte == '<')
{
if (html_bracket_nestings)
bow_verbosify (bow_verbose,
"Found nested '<' in HTML\n");
html_bracket_nestings = 1;
}
if (byte == '>')
{
if (html_bracket_nestings == 0)
bow_verbosify (bow_verbose,
"Found `>' outside HTML token\n");
html_bracket_nestings = 0;
}
if (html_bracket_nestings == 0)
buf[wordlen] = tolower (byte);
}
assert (lex->document_position <= lex->document_length);
if (wordlen >= buflen)
bow_error ("Encountered word longer than buffer length=%d", buflen);
/* Terminate it. */
buf[wordlen] = '\0';
return wordlen;
}
/* A lexer that ignores all HTML directives, ignoring all characters
between angled brackets: < and >. */
const bow_lexer _bow_html_lexer =
{
sizeof (bow_lex),
NULL,
bow_lexer_simple_open_text_fp,
bow_lexer_simple_open_str,
bow_lexer_simple_get_word,
bow_lexer_html_get_raw_word,
bow_lexer_simple_postprocess_word,
bow_lexer_simple_close
};
const bow_lexer *bow_html_lexer = &_bow_html_lexer;
|