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 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
|
// crm_expr_clump.c - automatically cluster unlabelled documents
// Copyright 2009 William S. Yerazunis.
// This file is under GPLv3, as described in COPYING.
/////////////////////////////////////////////////////////////////////
// Original spec by Bill Yerazunis, original code by Joe Langeway,
// recode for CRM114 use by Bill Yerazunis.
//
// This code section (crm_expr_clump and subsidiary routines) is
// dual-licensed to both William S. Yerazunis and Joe Langeway,
// including the right to reuse this code in any way desired,
// including the right to relicense it under any other terms as
// desired.
/////////////////////////////////////////////////////////////////////
// This file is part of on going research and should not be considered
// a finished product, a reliable tool, an example of good software
// engineering, or a reflection of any quality of Joe's besides his
// tendency towards long hours.
//
// Here's what's going on:
//
// Documents are fed in with calls to "clump" and the distance between each
// document is recorded in a matrix. We then find clusters for automatic
// classification without the need for a gold standard judgement ahead
// of time.
//
// Cluster assignments start at index 1 and negative numbers indicate
// permanent assignments made by crm.
// include some standard files
#include "crm114_sysincludes.h"
// include any local crm114 configuration file
#include "crm114_config.h"
// include the crm114 data structures file
#include "crm114_structs.h"
// and include the routine declarations file
#include "crm114.h"
// the globals used when we need a big buffer - allocated once, used
// wherever needed. These are sized to the same size as the data window.
// Do not mutate/realloc these.
extern char *outbuf;
#define MAX_CLUSTERS 4096
#define CLUSTER_LABEL_LEN 32
#define DOCUMENT_TAG_LEN 32
typedef struct mythical_clumper_header
{
long max_documents, n_documents;
long document_offsets_offset;//list of offsets to documents
long clusters_offset; //cluster assignments of documents
long distance_matrix_offset;
long cluster_labels_offset;
long document_tags_offset;
long n_perma_clusters;
long file_length; //is the offset of new files when learning
long last_action; //0 = made clumps, non-zero means make clumps if you've got
// a chance and we're told not to
long n_clusters;
} CLUMPER_HEADER_STRUCT;
typedef struct mythical_clumper_state
{
char *file_origin;
CLUMPER_HEADER_STRUCT *header;
long *document_offsets;
long *cluster_assignments;
float *distance_matrix;
char (*cluster_labels)[CLUSTER_LABEL_LEN];
char (*document_tags)[DOCUMENT_TAG_LEN];
} CLUMPER_STATE_STRUCT;
// tracing for this module
int joe_trace = 0;
long max_documents = 1000;
static void make_new_clumper_backing_file(char *filename)
{
CLUMPER_HEADER_STRUCT H, *h = &H;
FILE *f;
long i;
h->max_documents = max_documents;
h->n_documents = 0;
h->document_offsets_offset = sizeof(CLUMPER_HEADER_STRUCT);
h->clusters_offset = h->document_offsets_offset +
sizeof(long) * max_documents;
h->distance_matrix_offset = h->clusters_offset +
sizeof(long) * max_documents;
h->cluster_labels_offset = h->distance_matrix_offset + ( sizeof(float) *
max_documents * (max_documents + 1) / 2);
h->document_tags_offset = h->cluster_labels_offset +
( sizeof(char) * max_documents * CLUSTER_LABEL_LEN);
h->file_length = h->document_tags_offset +
( sizeof(char) * max_documents * DOCUMENT_TAG_LEN);
h->n_perma_clusters = 0;
h->n_clusters = 0;
crm_force_munmap_filename(filename);
f = fopen(filename, "wb");
dontcare = fwrite(h, 1, sizeof(CLUMPER_HEADER_STRUCT), f);
i = h->file_length - sizeof(CLUMPER_HEADER_STRUCT);
if(joe_trace)
fprintf(stderr, "about to write %ld zeros to backing file\n", i);
while(i--)
fputc('\0', f);
fflush(f);
fclose(f);
if(joe_trace)
fprintf(stderr, "Just wrote backing file for clumper size %ld\n",
h->file_length);
}
static int map_file(CLUMPER_STATE_STRUCT *s, char *filename)
{
struct stat statee;
if(stat(filename, &statee))
{
nonfatalerror("Couldn't stat file!", filename);
return -1;
}
s->file_origin = crm_mmap_file
(filename,
0, statee.st_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
NULL);
if(s->file_origin == MAP_FAILED)
{
nonfatalerror("Couldn't mmap file!", filename);
return -1;
}
if(joe_trace)
fprintf(stderr,"Definitely think I've mapped a file.\n");
s->header = (CLUMPER_HEADER_STRUCT *)(s->file_origin);
s->document_offsets =
(void *)( s->file_origin + s->header->document_offsets_offset );
s->cluster_assignments =
(void *)( s->file_origin + s->header->clusters_offset );
s->distance_matrix =
(void *)( s->file_origin + s->header->distance_matrix_offset );
s->cluster_labels =
(void *)( s->file_origin + s->header->cluster_labels_offset );
s->document_tags =
(void *)( s->file_origin + s->header->document_tags_offset );
return 0;
}
static void unmap_file(CLUMPER_STATE_STRUCT *s)
{
crm_munmap_file ((void *) s->file_origin);
}
static float *aref_dist_mat(float *m, int j, int i)
{
if(i < j) {int t = i; i = j; j = t;}
return m + i * (i - 1) / 2 + j;
}
static float get_document_affinity(unsigned int *doc1, unsigned int *doc2)
{
int u = 0, l1 = 0, l2 = 0;
for(;;)
if(doc1[l1] == 0)
{
while(doc2[l2] != 0) l2++;
break;
}
else if(doc2[l2] == 0)
{
while(doc1[l1] != 0) l1++;
break;
}
else if(doc1[l1] == doc2[l2])
{
u++; l1++; l2++;
}
else if(doc1[l1] < doc2[l2])
l1++;
else if(doc1[l1] > doc2[l2])
l2++;
else
{
fprintf(stderr, "panic in the disco! ");
break;
}
if(joe_trace)
fprintf(stderr, "Just compared two documents u=%d l1=%d l2=%d\n",
u, l1, l2);
return pow((double)(1.0 + u * u) / (double)(1.0 + l1 * l2), 0.2);
}
static int compare_features(const void *a, const void *b)
{
if(*(unsigned int *)a < *(unsigned int *)b)
return -1;
if(*(unsigned int *)a > *(unsigned int *)b)
return 1;
return 0;
}
static int eat_document
( char *text, long text_len, long *ate,
regex_t *regee,
unsigned int *feature_space, long max_features,
long long flags)
{
long n_features = 0, i, j;
unsigned int hash_pipe[OSB_BAYES_WINDOW_LEN];
int hash_coefs[] = { 1, 3, 5, 11, 23, 47};
regmatch_t match[1];
char *t_start;
long t_len;
long f;
unsigned long long unigram, unique, string;
unigram = flags & CRM_UNIGRAM;
string = flags & CRM_STRING;
unique = flags & (CRM_UNIQUE | CRM_STRING);
*ate = 0;
for(i = 0; i < OSB_BAYES_WINDOW_LEN; i++)
hash_pipe[i] = 0xdeadbeef;
while(text_len > 0 && n_features < max_features - 1)
{
if(crm_regexec (regee, text, text_len, 1, match, 0, NULL))
//no match or regex error, we're done
break;
else
{
t_start = text + match->rm_so;
t_len = match->rm_eo - match->rm_so;
if(string)
{
text += match->rm_so + 1;
text_len -= match->rm_so + 1;
*ate += match->rm_so + 1;
}else
{
text += match->rm_eo;
text_len -= match->rm_eo;
*ate += match->rm_eo;
}
for(i = OSB_BAYES_WINDOW_LEN - 1; i > 0; i--)
hash_pipe[i] = hash_pipe[i - 1];
hash_pipe[0] = strnhash(t_start, t_len);
}
f = 0;
if(unigram)
feature_space[n_features++] = hash_pipe[0];
else
for(i = 1; i < OSB_BAYES_WINDOW_LEN && hash_pipe[i] != 0xdeadbeef; i++)
feature_space[n_features++] =
hash_pipe[0] + hash_pipe[i] * hash_coefs[i];
}
qsort(feature_space, n_features, sizeof(unsigned int), compare_features);
if(unique)
{
i = 0;
for(j = 1; j < n_features; j++)
if(feature_space[i] != feature_space[j])
feature_space[++i] = feature_space[j];
feature_space[++i] = 0;
n_features = i + 1; //the zero counts
} else
feature_space[n_features++] = 0;
return n_features;
}
static long find_closest_document
(CLUMPER_STATE_STRUCT *s,
char *text, long text_len,
regex_t *regee,
long long flags)
{
unsigned int feature_space[32768];
long n, i, b = -1;
float b_s = 0.0, n_s;
n = eat_document(text, text_len, &i,
regee, feature_space, 32768,
flags);
for(i = 0; i < s->header->n_documents; i++)
{
n_s = get_document_affinity
(feature_space, (unsigned int *)(s->file_origin + s->document_offsets[i]));
if(n_s > b_s)
{
b = i;
b_s = n_s;
}
}
return b;
}
typedef struct mythical_cluster_head
{
struct mythical_cluster_head *head, *next_head, *prev_head, *next;
long count;
} CLUSTER_HEAD_STRUCT;
static void join_clusters(CLUSTER_HEAD_STRUCT *a, CLUSTER_HEAD_STRUCT *b)
{
if(joe_trace)
fprintf(stderr, "Joining clusters of sizes %ld and %ld\n,",
a->head->count, b->head->count);
while(a->next) a = a->next;
b = b->head;
a->next = b;
a->head->count += b->count;
b->count = 0; //though we wont actually touch this value anymore
if(b->prev_head)
b->prev_head->next_head = b->next_head;
if(b->next_head)
b->next_head->prev_head = b->prev_head;
b->next_head = NULL;
b->prev_head = NULL;
do
b->head = a->head;
while( (b = b->next) );
}
static void agglomerative_averaging_cluster(CLUMPER_STATE_STRUCT *s, long goal)
{
long i, j, k, l, n = s->header->n_documents;
CLUSTER_HEAD_STRUCT *clusters = malloc(n * sizeof(CLUSTER_HEAD_STRUCT)),
*a, *b, *c, first_head_ptr;
float *M = malloc( (n * (n + 1) / 2 - 1) * sizeof(float)), d, e;
long ck, cl, ckl;
if(joe_trace)
fprintf(stderr, "agglomerative averaging clustering...\n");
for(i = 1; i < s->header->n_documents - 1; i++)
{
clusters[i].head = &clusters[i];
clusters[i].prev_head = &clusters[i - 1];
clusters[i].next_head = &clusters[i + 1];
clusters[i].next = NULL;
clusters[i].count = 1;
}
clusters[0].head = &clusters[0];
clusters[0].prev_head = &first_head_ptr;
clusters[0].next_head = &clusters[1];
clusters[0].next = NULL;
clusters[0].count = 1;
if(s->header->n_documents > 1) //don't muck the first one!
{
clusters[s->header->n_documents-1].head =
&clusters[s->header->n_documents - 1];
clusters[s->header->n_documents - 1].prev_head =
&clusters[s->header->n_documents - 2];
clusters[s->header->n_documents - 1].next = NULL;
clusters[s->header->n_documents - 1].count = 1;
}
//always make sure the chain ends
clusters[s->header->n_documents - 1].next_head = NULL;
first_head_ptr.next_head = &clusters[0];
j = (n * (n + 1) / 2 - 1);
for(i = 0; i < j; i++)
M[i] = s->distance_matrix[i];
for(a = first_head_ptr.next_head; a; a = a->next_head)
if(s->cluster_assignments[a - clusters] < 0)
for(b = a->next_head; b; b = b->next_head)
if(s->cluster_assignments[a - clusters]
== s->cluster_assignments[b - clusters])
{
k = a - clusters;
l = b - clusters;
ck = clusters[k].count;
cl = clusters[l].count;
ckl = ck + cl;
for(c = &clusters[0]; c; c = c->next_head)
{
i = c - clusters;
if(i == k || i == l)
continue;
*aref_dist_mat(M, k, i) = (ck * *aref_dist_mat(M, k, i) +
cl * *aref_dist_mat(M, l, i) ) / ckl;
*aref_dist_mat(M, l, i) = 0.0;
}
join_clusters(&clusters[k], &clusters[l]);
n--;
}
while(n > goal)
{
l = 0; k = 0;
d = -1.0;
for(a = first_head_ptr.next_head; a; a = a->next_head)
for(b = a->next_head; b; b = b->next_head)
{
i = a - clusters;
j = b - clusters;
if(s->cluster_assignments[i] < 0 && s->cluster_assignments[j] < 0)
e = *aref_dist_mat(M, i, j) = -1000000000.0;
else
e = *aref_dist_mat(M, i, j);
if( e > d )
{
if(s->cluster_assignments[j] < 0)
{
k = j;
l = i;
} else
{
k = i;
l = j;
}
d = e;
}
}
if(l == 0 && k == 0)
{
fprintf(stderr, "CLUMP FAILED TO JOIN ENOUGH CLUMPS!\n");
break;
}
ck = clusters[k].count;
cl = clusters[l].count;
ckl = ck + cl;
for(a = &clusters[0]; a; a = a->next_head)
{
i = a - clusters;
if(i == k || i == l)
continue;
*aref_dist_mat(M, k, i) = (ck * *aref_dist_mat(M, k, i) +
cl * *aref_dist_mat(M, l, i) ) / ckl;
*aref_dist_mat(M, l, i) = 0.0;
}
join_clusters(&clusters[k], &clusters[l]);
n--;
}
i = s->header->n_perma_clusters + 1;
for(a = &clusters[0]; a; a = a->next_head)
{
if(s->cluster_assignments[a - clusters] < 0)
j = -s->cluster_assignments[a - clusters];
else
j = i++;
for(b = a; b; b = b->next)
s->cluster_assignments[b - clusters] = j;
}
s->header->n_clusters = n;
free(M);
}
static void index_to_pair(long t, long *i, long *j)
{
long p = 2 * t;
*i = (long)sqrt(p);
if(*i * *i + *i > p)
(*i)--;
*j = t - (*i * (*i + 1) / 2);
}
static int compare_float_ptrs(const void *a, const void *b)
{
if(**(float **)a > **(float **)b)
return 1;
if(**(float **)a < **(float **)b)
return -1;
return 0;
}
static void agglomerative_nearest_cluster(CLUMPER_STATE_STRUCT *s, long goal)
{
long i, j, k, l, m, n = s->header->n_documents;
CLUSTER_HEAD_STRUCT *clusters = malloc(n * sizeof(CLUSTER_HEAD_STRUCT)),
*a, *b, first_head_ptr;
float **M = malloc( (n * (n + 1) / 2 - 1) * sizeof(float *));
if(joe_trace)
fprintf(stderr, "agglomerative nearest clustering...\n");
for(i = 1; i < s->header->n_documents - 1; i++)
{
clusters[i].head = &clusters[i];
clusters[i].prev_head = &clusters[i - 1];
clusters[i].next_head = &clusters[i + 1];
clusters[i].next = NULL;
clusters[i].count = 1;
}
clusters[0].head = &clusters[0];
clusters[0].prev_head = &first_head_ptr;
clusters[0].next_head = &clusters[1];
clusters[0].next = NULL;
clusters[0].count = 1;
if(s->header->n_documents > 1) //don't muck the first one!
{
clusters[s->header->n_documents - 1].head =
&clusters[s->header->n_documents - 1];
clusters[s->header->n_documents - 1].prev_head =
&clusters[s->header->n_documents - 2];
clusters[s->header->n_documents - 1].next = NULL;
clusters[s->header->n_documents - 1].count = 1;
}
//always make sure the chain ends
clusters[s->header->n_documents - 1].next_head = NULL;
first_head_ptr.next_head = &clusters[0];
j = (n * (n + 1) / 2 - 1);
for(i = 0; i < j; i++)
M[i] = &s->distance_matrix[i];
qsort(M, j, sizeof(float *), compare_float_ptrs);
for(a = first_head_ptr.next_head; a; a = a->next_head)
if(s->cluster_assignments[a - clusters] < 0)
for(b = a->next_head; b; b = b->next_head)
if(s->cluster_assignments[a - clusters]
== s->cluster_assignments[b - clusters])
{
k = a - clusters;
l = b - clusters;
join_clusters(&clusters[k], &clusters[l]);
n--;
}
i = j;
while(n > goal)
{
do
{
k = M[--i] - s->distance_matrix;
index_to_pair(k, &l, &m);
} while(clusters[m].head == clusters[l].head);
join_clusters(&clusters[m], &clusters[l]);
n--;
}
i = s->header->n_perma_clusters + 1;
for(a = &clusters[0]; a; a = a->next_head)
{
if(s->cluster_assignments[a - clusters] < 0)
j = -s->cluster_assignments[a - clusters];
else
j = i++;
for(b = a; b; b = b->next)
s->cluster_assignments[b - clusters] = j;
}
free(M);
s->header->n_clusters = n;
}
double square(double a) {return a * a;}
double minf(double a, double b) {return a < b ? a : b;}
#define H_BUCKETS 50
static void thresholding_average_cluster(CLUMPER_STATE_STRUCT *s)
{
long i, j, k, l, ck, cl, ckl, n = s->header->n_documents;
CLUSTER_HEAD_STRUCT *clusters = malloc(n * sizeof(CLUSTER_HEAD_STRUCT)),
*a, *b, *c, first_head_ptr;
long H[H_BUCKETS], C[H_BUCKETS];
float A[H_BUCKETS], t_A, t, t_score, scoro,gM;
float min, max, scale;
float *M = malloc( (n * (n + 1) / 2 - 1) * sizeof(float)), d, e;
if(joe_trace)
fprintf(stderr, "threshold average clustering...\n");
for(i = 1; i < s->header->n_documents - 1; i++)
{
clusters[i].head = &clusters[i];
clusters[i].prev_head = &clusters[i - 1];
clusters[i].next_head = &clusters[i + 1];
clusters[i].next = NULL;
clusters[i].count = 1;
}
clusters[0].head = &clusters[0];
clusters[0].prev_head = &first_head_ptr;
clusters[0].next_head = &clusters[1];
clusters[0].next = NULL;
clusters[0].count = 1;
if(s->header->n_documents > 1) //don't muck the first one!
{
clusters[s->header->n_documents-1].head =
&clusters[s->header->n_documents - 1];
clusters[s->header->n_documents - 1].prev_head =
&clusters[s->header->n_documents - 2];
clusters[s->header->n_documents - 1].next = NULL;
clusters[s->header->n_documents - 1].count = 1;
}
//always make sure the chain ends
clusters[s->header->n_documents - 1].next_head = NULL;
first_head_ptr.next_head = &clusters[0];
j = (n * (n + 1) / 2 - 1);
for(i = 0; i < j; i++)
M[i] = s->distance_matrix[i];
for(i = 0; i < H_BUCKETS; i++)
H[i] = 0.0;
j = n * (n - 1) / 2;
min = 1000000000.0;
max = -1000000000.0;
for(i = 0; i < j; i++)
{
if(M[i] < min)
min = s->distance_matrix[i];
if(M[i] > max)
max = s->distance_matrix[i];
}
scale = (max - min) / ((float)H_BUCKETS - 0.1);
if (scale == 0)
scale = 1.0;
for(i = 0; i < j; i++)
H[ (int)( (M[i] - min) / scale ) ]++;
if(joe_trace)
{
fprintf(stderr, "Histogram of document distances:\n");
for(i = 0; i < H_BUCKETS; i++)
{
for(k = 0; k < H[i]; k += 100)
fputc('*', stderr);
fputc('\n', stderr);
}
}
k = 0;
t_A = 0.0;
for(i = 0; i < H_BUCKETS; i++)
{
k = C[i] = H[i] + k;
t_A = A[i] = H[i] * (min + (i + 0.5) * scale) + t_A;
}
gM = t_A / (float)j;
t_score = 0.0;
t = -1.0;
for(i = 2; i < H_BUCKETS - 2; i++)
{
scoro = square(gM - (t_A - A[i]) / (k - C[i])) * (k - C[i])
+ square(gM - A[i] / C[i]) * C[i];
if(scoro > t_score)
{
t_score = scoro;
t = min + scale * (float)(i );
}
}
if(joe_trace)
fprintf(stderr, "min = %f, max = %f, t = %f\n", min, max, t);
for(a = first_head_ptr.next_head; a; a = a->next_head)
if(s->cluster_assignments[a - clusters] < 0)
for(b = a->next_head; b; b = b->next_head)
if(s->cluster_assignments[a - clusters]
== s->cluster_assignments[b - clusters])
{
k = a - clusters;
l = b - clusters;
ck = clusters[k].count;
cl = clusters[l].count;
ckl = ck + cl;
for(c = &clusters[0]; c; c = c->next_head)
{
i = c - clusters;
if(i == k || i == l)
continue;
//*aref_dist_mat(M, k, i) = (ck * *aref_dist_mat(M, k, i) +
// cl * *aref_dist_mat(M, l, i) ) / ckl;
*aref_dist_mat(M, k, i) =
minf(*aref_dist_mat(M, k, i), *aref_dist_mat(M, l, i));
*aref_dist_mat(M, l, i) = 0.0;
}
join_clusters(&clusters[k], &clusters[l]);
n--;
}
for(;;)
{
l = 0; k = 0;
d = -1.0;
for(a = first_head_ptr.next_head; a; a = a->next_head)
for(b = a->next_head; b; b = b->next_head)
{
i = a - clusters;
j = b - clusters;
if(s->cluster_assignments[i] < 0 && s->cluster_assignments[j] < 0)
{
e = *aref_dist_mat(M, i, j) = -1000000000.0;
if(joe_trace)
fprintf(stderr, rand() & 0x1 ? "wonk!\n" : " wonk!\n");
}
else
e = *aref_dist_mat(M, i, j);
if( e > d )
{
if(s->cluster_assignments[j] < 0)
{
k = j;
l = i;
} else
{
k = i;
l = j;
}
d = e;
}
}
if(joe_trace)
fprintf(stderr, "l = %ld, k = %ld, d = %f\n", l, k, d);
if( (l == 0 && k == 0) || d < t) //we're done
break;
ck = clusters[k].count;
cl = clusters[l].count;
ckl = ck + cl;
for(a = &clusters[0]; a; a = a->next_head)
{
i = a - clusters;
if(i == k || i == l)
continue;
*aref_dist_mat(M, k, i) = (ck * *aref_dist_mat(M, k, i) +
cl * *aref_dist_mat(M, l, i) ) / ckl;
*aref_dist_mat(M, l, i) = 0.0;
}
join_clusters(&clusters[k], &clusters[l]);
n--;
}
i = s->header->n_perma_clusters + 1;
for(a = &clusters[0]; a; a = a->next_head)
{
if(s->cluster_assignments[a - clusters] < 0)
j = -s->cluster_assignments[a - clusters];
else
j = i++;
for(b = a; b; b = b->next)
s->cluster_assignments[b - clusters] = j;
}
s->header->n_clusters = n;
free(M);
}
static void assign_perma_cluster(CLUMPER_STATE_STRUCT *s,
long doc,
char *lab)
{
long i;
for(i = 1; i <= s->header->n_perma_clusters; i++)
if(0 == strcmp(s->cluster_labels[i], lab))
break;
if(i > s->header->n_perma_clusters)
{
i = ++(s->header->n_perma_clusters);
strcpy(s->cluster_labels[i], lab);
}
s->cluster_assignments[doc] = -i;
}
long max(long a, long b) {return a > b ? a : b;}
int crm_expr_clump(CSL_CELL *csl, ARGPARSE_BLOCK *apb)
{
char *txtptr;
long txtstart;
long txtlen;
char htext[MAX_PATTERN];
char filename[MAX_PATTERN];
long htext_len;
char regex_text[MAX_PATTERN]; // the regex pattern
long regex_text_len;
char param_text[MAX_PATTERN];
long param_text_len;
unsigned long long bychunk;
int n_clusters = 0;
char tag[DOCUMENT_TAG_LEN];
char class[CLUSTER_LABEL_LEN];
struct stat statbuf;
CLUMPER_STATE_STRUCT S, *s = &S;
regex_t regee;
regmatch_t matchee[2];
long i, j, k, l;
if (crm_exec_box_restriction(csl, apb, &txtptr, &txtstart, &txtlen) != 0)
return 0;
crm_get_pgm_arg (htext, MAX_PATTERN, apb->p1start, apb->p1len);
htext_len = apb->p1len;
htext_len = crm_nexpandvar (htext, htext_len, MAX_PATTERN);
i = 0;
while (htext[i] < 0x021) i++;
j = i;
while (htext[j] >= 0x021) j++;
htext[j] = '\000';
strcpy (filename, &htext[i]);
//use regex_text and regee to grab parameters
crm_get_pgm_arg (param_text, MAX_PATTERN, apb->s2start, apb->s2len);
param_text_len = apb->s2len;
param_text_len = crm_nexpandvar (param_text, param_text_len, MAX_PATTERN);
param_text[ param_text_len ] = '\0';
if(joe_trace)
fprintf( stderr, "param_text = %s\n", param_text );
strcpy(regex_text, "n_clusters[[:space:]]*=[[:space:]]*([0-9]+)");
if( crm_regcomp (®ee, regex_text, strlen(regex_text), REG_EXTENDED) )
{
nonfatalerror("Problem compiling regex to grab params:", regex_text);
return 0;
}
if(!crm_regexec (®ee, param_text, param_text_len, 2, matchee, 0, NULL))
{
param_text[matchee[1].rm_eo + 1] = '\0';
if(joe_trace)
fprintf(stderr, "¶m_text[matchee[1].rm_so] = %s\n",
¶m_text[matchee[1].rm_so]);
n_clusters = atol(¶m_text[matchee[1].rm_so]);
if(joe_trace)
fprintf(stderr, "n_clusters = %d\n", n_clusters);
}
strcpy(regex_text, "tag[[:space:]]*=[[:space:]]*([[:graph:]]+)");
if( crm_regcomp (®ee, regex_text, strlen(regex_text), REG_EXTENDED) )
{
nonfatalerror("Problem compiling regex to grab params:", regex_text);
return 0;
}
if(!crm_regexec (®ee, param_text, param_text_len, 2, matchee, 0, NULL))
{
param_text[matchee[1].rm_eo] = '\0';
strcpy(tag, ¶m_text[matchee[1].rm_so]);
} else
tag[0] = '\0';
strcpy(regex_text, "clump[[:space:]]*=[[:space:]]*([[:graph:]]+)");
if( crm_regcomp (®ee, regex_text, strlen(regex_text), REG_EXTENDED) )
{
nonfatalerror("Problem compiling regex to grab params:", regex_text);
return 0;
}
if(!crm_regexec (®ee, param_text, param_text_len, 2, matchee, 0, NULL))
{
param_text[matchee[1].rm_eo] = '\0';
strcpy(class, ¶m_text[matchee[1].rm_so]);
} else
class[0] = '\0';
strcpy(regex_text, "max_documents[[:space:]]*=[[:space:]]*([[:graph:]]+)");
if( crm_regcomp (®ee, regex_text, strlen(regex_text), REG_EXTENDED) )
{
nonfatalerror("Problem compiling regex to grab params:", regex_text);
return 0;
}
if(!crm_regexec (®ee, param_text, param_text_len, 2, matchee, 0, NULL))
{
param_text[matchee[1].rm_eo] = '\0';
max_documents = atol(¶m_text[matchee[1].rm_so]);
}
//we've already got a default max_documents
crm_get_pgm_arg (regex_text, MAX_PATTERN, apb->s1start, apb->s1len);
regex_text_len = apb->s1len;
if(regex_text_len == 0)
{
strcpy(regex_text, "[[:graph:]]+");
regex_text_len = strlen( regex_text );
}
regex_text[regex_text_len] = '\0';
regex_text_len = crm_nexpandvar (regex_text, regex_text_len, MAX_PATTERN);
if( crm_regcomp (®ee, regex_text, regex_text_len, REG_EXTENDED) )
{
nonfatalerror("Problem compiling this regex:", regex_text);
return 0;
}
bychunk = apb->sflags & CRM_BYCHUNK;
if (apb->sflags & CRM_REFUTE)
{
if(map_file(s, filename))
//we already nonfatalerrored
return 0;
if(tag[0]) {
for(i = s->header->n_documents; i >= 0; i--)
if(0 == strcmp(tag, s->document_tags[i]))
break;
}
else
i = find_closest_document(s, txtptr + txtstart, txtlen,
®ee, apb->sflags);
if(i < 0)
{
unmap_file(s);
return 0;
}
memmove(s->file_origin + s->document_offsets[i],
s->file_origin + s->document_offsets[i + 1],
(s->header->file_length - s->document_offsets[i + 1]) );
memmove(&s->document_tags[i],
&s->document_tags[i + 1],
sizeof(char) * DOCUMENT_TAG_LEN *(s->header->n_documents - i - 1 ));
memmove(&s->cluster_labels[i],
&s->cluster_labels[i + 1],
sizeof(char) * CLUSTER_LABEL_LEN*(s->header->n_documents - i - 1 ));
s->header->n_documents--;
j = s->document_offsets[i + 1] - s->document_offsets[i];
for(k = i; k < s->header->n_documents; k++)
{
s->document_offsets[k] = s->document_offsets[k + 1] - j;
s->cluster_assignments[k] = s->cluster_assignments[k + 1];
}
s->header->file_length -= j;
for(k = 0; k < s->header->n_documents; k++)
for(l = max(k + 1, i); l < s->header->n_documents; l++)
*aref_dist_mat(s->distance_matrix, k, l) =
*aref_dist_mat(s->distance_matrix, k, l + 1);
if(n_clusters > 0)
{
if(bychunk)
agglomerative_averaging_cluster(s, n_clusters);
else
agglomerative_nearest_cluster(s, n_clusters);
}
else if(n_clusters == 0)
{
if(bychunk)
thresholding_average_cluster(s);
else
thresholding_average_cluster(s);
}
l = s->header->file_length;
unmap_file(s);
crm_force_munmap_filename(filename);
dontcare = truncate(filename, l);
return 0;
} else
{ //LEARNIN'!
long n;
unsigned int feature_space[32768];
FILE *f;
if(stat(filename, &statbuf))
make_new_clumper_backing_file(filename);
if(txtlen == 0)
{
if(tag[0] && class[0]) //is not null
{
if(map_file(s, filename))
//we already nonfatalerrored
return 0;
for(i = s->header->n_documents - 1; i >= 0; i++)
if(0 == strcmp(tag, s->document_tags[i]))
break;
if(i >= 0)
assign_perma_cluster(s, i, class);
unmap_file(s);
}
return 0;
}
n = eat_document
( txtptr + txtstart, txtlen, &i,
®ee,
feature_space, 32768,
apb->sflags );
crm_force_munmap_filename(filename);
f = fopen(filename, "ab+");
dontcare = fwrite(feature_space, n, sizeof(unsigned int), f);
fclose(f);
if(map_file(s, filename))
//we already nonfatalerrored
return 0;
if(s->header->n_documents >= s->header->max_documents)
{
nonfatalerror("This clump backing file is full and cannot"
" assimilate new documents!", filename);
unmap_file(s);
return 0;
}
i = s->header->n_documents++;
s->document_offsets[i] = s->header->file_length;
s->header->file_length += sizeof(unsigned int) * n;
for(j = 0; j < i; j++)
*aref_dist_mat(s->distance_matrix, j, i) = get_document_affinity(
(unsigned int *)( s->file_origin + s->document_offsets[i]),
(unsigned int *)( s->file_origin + s->document_offsets[j]) );
strcpy(s->document_tags[i], tag);
if(class[0])
assign_perma_cluster(s, i, class);
else
s->cluster_assignments[i] = 0;
if(n_clusters > 0)
{
if(bychunk)
agglomerative_averaging_cluster(s, n_clusters);
else
agglomerative_nearest_cluster(s, n_clusters);
}
else if(n_clusters == 0)
{
if(bychunk)
thresholding_average_cluster(s);
else
thresholding_average_cluster(s);
}
unmap_file(s);
return 0;
}
}
int sprint_lab(CLUMPER_STATE_STRUCT *s, char *b, int l)
{
if(l == 0)
{
strcpy(b, "unassigned");
return strlen("unassigned");
}
if(s->cluster_labels[l][0] != '\0')
return sprintf(b, "%s", s->cluster_labels[l]);
else
return sprintf(b, "clump_#%d", l);
}
int sprint_tag(CLUMPER_STATE_STRUCT *s, char *b, int d)
{
if(s->document_tags[d][0] != '\0')
return sprintf(b, "%s", s->document_tags[d]);
else
return sprintf(b, "document_#%d", d);
}
int crm_expr_pmulc(CSL_CELL *csl, ARGPARSE_BLOCK *apb)
{
char *txtptr;
long txtstart;
long txtlen;
char htext[MAX_PATTERN];
char filename[MAX_PATTERN];
long htext_len;
char regex_text[MAX_PATTERN]; // the regex pattern
long regex_text_len;
unsigned long long bychunk;
char out_var[MAX_PATTERN];
long out_var_len;
float A[MAX_CLUSTERS], T;
long N[MAX_CLUSTERS];
double p[MAX_CLUSTERS], pR[MAX_CLUSTERS];
unsigned int feature_space[32768];
long n;
long closest_doc = -1;
float closest_doc_affinity;
long out_len = 0;
CLUMPER_STATE_STRUCT S, *s = &S;
regex_t regee;
long i, j;
if (crm_exec_box_restriction(csl, apb, &txtptr, &txtstart, &txtlen) != 0)
return 0;
crm_get_pgm_arg (htext, MAX_PATTERN, apb->p1start, apb->p1len);
htext_len = apb->p1len;
htext_len = crm_nexpandvar (htext, htext_len, MAX_PATTERN);
i = 0;
while (htext[i] < 0x021) i++;
j = i;
while (htext[j] >= 0x021) j++;
htext[j] = '\000';
strcpy (filename, &htext[i]);
//grab output variable name
crm_get_pgm_arg (out_var, MAX_PATTERN, apb->p2start, apb->p2len);
out_var_len = apb->p2len;
out_var_len = crm_nexpandvar (out_var, out_var_len, MAX_PATTERN);
crm_get_pgm_arg (regex_text, MAX_PATTERN, apb->s1start, apb->s1len);
regex_text_len = apb->s1len;
if(regex_text_len == 0)
{
strcpy(regex_text, "[[:graph:]]+");
regex_text_len = strlen( regex_text );
}
regex_text[regex_text_len] = '\0';
regex_text_len = crm_nexpandvar (regex_text, regex_text_len, MAX_PATTERN);
if( crm_regcomp (®ee, regex_text, regex_text_len, REG_EXTENDED) )
{
nonfatalerror("Problem compiling this regex:", regex_text);
return 0;
}
bychunk = apb->sflags & CRM_BYCHUNK;
if(map_file(s, filename))
//we already nonfatalerrored
return 0;
if(txtlen == 0)
{
for(i = 0; i < s->header->n_documents; i++)
{
A[0] = 0.0;
N[0] = 1;
for(j = 0; j < s->header->n_documents; j++)
if(i != j && s->cluster_assignments[i] == s->cluster_assignments[j])
{
N[0]++;
if(bychunk)
A[0] += *aref_dist_mat(s->distance_matrix, i, j);
else
if(*aref_dist_mat(s->distance_matrix, i, j) > A[0])
A[0] = *aref_dist_mat(s->distance_matrix, i, j);
}
if(bychunk)
A[0] /= (float)N[0];
out_len += sprintf(outbuf + out_len, "%ld (", i);
out_len += sprint_tag(s, outbuf + out_len, i);
out_len += sprintf(outbuf + out_len,
") clump: %ld (", s->cluster_assignments[i]);
out_len += sprint_lab(s, outbuf + out_len, s->cluster_assignments[i]);
out_len += sprintf(outbuf + out_len, ") affinity: %0.4f\n", A[0]);
}
outbuf[out_len] = '\0';
if(out_var_len)
crm_destructive_alter_nvariable(out_var, out_var_len, outbuf, out_len);
unmap_file(s);
return 0;
} else
{
if(joe_trace)
fprintf(stderr, "pmulcing!\n");
n = eat_document
( txtptr + txtstart, txtlen, &i,
®ee,
feature_space, 32768,
apb->sflags );
closest_doc_affinity = -1.0;
for(i = 0; i <= s->header->n_clusters; i++)
{
A[i] = 0.0;
N[i] = 0;
}
if(bychunk)
{
for(i = 0; i < s->header->n_documents; i++)
{
j = s->cluster_assignments[i];
if(j == 0)
continue;
T = get_document_affinity(feature_space, (void *)(s->file_origin +
s->document_offsets[i]));
A[j] += T;
if(T > closest_doc_affinity)
{
closest_doc = i;
closest_doc_affinity = T;
}
N[j]++;
}
for(i = 1; i <= s->header->n_clusters; i++)
T += A[i] /= N[i];
} else
{
for(i = 0; i < s->header->n_documents; i++)
{
j = s->cluster_assignments[i];
T = get_document_affinity(feature_space, (void *)(s->file_origin +
s->document_offsets[i]));
if(T > A[j])
A[j] = T;
if(T > closest_doc_affinity)
{
closest_doc = i;
closest_doc_affinity = T;
}
N[j]++;
}
}
T = 0.0000001;
j = 1;
for(i = 1; i <= s->header->n_clusters; i++)
{
if(A[i] > A[j])
j = i;
if(A[i] == 0.0)
p[i] = 0.0;
else
p[i] = normalized_gauss(1.0 / A[i] - 1.0, 0.5);
//p[i] = A[i];
T += p[i];
}
if(s->header->n_clusters < 2)
p[j = 1] = 0.0;
for(i = 1; i <= s->header->n_clusters; i++)
{
p[i] /= T;
pR[i] = 10 * ( log10(0.0000001 + p[i])
- log10(1.0000001 - p[i]) );
}
if(joe_trace)
fprintf(stderr, "generating output...\n");
if(p[j] > 0.5)
out_len += sprintf(outbuf + out_len,
"PMULC succeeds; success probabilty: %0.4f pR: %0.4f\n", p[j], pR[j]);
else
out_len += sprintf(outbuf + out_len,
"PMULC fails; success probabilty: %0.4f pR: %0.4f\n", p[j], pR[j]);
out_len += sprintf(outbuf + out_len,
"Best match to clump #%ld (", j);
out_len += sprint_lab(s, outbuf + out_len, j);
out_len += sprintf(outbuf + out_len,
") prob: %0.4f pR: %0.4f\n", p[j], pR[j]);
out_len += sprintf(outbuf + out_len,
"Closest document: #%ld (", closest_doc);
out_len += sprint_tag(s, outbuf + out_len, closest_doc);
out_len += sprintf(outbuf + out_len,
") affinity: %0.4f\n", closest_doc_affinity);
out_len += sprintf(outbuf + out_len,
"Total features in input file: %ld\n", n);
for(i = 1; i <= s->header->n_clusters; i++)
{
out_len += sprintf(outbuf + out_len,
"%ld: (", i);
out_len += sprint_lab(s, outbuf + out_len, i);
out_len += sprintf(outbuf + out_len,
"): documents: %ld affinity: %0.4f prob: %0.4f pR: %0.4f\n",
N[i], A[i], p[i], pR[i]);
}
if (p[j] > 0.5000)
{
if (user_trace)
fprintf (stderr, "CLUMP was a SUCCESS, continuing execution.\n");
}
else
{
if (user_trace)
fprintf (stderr, "CLUMP was a FAIL, skipping forward.\n");
// and do what we do for a FAIL here
csl->cstmt = csl->mct[csl->cstmt]->fail_index - 1;
csl->aliusstk [csl->mct[csl->cstmt]->nest_level] = -1;
};
outbuf[out_len] = '\0';
if(joe_trace)
fprintf(stderr, "JOE_TRACE:\n%s", outbuf);
if(out_var_len)
crm_destructive_alter_nvariable(out_var, out_var_len, outbuf, out_len);
unmap_file(s);
return 0;
}
}
|