📄 npx.c
字号:
/*---------------------------------------------------------------------- File : npx.c Contents: naive possibilistic classifier execution Author : Christian Borgelt History : 07.02.2001 file created from file nbc.c 17.07.2001 adapted to modified module scan 18.09.2002 bug concerning missing target fixed 02.02.2003 bug in alignment in connection with -d fixed 23.04.2003 missing AS_MARKED added for second reading 16.08.2003 slight changes in error message output----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <assert.h>#ifndef AS_RDWR#define AS_RDWR#endif#ifndef AS_PARSE#define AS_PARSE#endif#ifndef NPC_PARSE#define NPC_PARSE#endif#include "nposs.h"#include "io.h"#ifdef STORAGE#include "storage.h"#endif/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*/#define PRGNAME "npx"#define DESCRIPTION "naive possibilistic classifier execution"#define VERSION "version 1.8 (2004.08.12) " \ "(c) 1999-2004 Christian Borgelt"/* --- error codes --- */#define OK 0 /* no error */#define E_NONE 0 /* no error */#define E_NOMEM (-1) /* not enough memory */#define E_FOPEN (-2) /* cannot open file */#define E_FREAD (-3) /* read error on file */#define E_FWRITE (-4) /* write error on file */#define E_OPTION (-5) /* unknown option */#define E_OPTARG (-6) /* missing option argument */#define E_ARGCNT (-7) /* wrong number of arguments */#define E_STDIN (-8) /* double assignment of stdin */#define E_PARSE (-9) /* parse error */#define E_CLASS (-10) /* missing class */#define E_UNKNOWN (-11) /* unknown error *//*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- classification result info.--- */ ATT *att; /* class attribute */ char *n_class; /* name of classification column */ int w_class; /* width of classification column */ int class; /* class (classification result) */ char *n_poss; /* name of possibility column */ int w_poss; /* width of possibility column */ double poss; /* degree of possibility of result */} CRINFO; /* (classification result info.) *//*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/static const char *errmsgs[] = { /* error messages */ /* E_NONE 0 */ "no error\n", /* E_NOMEM -1 */ "not enough memory\n", /* E_FOPEN -2 */ "cannot open file `%s'\n", /* E_FREAD -3 */ "read error on file `%s'\n", /* E_FWRITE -4 */ "write error on file `%s'\n", /* E_OPTION -5 */ "unknown option -%c\n", /* E_OPTARG -6 */ "missing option argument\n", /* E_ARGCNT -7 */ "wrong number of arguments\n", /* E_STDIN -8 */ "double assignment of standard input\n", /* E_PARSE -9 */ "parse error(s) on file `%s'\n", /* E_CLASS -10 */ "missing class `%s' in file `%s'\n", /* E_UNKNOWN -11 */ "unknown error\n"};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/const char *prgname = NULL; /* program name for error messages */static SCAN *scan = NULL; /* scanner */static NPC *npc = NULL; /* naive possibilistic classifier */static ATTSET *attset = NULL; /* attribute set */static FILE *in = NULL; /* input file */static FILE *out = NULL; /* output file */static CRINFO cri = { /* classification result information */ NULL, /* class attribute */ "npc", 0, 0, /* data for classification column */ NULL, 0, 0.0 }; /* data for possibility column *//*---------------------------------------------------------------------- Functions----------------------------------------------------------------------*/static void error (int code, ...){ /* --- print error message */ va_list args; /* list of variable arguments */ const char *msg; /* error message */ assert(prgname); /* check the program name */ if (code < E_UNKNOWN) code = E_UNKNOWN; if (code < 0) { /* if to report an error, */ msg = errmsgs[-code]; /* get error message */ if (!msg) msg = errmsgs[-E_UNKNOWN]; fprintf(stderr, "\n%s: ", prgname); va_start(args, code); /* get variable arguments */ vfprintf(stderr, msg, args);/* print error message */ va_end(args); /* end argument evaluation */ } #ifndef NDEBUG if (npc) npc_delete(npc, 0); if (attset) as_delete(attset); /* clean up memory */ if (scan) sc_delete(scan); /* and close files */ if (in && (in != stdin)) fclose(in); if (out && (out != stdout)) fclose(out); #endif #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif exit(code); /* abort programm */} /* error() *//*--------------------------------------------------------------------*/static void infout (ATTSET *set, FILE *file, int mode, CCHAR *seps){ /* --- write additional information */ int i, k; /* loop variables, buffers */ CCHAR *class; /* class attribute/value name */ char *poss; /* name of possibility column */ char buf[32]; /* buffer for output */ if (mode & AS_ATT) { /* if to write header */ class = cri.n_class; /* get name of classification column */ poss = cri.n_poss; /* and name of possibility column */ if (mode & AS_ALIGN) { /* if to align fields */ if ((mode & AS_WEIGHT) || poss) { i = att_valwd(cri.att, 0); k = (int)strlen(class); cri.w_class = (i > k) ? i : k; } /* compute width of class column */ if (poss && (mode & AS_WEIGHT)) { k = (int)strlen(poss); cri.w_poss = (4 > k) ? 4 : k; } } } /* compute width of poss. column */ else { /* if to write a normal record */ class = att_valname(cri.att, cri.class); if (cri.n_poss) sprintf(buf, "%g", cri.poss); poss = buf; /* format value and get the buffer */ } /* get and format field contents */ k = fprintf(file, class); /* write classification result */ for (i = cri.w_class -k; --i >= 0; ) fputc(seps[0], file); /* if to align, pad with blanks */ if (cri.n_poss) { /* if to write class possibility */ fputc(seps[1], file); /* write field separator */ fputs(poss, file); /* and number of errors */ for (i = cri.w_poss -k; --i >= 0; ) fputc(seps[0], file); } /* if to align, pad with blanks */} /* infout() *//*--------------------------------------------------------------------*/int main (int argc, char* argv[]){ /* --- main function */ int i, k = 0, f; /* loop variables, buffer */ char *s; /* to traverse options */ char **optarg = NULL; /* option argument */ char *fn_hdr = NULL; /* name of table header file */ char *fn_tab = NULL; /* name of table file */ char *fn_npc = NULL; /* name of classifier file */ char *fn_out = NULL; /* name of output file */ char *blanks = NULL; /* blanks */ char *fldseps = NULL; /* field separators */ char *recseps = NULL; /* record separators */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -