📄 npx.c
字号:
char *uvchars = NULL; /* unknown value characters */ int inflags = 0; /* table file read flags */ int outflags = AS_ATT; /* table file write flags */ int tplcnt = 0; /* number of tuples */ double tplwgt = 0; /* weight of tuples */ double errcnt = 0; /* number of misclassifications */ int attid; /* loop variable for attributes */ float wgt; /* tuple/instantiation weight */ TFSERR *err; /* error information */ prgname = argv[0]; /* get program name for error msgs. */ /* --- print startup/usage message --- */ if (argc > 1) { /* if arguments are given */ fprintf(stderr, "%s - %s\n", argv[0], DESCRIPTION); fprintf(stderr, VERSION); } /* print a startup message */ else { /* if no argument given */ printf("usage: %s [options] nbcfile " "[-d|-h hdrfile] tabfile [outfile]\n", argv[0]); printf("naive possibilistic classifier execution\n"); printf("%s\n", VERSION); printf("-c# classification field name " "(default: %s)\n", cri.n_class); printf("-p# confidence field name " "(default: no confidence output)\n"); printf("-n number of tuple occurrences in last field\n"); printf("-a align fields (default: do not align)\n"); printf("-w do not write field names to output file\n"); printf("-b/f/r# blanks, field and record separators " "(default: \" \\t\", \" \\t\", \"\\n\")\n"); printf("-u# unknown value characters (default: \"?\")\n"); printf("npcfile file containing classifier description\n"); printf("-d use default table header " "(field names = field numbers)\n"); printf("-h read table header (field names) from hdrfile\n"); printf("hdrfile file containing table header (field names)\n"); printf("tabfile table file to read " "(field names in first record)\n"); printf("outfile file to write output table to (optional)\n"); return 0; /* print a usage message */ } /* and abort the program */ /* --- evaluate arguments --- */ for (i = 1; i < argc; i++) { /* traverse arguments */ s = argv[i]; /* get option argument */ if (optarg) { *optarg = s; optarg = NULL; continue; } if ((*s == '-') && *++s) { /* -- if argument is an option */ while (*s) { /* traverse options */ switch (*s++) { /* evaluate option */ case 'c': optarg = &cri.n_class; break; case 'p': optarg = &cri.n_poss; break; case 'n': inflags |= AS_WEIGHT; outflags |= AS_WEIGHT; break; case 'a': outflags |= AS_ALIGN; break; case 'w': outflags &= ~AS_ATT; break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'u': optarg = &uvchars; break; case 'd': inflags |= AS_DFLT; break; case 'h': optarg = &fn_hdr; break; default : error(E_OPTION, *--s); break; } /* set option variables */ if (!*s) break; /* if at end of string, abort loop */ if (optarg) { *optarg = s; optarg = NULL; break; } } } /* get option argument */ else { /* if argument is no option */ switch (k++) { /* evaluate non-option */ case 0: fn_npc = s; break; case 1: fn_tab = s; break; case 2: fn_out = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check the option argument */ if ((k < 2) || (k > 3)) /* and the number of arguments */ error(E_ARGCNT); if (fn_hdr && (strcmp(fn_hdr, "-") == 0)) fn_hdr = ""; /* convert "-" to "" */ i = (!fn_npc || !*fn_npc) ? 1 : 0; if (!fn_tab || !*fn_tab) i++; if ( fn_hdr && !*fn_hdr) i++;/* check assignments of stdin */ if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (fn_hdr) /* set the header file flag */ inflags = AS_ATT | (inflags & ~AS_DFLT); if ((outflags & AS_ATT) && (outflags & AS_ALIGN)) outflags |= AS_ALNHDR; /* set align to header flag */ /* --- read naive possibilistic classifier --- */ scan = sc_create(fn_npc); /* create a scanner */ if (!scan) error((!fn_npc || !*fn_npc) ? E_NOMEM : E_FOPEN, fn_npc); attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM); /* create an attribute set */ fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if ((sc_nexter(scan) < 0) /* start scanning (get first token) */ || (as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0)) /* parse attribute set */ error(E_PARSE, sc_fname(scan)); npc = npc_parse(attset,scan); /* parse the poss. classifier */ if (!npc || !sc_eof(scan)) /* and check for end of file */ error(E_PARSE, sc_fname(scan)); sc_delete(scan); scan = NULL; /* delete the scanner */ fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(attset)); cri.att = as_att(attset, npc_clsid(npc)); /* --- read table header --- */ for (attid = as_attcnt(attset); --attid >= 0; ) att_setmark(as_att(attset, attid), 1); att_setmark(cri.att, 0); /* mark all attribs. except the class */ as_chars(attset, blanks, fldseps, recseps, uvchars); in = io_hdr(attset, fn_hdr, fn_tab, inflags|AS_MARKED, 1); if (!in) error(1); /* read the table header */ /* --- classify tuples --- */ if ((att_getmark(cri.att) < 0)/* either the class must be present */ && (k <= 2)) /* or an output file must be written */ error(E_CLASS, att_name(cri.att), fn_tab); if (k > 2) { /* if to write an output table */ if ((outflags & AS_ALIGN) /* if to align output file */ && (in != stdin)) { /* and not to read from stdin */ i = AS_INST | (inflags & ~(AS_ATT|AS_DFLT)); while (as_read(attset, in, i) == 0); fclose(in); /* determine the column widths */ in = io_hdr(attset, fn_hdr, fn_tab, inflags|AS_MARKED, 1); if (!in) error(1); /* reread the table header */ } /* (necessary because of first tuple) */ if (!fn_out || !*fn_out) { /* if no proper file name is given */ out = stdout; fn_out = "<stdout>"; } /* write to stdout */ else { /* if a proper file name is given */ out = fopen(fn_out, "w"); /* open output file for writing */ if (!out) error(E_FOPEN, fn_out); } k = AS_MARKED|AS_INFO1|AS_RDORD|outflags; if (outflags & AS_ATT) /* if to write table header */ as_write(attset, out, k, infout); k = AS_INST|(k & ~AS_ATT); /* write the attribute names */ } /* to the output file */ f = AS_INST | (inflags & ~(AS_ATT|AS_DFLT)); i = ((inflags & AS_DFLT) && !(inflags & AS_ATT)) ? 0 : as_read(attset, in, f); while (i == 0) { /* record read loop */ cri.class = npc_exec(npc, NULL, &cri.poss); wgt = as_getwgt(attset); /* classify tuple */ tplwgt += wgt; tplcnt++; /* count tuple and sum its weight */ if (cri.class != att_inst(cri.att)->i) errcnt += wgt; /* count classification errors */ if (out && (as_write(attset, out, k, infout) != 0)) error(E_FWRITE, fn_out); /* write tuple to output file */ i = as_read(attset, in, f); /* try to read the next record */ } if (i < 0) { /* if an error occurred, */ err = as_err(attset); /* get the error information */ tplcnt += (inflags & (AS_ATT|AS_DFLT)) ? 1 : 2; io_error(i, fn_tab, tplcnt, err->s, err->fld, err->exp); error(1); /* print an error message */ } /* and abort the program */ if (in != stdin) fclose(in); /* close the table file and */ in = NULL; /* clear the file variable */ if (out && (out != stdout)) { /* if an output file exists, */ i = fclose(out); out = NULL; /* close the output file */ if (i) error(E_FWRITE, fn_out); } fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt); if (att_getmark(cri.att) >= 0) { fprintf(stderr, "%g error(s) (%.2f%%)\n", errcnt, (tplwgt > 0) ? 100*(errcnt /tplwgt) : 0); } /* if class found, print errors */ /* --- clean up --- */ #ifndef NDEBUG npc_delete(npc, 1); /* delete possibilistic classifier */ #endif /* and underlying attribute set */ #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -