/* Bayesian Noise Reduction - Contextual Symmetry Logic http://bnr.nuclearelephant.com Copyright (c) 2004 Jonathan A. Zdziarski 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. */ /* Example Program to demonstrate libbnr (using hard-coded sample data). First, run ./configure && make, then compile with: gcc -o example example.c -lbnr -L.libs For a real-world example, see libdspam.c in DSPAM v3.3.6 or higher */ #include #include #include "bnr.h" int main() { BNR_CTX *BTX; char *token; int elim; printf("Bayesian Noise Reduction - Contextual Symmetry Logic\n"); printf("http://bnr.nuclearelephant.com\n"); printf("Copyright (c) 2004 Jonathan A. Zdziarski\n\n"); printf("Initializing Noise Reduction Context...\n"); BTX = bnr_init(BNR_CHAR, 's'); if (BTX == NULL) { perror("unable to initialize BNR context"); exit(EXIT_FAILURE); } /* Add our tokens and token values. Original message ordering must be preserved! */ printf("Populating Message Stream...\n"); /* Some text */ bnr_add(BTX, "SOME", 0.0100); bnr_add(BTX, "TEXT", 0.9000); bnr_add(BTX, "NONSENSIBLE", 0.0001); /* Filtered */ bnr_add(BTX, "TEXT", 0.0001); /* Filtered */ bnr_add(BTX, "PATTERN", 0.9900); bnr_add(BTX, "MORE", 0.0100); bnr_add(BTX, "TEXT", 0.0300); /* Instantiate Patterns */ printf("Instantiating patterns...\n"); if (bnr_instantiate(BTX)) { perror("bnr_instantiate() failed!\n"); goto BAIL; } /* Assign probabilities to our patterns, using some sample values. One pattern will be given an extremely high spam probability to illustrate how inconsistencies are identified */ token = bnr_get_pattern(BTX); while(token) { printf("Pattern '%s' was instantiated\n", token); if (token && !strcmp(token, "bnr.s|0.00_0.00_1.00_")) bnr_set_pattern(BTX, token, 0.9900); else bnr_set_pattern(BTX, token, 0.5000); token = bnr_get_pattern(BTX); } /* Everything's set up now, perform the processing */ printf("Finalizing context...\n"); if (bnr_finalize(BTX)) { perror("bnr_finalize() failed!\n"); goto BAIL; } printf("Total Eliminations: %ld\n", BTX->eliminations); printf("Total Remaining: %ld\n", BTX->stream->items-BTX->eliminations); printf("Output stream: "); token = bnr_get_token(BTX, &elim); while(token) { if (!elim) printf("%s ", token); token = bnr_get_token(BTX, &elim); } printf("\n"); printf("Dubbed: "); token = bnr_get_token(BTX, &elim); while(token) { if (elim) printf("%s ", token); token = bnr_get_token(BTX, &elim); } printf("\n"); /* Destroy our context */ printf("Destroying noise reduction context...\n"); bnr_destroy(BTX); exit(EXIT_SUCCESS); BAIL: bnr_destroy(BTX); exit(EXIT_FAILURE); }