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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
|
/* $Id$
This is the Porter stemming algorithm, coded up in ANSI C by the
author. It may be be regarded as canonical, in that it follows the
algorithm presented in
Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
no. 3, pp 130-137,
only differing from it at the points maked --DEPARTURE-- below.
See also http://www.muscat.com/~martin/stem.html
The algorithm as described in the paper could be exactly replicated
by adjusting the points of DEPARTURE, but this is barely necessary,
because (a) the points of DEPARTURE are definitely improvements, and
(b) no encoding of the Porter stemmer I have seen is anything like
as exact as this version, even with the points of DEPARTURE!
You can compile it on Unix with 'gcc -O3 -o stem stem.c' after which
'stem' takes a list of inputs and sends the stemmed equivalent to
stdout.
The algorithm as encoded here is particularly fast.
Release 1
*/
#include <config.h>
#include <SWI-Prolog.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <wctype.h>
#include <wchar.h>
/* The main part of the stemming algorithm starts here. b is a buffer
holding a word to be stemmed. The letters are in b[k0], b[k0+1] ...
ending at b[k]. In fact k0 = 0 in this demo program. k is readjusted
downwards as the stemming progresses. Zero termination is not in fact
used in the algorithm.
Note that only lower case sequences are stemmed. Forcing to lower case
should be done before stem(...) is called.
*/
typedef struct vars
{ char *b; /* work to be stemmed */
int k, k0, j; /* j is a general offset into the string */
} vars;
/* cons(i) is TRUE <=> vs->b[i] is a consonant. */
static int
cons(int i, vars *vs)
{ switch (vs->b[i])
{ case 'a': case 'e': case 'i': case 'o': case 'u': return FALSE;
case 'y': return (i==vs->k0) ? TRUE : !cons(i-1, vs);
default: return TRUE;
}
}
/* m() measures the number of consonant sequences between vs->k0 and vs->j. if c is
a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
presence,
<c><v> gives 0
<c>vc<v> gives 1
<c>vcvc<v> gives 2
<c>vcvcvc<v> gives 3
....
*/
static int
m(vars *vs)
{ int n = 0;
int i = vs->k0;
while(TRUE)
{ if (i > vs->j) return n;
if (! cons(i, vs)) break;
i++;
}
i++;
while(TRUE)
{ while(TRUE)
{ if (i > vs->j) return n;
if (cons(i, vs)) break;
i++;
}
i++;
n++;
while(TRUE)
{ if (i > vs->j) return n;
if (! cons(i, vs)) break;
i++;
}
i++;
}
}
/* vowelinstem() is TRUE <=> vs->k0,...vs->j contains a vowel */
static int
vowelinstem(vars *vs)
{ int i; for (i = vs->k0; i <= vs->j; i++) if (! cons(i, vs)) return TRUE;
return FALSE;
}
/* doublec(vs->j) is TRUE <=> vs->j,(vs->j-1) contain a double consonant. */
static int
doublec(int j, vars *vs)
{ if (j < vs->k0+1) return FALSE;
if (vs->b[j] != vs->b[j-1]) return FALSE;
return cons(j, vs);
}
/* cvc(i) is TRUE <=> i-2,i-1,i has the form consonant - vowel - consonant
and also if the second c is not w,x or y. this is used when trying to
restore an e at the end of a short word. e.g.
cav(e), lov(e), hop(e), crim(e), but
snow, box, tray.
*/
static int
cvc(int i, vars *vs)
{ if (i < vs->k0+2 || !cons(i, vs) || cons(i-1, vs) || !cons(i-2, vs))
return FALSE;
{ int ch = vs->b[i];
if (ch == 'w' || ch == 'x' || ch == 'y') return FALSE;
}
return TRUE;
}
/* ends(s) is TRUE <=> vs->k0,...vs->k ends with the string s. */
static int
ends(char * s, vars *vs)
{ int length = s[0];
if (s[length] != vs->b[vs->k]) return FALSE; /* tiny speed-up */
if (length > vs->k-vs->k0+1) return FALSE;
if (memcmp(vs->b+vs->k-length+1,s+1,length) != 0) return FALSE;
vs->j = vs->k-length;
return TRUE;
}
/* setto(s) sets (vs->j+1),...vs->k to the characters in the string s, readjusting
vs->k. */
static void
setto(char * s, vars *vs)
{ int length = s[0];
memmove(vs->b+vs->j+1,s+1,length);
vs->k = vs->j+length;
}
/* r(s) is used further down. */
static void
r(char * s, vars *vs)
{ if (m(vs) > 0)
setto(s, vs);
}
/* step1ab() gets rid of plurals and -ed or -ing. e.g.
caresses -> caress
ponies -> poni
ties -> ti
caress -> caress
cats -> cat
feed -> feed
agreed -> agree
disabled -> disable
matting -> mat
mating -> mate
meeting -> meet
milling -> mill
messing -> mess
meetings -> meet
*/
static void
step1ab(vars *vs)
{ if (vs->b[vs->k] == 's')
{ if (ends("\04" "sses", vs)) vs->k -= 2; else
if (ends("\03" "ies", vs)) setto("\01" "i", vs); else
if (vs->b[vs->k-1] != 's') vs->k--;
}
if (ends("\03" "eed", vs)) { if (m(vs) > 0) vs->k--; } else
if ((ends("\02" "ed", vs) || ends("\03" "ing", vs)) && vowelinstem(vs))
{ vs->k = vs->j;
if (ends("\02" "at", vs)) setto("\03" "ate", vs); else
if (ends("\02" "bl", vs)) setto("\03" "ble", vs); else
if (ends("\02" "iz", vs)) setto("\03" "ize", vs); else
if (doublec(vs->k, vs))
{ vs->k--;
{ int ch = vs->b[vs->k];
if (ch == 'l' || ch == 's' || ch == 'z') vs->k++;
}
}
else if (m(vs) == 1 && cvc(vs->k, vs)) setto("\01" "e", vs);
}
}
/* step1c() turns terminal y to i when there is another vowel in the stem. */
static void
step1c(vars *vs)
{ if (ends("\01" "y", vs) && vowelinstem(vs))
vs->b[vs->k] = 'i';
}
/* step2() maps double suffices to single ones. so -ization ( = -ize plus
-ation) maps to -ize etc. note that the string before the suffix must give
m() > 0. */
static void step2(vars *vs)
{ if ( vs->k <= 0 )
return;
switch (vs->b[vs->k-1])
{
case 'a': if (ends("\07" "ational", vs)) { r("\03" "ate", vs); break; }
if (ends("\06" "tional", vs)) { r("\04" "tion", vs); break; }
break;
case 'c': if (ends("\04" "enci", vs)) { r("\04" "ence", vs); break; }
if (ends("\04" "anci", vs)) { r("\04" "ance", vs); break; }
break;
case 'e': if (ends("\04" "izer", vs)) { r("\03" "ize", vs); break; }
break;
case 'l': if (ends("\03" "bli", vs)) { r("\03" "ble", vs); break; } /*-DEPARTURE-*/
/* To match the published algorithm, replace this line with
case 'l': if (ends("\04" "abli", vs)) { r("\04" "able", vs); break; } */
if (ends("\04" "alli", vs)) { r("\02" "al", vs); break; }
if (ends("\05" "entli", vs)) { r("\03" "ent", vs); break; }
if (ends("\03" "eli", vs)) { r("\01" "e", vs); break; }
if (ends("\05" "ousli", vs)) { r("\03" "ous", vs); break; }
break;
case 'o': if (ends("\07" "ization", vs)) { r("\03" "ize", vs); break; }
if (ends("\05" "ation", vs)) { r("\03" "ate", vs); break; }
if (ends("\04" "ator", vs)) { r("\03" "ate", vs); break; }
break;
case 's': if (ends("\05" "alism", vs)) { r("\02" "al", vs); break; }
if (ends("\07" "iveness", vs)) { r("\03" "ive", vs); break; }
if (ends("\07" "fulness", vs)) { r("\03" "ful", vs); break; }
if (ends("\07" "ousness", vs)) { r("\03" "ous", vs); break; }
break;
case 't': if (ends("\05" "aliti", vs)) { r("\02" "al", vs); break; }
if (ends("\05" "iviti", vs)) { r("\03" "ive", vs); break; }
if (ends("\06" "biliti", vs)) { r("\03" "ble", vs); break; }
break;
case 'g': if (ends("\04" "logi", vs)) { r("\03" "log", vs); break; } /*-DEPARTURE-*/
/* To match the published algorithm, delete this line */
}
}
/* step3() deals with -ic-, -full, -ness etc. similar strategy to step2. */
static void
step3(vars *vs)
{ switch (vs->b[vs->k])
{
case 'e': if (ends("\05" "icate", vs)) { r("\02" "ic", vs); break; }
if (ends("\05" "ative", vs)) { r("\00" "", vs); break; }
if (ends("\05" "alize", vs)) { r("\02" "al", vs); break; }
break;
case 'i': if (ends("\05" "iciti", vs)) { r("\02" "ic", vs); break; }
break;
case 'l': if (ends("\04" "ical", vs)) { r("\02" "ic", vs); break; }
if (ends("\03" "ful", vs)) { r("\00" "", vs); break; }
break;
case 's': if (ends("\04" "ness", vs)) { r("\00" "", vs); break; }
break;
}
}
/* step4() takes off -ant, -ence etc., in context <c>vcvc<v>. */
static void step4(vars *vs)
{ if ( vs->k <= 0 )
return;
switch (vs->b[vs->k-1])
{ case 'a': if (ends("\02" "al", vs)) break; return;
case 'c': if (ends("\04" "ance", vs)) break;
if (ends("\04" "ence", vs)) break;
return;
case 'e': if (ends("\02" "er", vs)) break;
return;
case 'i': if (ends("\02" "ic", vs)) break;
return;
case 'l': if (ends("\04" "able", vs)) break;
if (ends("\04" "ible", vs)) break;
return;
case 'n': if (ends("\03" "ant", vs)) break;
if (ends("\05" "ement", vs)) break;
if (ends("\04" "ment", vs)) break;
if (ends("\03" "ent", vs)) break;
return;
case 'o': if (ends("\03" "ion", vs) && vs->j > 0 &&
(vs->b[vs->j] == 's' || vs->b[vs->j] == 't'))
break;
if (ends("\02" "ou", vs)) break;
return;
/* takes care of -ous */
case 's': if (ends("\03" "ism", vs)) break;
return;
case 't': if (ends("\03" "ate", vs)) break;
if (ends("\03" "iti", vs)) break;
return;
case 'u': if (ends("\03" "ous", vs)) break;
return;
case 'v': if (ends("\03" "ive", vs)) break;
return;
case 'z': if (ends("\03" "ize", vs)) break;
return;
default: return;
}
if (m(vs) > 1) vs->k = vs->j;
}
/* step5() removes a final -e if m() > 1, and changes -ll to -l if
m() > 1. */
static void step5(vars *vs)
{ vs->j = vs->k;
if (vs->b[vs->k] == 'e')
{ int a = m(vs);
if (a > 1 || (a == 1 && !cvc(vs->k-1, vs))) vs->k--;
}
if (vs->b[vs->k] == 'l' && doublec(vs->k, vs) && m(vs) > 1) vs->k--;
}
/* In stem(p,i,vs->j), p is a char pointer, and the string to be stemmed is from
p[i] to p[vs->j] inclusive. Typically i is zero and vs->j is the offset to the last
character of a string, (p[vs->j+1] == '\0'). The stemmer adjusts the
characters p[i] ... p[vs->j] and returns the new end-point of the string, vs->k.
Stemming never increases word length, so i <= vs->k <= vs->j. To turn the stemmer
into a module, declare 'stem' as extern, and delete the remainder of this
file.
*/
static int
stem(char * p, int i, int j)
{ vars vs;
vs.b = p; vs.k = j; vs.k0 = i;
if ( vs.k <= vs.k0+1) return vs.k; /*-DEPARTURE-*/
/* With this line, strings of length 1 or 2 don't go through the
stemming process, although no mention is made of this in the
published algorithm. Remove the line to match the published
algorithm. */
step1ab(&vs); step1c(&vs); step2(&vs); step3(&vs); step4(&vs); step5(&vs);
return vs.k;
}
/*--------------------stemmer definition ends here------------------------*/
/* SWI-Prolog hooks */
static int unaccent(const char *in, size_t len, char *out, size_t size);
static foreign_t
pl_stem(term_t t_in, term_t t_stem)
{ char *word;
size_t len, end;
char *f, *t, *s, *ew;
char buf[1024];
char plain[1024];
long l;
int rc;
if ( !PL_get_nchars(t_in, &len, &word, CVT_ALL|CVT_EXCEPTION) )
{ if ( PL_is_number(t_in) )
return PL_unify(t_in, t_stem);
return FALSE;
}
ew = &word[len];
s = len+1 > sizeof(buf) ? PL_malloc(len+1) : buf;
for(f=word, t=s; f<ew; )
*t++ = tolower(*f++);
if ( (l=unaccent(s, t-s, plain, sizeof(plain))) < (long)sizeof(plain) )
{ if ( l >= 0 )
{ if ( s != buf )
PL_free(s);
s = plain;
}
} else
{ char *s2 = PL_malloc(l+1);
unaccent(s, t-s, s2, l+1);
if ( s != buf )
PL_free(s);
s = s2;
}
end = stem(s, 0, (int)(len - 1));
s[end + 1] = '\0';
rc = PL_unify_atom_chars(t_stem, s);
if ( s != plain && s != buf )
PL_free(s);
return rc;
}
/*******************************
* ACCENTS *
*******************************/
/* See also http://www.ahinea.com/en/tech/accented-translate.html
*/
static char *unaccent_def[] =
{ "A", /* 192 */ "A", /* 193 */ "A", /* 194 */ "A", /* 195 */
"A", /* 196 */ "A", /* 197 */ "AE", /* 198 */ "C", /* 199 */
"E", /* 200 */ "E", /* 201 */ "E", /* 202 */ "E", /* 203 */
"I", /* 204 */ "I", /* 205 */ "I", /* 206 */ "I", /* 207 */
"D", /* 208 */ "N", /* 209 */ "O", /* 210 */ "O", /* 211 */
"O", /* 212 */ "O", /* 213 */ "O", /* 214 */ NULL, /* 215 */
NULL, /* 216 */ "U", /* 217 */ "U", /* 218 */ "U", /* 219 */
"U", /* 220 */ "Y", /* 221 */ NULL, /* 222 */ "ss", /* 223 */
"a", /* 224 */ "a", /* 225 */ "a", /* 226 */ "a", /* 227 */
"a", /* 228 */ "a", /* 229 */ "ae", /* 230 */ "c", /* 231 */
"e", /* 232 */ "e", /* 233 */ "e", /* 234 */ "e", /* 235 */
"i", /* 236 */ "i", /* 237 */ "i", /* 238 */ "i", /* 239 */
"d", /* 240 */ "n", /* 241 */ "o", /* 242 */ "o", /* 243 */
"o", /* 244 */ "o", /* 245 */ "o", /* 246 */ NULL, /* 247 */
NULL, /* 248 */ "u", /* 249 */ "u", /* 250 */ "u", /* 251 */
"u", /* 252 */ "y", /* 253 */ NULL, /* 254 */ "y", /* 255 */
};
static int
unaccent(const char *in, size_t len, char *out, size_t size)
{ char *to = out, *toe = &out[size];
const char *ein = &in[len];
int changes = 0;
for( ; in < ein; in++)
{ int c = (*in)&0xff;
char *m;
if ( c < 192 || !(m=unaccent_def[c-192]) )
{ if ( to < toe )
*to = c;
to++;
} else
{ changes++;
while(*m)
{ if ( to < toe )
*to = *m;
to++;
m++;
}
}
}
if ( to < toe )
*to = '\0';
if ( changes == 0 )
return (int)(out-to); /* no change: negative */
return (int)(to-out);
}
static foreign_t
pl_unaccent(term_t from, term_t to)
{ char buf[1024];
char *f;
int len;
size_t fl;
if ( !PL_get_nchars(from, &fl, &f, CVT_ALL|CVT_EXCEPTION) )
return FALSE;
if ( (len=unaccent(f, fl, buf, sizeof(buf))) <= (int)sizeof(buf) )
{ if ( len < 0 ) /* no change */
return PL_unify(to, from);
else
return PL_unify_atom_nchars(to, len, buf);
} else
{ char *t = PL_malloc(len+1);
int rc;
unaccent(f, fl, t, len+1);
rc = PL_unify_atom_nchars(to, len, t);
PL_free(t);
return rc;
}
}
/*******************************
* TOKENISE *
*******************************/
typedef enum
{ TOK_INT,
TOK_FLOAT,
TOK_WORD,
TOK_PUNCT,
TOK_UNKNOWN
} toktype;
#undef isdigit
#define issign(c) ((c) == '-' || (c) == '+' )
#define isdigit(c) ((c) >= '0' && (c) <= '9')
static int
tokenizeA(const char *in, size_t len,
int (*call)(const char *s,
size_t len,
toktype type,
void *closure),
void *closure)
{ const unsigned char *s = (const unsigned char*)in;
const unsigned char *se = &s[len];
toktype type;
while(s<se)
{ const unsigned char *st; /* start token */
while(s<se && iswspace(*s)) /* skip blanks */
s++;
if ( s >= se )
break;
st = s;
type = TOK_UNKNOWN;
if ( *s == '-' && se-s > 1 && isdigit(s[1]) )
{ s += 2;
type = TOK_INT;
} else if ( isdigit(*s) )
{ s++;
type = TOK_INT;
}
if ( type == TOK_INT )
{ while(s<se && isdigit(*s))
s++;
if ( s+2 <= se && *s == '.' && isdigit(s[1]) )
{ s += 2;
type = TOK_FLOAT;
while(s<se && isdigit(*s))
s++;
}
if ( s+2 <= se &&
(*s == 'e' || *s == 'E') &&
(isdigit(s[1]) || (s+3 <= se && issign(s[1]) && isdigit(s[2]))) )
{ s += 2;
type = TOK_FLOAT;
while(s<se && isdigit(*s))
s++;
}
if ( !(*call)((const char*)st, s-st, type, closure) )
{ if ( PL_exception(0) )
return FALSE;
while(s<se && iswalnum(*s))
s++;
if ( !(*call)((const char*)st, s-st, TOK_WORD, closure) )
return FALSE;
}
} else if ( iswalnum(*s) )
{ while(s<se && iswalnum(*s))
s++;
if ( !(*call)((const char*)st, s-st, TOK_WORD, closure) )
return FALSE;
} else
{ s++;
if ( !(*call)((const char*)st, 1, TOK_PUNCT, closure) )
return FALSE;
}
}
return TRUE;
}
static int
tokenizeW(const wchar_t *in, size_t len,
int (*call)(const wchar_t *s,
size_t len,
toktype type,
void *closure),
void *closure)
{ const wchar_t *s = (const wchar_t*)in;
const wchar_t *se = &s[len];
toktype type;
while(s<se)
{ const wchar_t *st; /* start token */
while(s<se && iswspace(*s)) /* skip blanks */
s++;
if ( s >= se )
break;
st = s;
type = TOK_UNKNOWN;
if ( *s == '-' && se-s > 1 && isdigit(s[1]) )
{ s += 2;
type = TOK_INT;
} else if ( isdigit(*s) )
{ s++;
type = TOK_INT;
}
if ( type == TOK_INT )
{ while(s<se && isdigit(*s))
s++;
if ( s+2 <= se && *s == '.' && isdigit(s[1]) )
{ s += 2;
type = TOK_FLOAT;
while(s<se && isdigit(*s))
s++;
}
if ( s+2 <= se &&
(*s == 'e' || *s == 'E') &&
(isdigit(s[1]) || (s+3 <= se && issign(s[1]) && isdigit(s[2]))) )
{ s += 2;
type = TOK_FLOAT;
while(s<se && isdigit(*s))
s++;
}
if ( !(*call)((const wchar_t*)st, s-st, type, closure) )
{ if ( PL_exception(0) )
return FALSE;
while(s<se && iswalnum(*s))
s++;
if ( !(*call)((const wchar_t*)st, s-st, TOK_WORD, closure) )
return FALSE;
}
} else if ( iswalnum(*s) )
{ while(s<se && iswalnum(*s))
s++;
if ( !(*call)((const wchar_t*)st, s-st, TOK_WORD, closure) )
return FALSE;
} else
{ s++;
if ( !(*call)((const wchar_t*)st, 1, TOK_PUNCT, closure) )
return FALSE;
}
}
return TRUE;
}
typedef struct
{ term_t head;
term_t tail;
term_t tmp;
} list;
static int
unify_tokenA(const char *s, size_t len, toktype type, void *closure)
{ list *l = closure;
switch(type)
{ case TOK_INT:
case TOK_FLOAT:
{ char buf[100];
char *a, *o;
const char *i;
int rc;
if ( len+1 > sizeof(buf) )
{ if ( !(a = malloc(len+1)) )
return PL_resource_error("memory");
} else
{ a = buf;
}
for(i=s,o=a; len-- > 0; )
*o++ = (char)*i++;
*o = '\0';
rc = PL_chars_to_term(a, l->tmp);
if ( a != buf )
free(a);
if ( !rc )
return rc;
break;
}
default:
if ( !PL_put_atom_nchars(l->tmp, len, s) )
return FALSE;
break;
}
return ( PL_unify_list(l->tail, l->head, l->tail) &&
PL_unify(l->head, l->tmp) );
}
static int
unify_tokenW(const wchar_t *s, size_t len, toktype type, void *closure)
{ list *l = closure;
switch(type)
{ case TOK_INT:
case TOK_FLOAT:
{ char buf[100];
char *a, *o;
const wchar_t *i;
int rc;
if ( len+1 > sizeof(buf) )
{ if ( !(a = malloc(len+1)) )
return PL_resource_error("memory");
} else
{ a = buf;
}
for(i=s,o=a; len-- > 0; )
*o++ = (char)*i++;
*o = '\0';
rc = PL_chars_to_term(a, l->tmp);
if ( a != buf )
free(a);
if ( !rc )
return rc;
break;
}
default:
if ( !PL_put_variable(l->tmp) ||
!PL_unify_wchars(l->tmp, PL_ATOM, len, s) )
return FALSE;
break;
}
return ( PL_unify_list(l->tail, l->head, l->tail) &&
PL_unify(l->head, l->tmp) );
}
static foreign_t
pl_tokenize(term_t text, term_t tokens)
{ char *s;
wchar_t *ws;
size_t len;
list l;
l.tail = PL_copy_term_ref(tokens);
l.head = PL_new_term_ref();
l.tmp = PL_new_term_ref();
if ( PL_get_nchars(text, &len, &s, CVT_ALL) )
{ if ( !tokenizeA(s, len, unify_tokenA, &l) )
return FALSE;
} else if ( PL_get_wchars(text, &len, &ws, CVT_ALL|CVT_EXCEPTION) )
{ if ( !tokenizeW(ws, len, unify_tokenW, &l) )
return FALSE;
} else
return FALSE;
return PL_unify_nil(l.tail);
}
static int
unify_stem(const char *s, size_t len, toktype type, void *closure)
{ list *list = closure;
if ( type == TOK_PUNCT )
return TRUE;
if ( type == TOK_INT || type == TOK_FLOAT )
return unify_tokenA(s, len, type, closure);
if ( PL_unify_list(list->tail, list->head, list->tail) )
{ char tmp[1024];
char *buf = tmp;
char *q;
int i, rc, end, l;
/* unaccent */
l = abs(unaccent(s, len, buf, sizeof(tmp)));
if ( l > (int)sizeof(buf) )
{ buf = PL_malloc(l+1);
unaccent(s, len, buf, l+1);
}
/* downcase */
for(q=buf, i=0; i++ < l; q++)
*q = tolower(*q);
end = stem(buf, 0, l-1);
buf[++end] = '\0';
rc = PL_unify_atom_nchars(list->head, end, buf);
if ( buf != tmp )
PL_free(buf);
return rc;
}
return FALSE;
}
static foreign_t
pl_atom_to_stem_list(term_t text, term_t stems)
{ char *s;
size_t len;
list l;
if ( !PL_get_nchars(text, &len, &s, CVT_ALL) )
return FALSE;
l.tail = PL_copy_term_ref(stems);
l.head = PL_new_term_ref();
l.tmp = PL_new_term_ref();
if ( !tokenizeA(s, len, unify_stem, &l) )
return FALSE;
return PL_unify_nil(l.tail);
}
/*******************************
* INSTALL *
*******************************/
install_t
install_porter_stem()
{ PL_register_foreign("porter_stem", 2, pl_stem, 0);
PL_register_foreign("unaccent_atom", 2, pl_unaccent, 0);
PL_register_foreign("tokenize_atom", 2, pl_tokenize, 0);
PL_register_foreign("atom_to_stem_list", 2, pl_atom_to_stem_list, 0);
}
/* end of SWI-Prolog */
|