📄 bci.c
字号:
printf("-b/f/r# blank characters, field and record separators\n" " (default: \" \\t\\r\", \" \\t\", \"\\n\")\n"); printf("-u# unknown value characters (default: \"?\")\n"); printf("-n number of tuple occurrences in last field\n"); printf("domfile file containing domain descriptions\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("bcfile file to write Bayes classifier to\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 (1) { /* traverse characters */ switch (*s++) { /* evaluate option */ case 'F': full = 1; break; case 'c': optarg = &clscol; break; case 'w': balance = (*s) ? *s++ : 0; break; case 's': simp = (*s) ? *s++ : 0; break; case 'L': lcorr = strtod(s, &s); break; case 't': setup |= NBC_DISTUV; break; case 'm': setup |= NBC_MAXLLH; break; case 'p': desc |= NBC_REL; break; case 'l': maxlen = (int)strtol(s, &s, 0); break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'u': optarg = &uvchars; break; case 'n': flags |= AS_WEIGHT; break; case 'd': flags |= 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_dom = s; break; case 1: fn_tab = s; break; case 2: fn_bc = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check the option argument */ if (k != 3) error(E_ARGCNT); /* and the number of arguments */ if (fn_hdr && (strcmp(fn_hdr, "-") == 0)) fn_hdr = ""; /* convert "-" to "" */ i = (!fn_dom || !*fn_dom) ? 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 (simp == 'a') setup |= NBC_ADD; else if (simp == 'r') setup |= NBC_REMOVE; else if (simp != 0 ) /* check simplification mode */ error(E_SIMP, simp); /* (must be 'add' or 'remove') */ if ((balance != 0) && (balance != 'l') && (balance != 'b') && (balance != 's')) error(E_BALANCE, balance); /* check balancing mode */ if (fn_hdr) /* set the header file flag */ flags = AS_ATT | (flags & ~AS_DFLT); /* --- read attribute set --- */ scan = sc_create(fn_dom); /* create a scanner */ if (!scan) error((!fn_dom || !*fn_dom) ? E_NOMEM : E_FOPEN, fn_dom); 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 and */ || !sc_eof(scan)) /* check for end of file */ error(E_PARSE, sc_fname(scan)); sc_delete(scan); scan = NULL; /* delete the scanner */ attcnt = as_attcnt(attset); /* get and print the number of atts. */ fprintf(stderr, "[%d attribute(s)] done.\n", attcnt); /* --- determine id of class column --- */ if (!clscol) /* if no class column name given, */ clsid = attcnt -1; /* use the last column as the class */ else { /* if a class column name given */ clsid = as_attid(attset, clscol); if (clsid < 0) error(E_CLASS, clscol, fn_dom); } /* check whether class exists */ att = as_att(attset, clsid); /* get the class attribute */ if (att_type(att) != AT_SYM) /* check the type of the class */ error(E_CTYPE, att_name(att)); /* (must be symbolic) */ /* --- read table header --- */ as_chars(attset, blanks, fldseps, recseps, uvchars); in = io_hdr(attset, fn_hdr, fn_tab, flags, 1); if (!in) error(1); /* read the table header */ /* --- construct naive Bayes classifier --- */ if (!full /* if to induce a naive Bayes class. */ && (balance /* and to balance class frequencies */ || simp)) { /* or to simplify the classifier */ table = io_bodyin(attset, in, fn_tab, flags, "table", 1); if (!table) error(1); /* read the table body */ fprintf(stderr, "reducing%s table ... ", (balance) ? " and balancing" : ""); tab_reduce(table); /* reduce table for speed up */ if (balance) { /* if the balance flag is set */ tab_balance(table, clsid, (balance == 'l') ? -2.0F : (balance == 'b') ? -1.0F : 0.0F, NULL); } /* balance the class frequencies */ tplwgt = tab_getwgt(table, 0, INT_MAX); fprintf(stderr, "[%d/", tab_tplcnt(table)); fprintf(stderr, "%g tuple(s)] done.\n", tplwgt); fprintf(stderr, "building classifier ... "); nbc = nbc_induce(table, clsid, setup, lcorr); if (!nbc) error(E_NOMEM); /* induce a classifier and */ attcnt = nbc_mark(nbc); /* mark the selected attributes */ fprintf(stderr, "done.\n");}/* print a success message */ else { /* if to build a normal classifier */ if (full) fbc = fbc_create(attset, clsid); else nbc = nbc_create(attset, clsid); if (!fbc && !nbc) /* create either a full or */ error(E_NOMEM); /* a naive Bayes classifier */ k = AS_INST | (flags & ~(AS_ATT|AS_DFLT)); i = ((flags & AS_DFLT) && !(flags & AS_ATT)) ? 0 : as_read(attset, in, k); while (i == 0) { /* record read loop */ if (((fbc) ? fbc_add(fbc, NULL) : nbc_add(nbc, NULL)) != 0) error(E_NOMEM); /* process tuple and count it */ tplcnt++; tplwgt += as_getwgt(attset); i = as_read(attset, in, k); } /* try to read the next record */ if (i < 0) { /* if an error occurred, */ err = as_err(attset); /* get the error information */ tplcnt += (flags & (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 input file */ in = NULL; /* and set up the classifier */ if (fbc) { fbc_setup(fbc, setup, lcorr); attcnt = fbc_mark(fbc); } else { nbc_setup(nbc, setup|NBC_ALL, lcorr); } fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt); } /* print a success message */ /* --- describe created classifier --- */ if (fn_bc && *fn_bc) /* if an output file name is given, */ out = fopen(fn_bc, "w"); /* open output file for writing */ else { /* if no output file name is given, */ out = stdout; fn_bc = "<stdout>"; } /* write to std. output */ fprintf(stderr, "writing %s ... ", fn_bc); if (!out) error(E_FOPEN, fn_bc); k = (full || simp) /* print only the class and */ ? AS_MARKED : 0; /* the marked attributes */ if (as_desc(attset, out, k|AS_TITLE|AS_IVALS, maxlen) != 0) error(E_FWRITE, fn_bc); /* describe attribute domains */ fputc('\n', out); /* leave one line empty */ k = (simp) ? NBC_MARKED : 0; /* print only marked attributes */ if (((fbc) ? fbc_desc(fbc, out, desc |FBC_TITLE, maxlen) : nbc_desc(nbc, out, desc|k|NBC_TITLE, maxlen)) != 0) error(E_FWRITE, fn_bc); /* describe Bayes classifier */ if (maxlen <= 0) maxlen = 72; /* determine maximal line length */ fputs("\n/*", out); /* append additional information */ for (k = maxlen -2; --k >= 0; ) fputc('-', out); fprintf(out, "\n number of attributes: %d", attcnt); fprintf(out, "\n number of tuples : %g\n", tplwgt); for (k = maxlen -2; --k >= 0; ) fputc('-', out); fputs("*/\n", out); /* terminate additional information */ if (out != stdout) { /* if not written to stdout, */ k = fclose(out); out = NULL;/* close the output file */ if (k) error(E_FWRITE, fn_bc); } /* print a success message */ fprintf(stderr, "[%d attribute(s)] done.\n", attcnt); /* --- clean up --- */ #ifndef NDEBUG if (table) tab_delete(table, 0); /* delete table, */ if (nbc) nbc_delete(nbc, 1); /* naive Bayes classifier, */ if (fbc) fbc_delete(fbc, 1); /* full Bayes 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 + -