📄 clc.c
字号:
printf("-b/f/r# blank characters, field and record separators\n" " (default: \" \\t\\r\", \" ,\\t\", \"\\n\")\n"); printf("-C# comment characters (default: \"#\")\n"); printf("clsfile file to read cluster set description from\n"); printf("-d use default 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"); 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 '!': help(); break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'C': optarg = &comment; break; case 'x': donly = 1; break; case 'm': optarg = &conjfn1; break; case 'c': optarg = &conjfn2; 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_cls1 = s; break; case 1: fn_cls2 = s; break; case 2: fn_in = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check option argument and */ if (k != 3) error(E_ARGCNT); /* the number of arguments */ i = (!fn_cls1 || !*fn_cls1) ? 1 : 0; if (!fn_cls2 || !*fn_cls2) i++; if (!fn_in || !*fn_in ) i++; if (fn_hdr) { /* set the header file flag */ flags = AS_ATT | (flags & ~AS_DFLT); if (strcmp(fn_hdr, "-") == 0) fn_hdr = ""; if (!*fn_hdr) i++; /* convert "-" to "" and */ } /* count assignments of stdin */ if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (donly) cfc1 = cfc2 = -1; /* if only mean squared difference, */ else { /* clear both confunctions */ cfc1 = code(conjtab,conjfn1); if (cfc1 < 0) error(E_CONJFN, conjfn1); cfc2 = code(conjtab,conjfn2); if (cfc2 < 0) error(E_CONJFN, conjfn2); } /* otherwise get the conjunctions */ /* --- read first cluster set --- */ scan = sc_create(fn_cls1); /* create a scanner */ if (!scan) error((!fn_cls1 || !*fn_cls1) ? E_NOMEM:E_FOPEN, fn_cls1); fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if (sc_nexter(scan) < 0) error(E_PARSE, sc_fname(scan)); matinp = (sc_token(scan) == T_ID) && (strcmp(sc_value(scan), "dom") != 0); if (matinp) /* if matrix version */ clset1 = cls_parse(scan); /* parse the input cluster set */ else { /* if table version */ attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM);/* create an attribute set */ if ((as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0)) /* parse the attribute set */ error(E_PARSE, sc_fname(scan)); for (i = as_attcnt(attset); --i >= 0; ) if (att_getdir(as_att(attset, i)) != DIR_IN) att_setmark(as_att(attset, i), -1); attmap = am_create(attset, AM_MARKED, 1.0); if (!attmap) error(E_NOMEM);/* create an attribute map */ clset1 = cls_parsex(scan, attmap, 1); } /* parse the cluster set */ if (!clset1 || !sc_eof(scan)) error(E_PARSE, sc_fname(scan)); attcnt = (matinp) /* get the number of attributes */ ? cls_incnt(clset1) : as_attcnt(attset); clscnt = cls_clscnt(clset1); /* get the number of clusters */ fprintf(stderr, "[%d attribute(s), ", attcnt); fprintf(stderr, "%d cluster(s)] done.", clscnt); sc_delete(scan); scan = NULL; /* delete the scanner */ cls_setup(clset1); /* set up the cluster set */ /* --- read second cluster set --- */ scan = sc_create(fn_cls2); /* create a scanner */ if (!scan) error((!fn_cls2 || !*fn_cls2) ? E_NOMEM:E_FOPEN, fn_cls2); fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if (sc_nexter(scan) < 0) error(E_PARSE, sc_fname(scan)); if (matinp) /* if matrix version */ clset2 = cls_parse(scan); /* parse the input cluster set */ else { /* if table version */ dummy = as_create("domains", att_delete); if (!dummy) error(E_NOMEM); /* create an attribute set */ if ((as_parse(dummy, scan, AT_ALL) != 0) || (as_attcnt(dummy) <= 0))/* parse the attribute set */ error(E_PARSE, sc_fname(scan)); as_delete(dummy); /* delete the attribute set */ clset2 = cls_parsex(scan, attmap, 1); } /* parse the cluster set */ if (!clset2 || !sc_eof(scan)) error(E_PARSE, sc_fname(scan)); i = cls_clscnt(clset2); /* get the number of clusters */ fprintf(stderr, "[%d attribute(s), ", attcnt); fprintf(stderr, "%d cluster(s)] done.", i); sc_delete(scan); scan = NULL; /* delete the scanner */ if (i != clscnt) error(E_DIFFCNT, clscnt, i); cls_setup(clset2); /* set up the cluster set */ /* --- create partition matrices --- */ pmat1 = pm_create(clscnt); /* create a partition matrix */ pmat2 = pm_create(clscnt); /* for each of the cluster sets */ if (!pmat1 && !pmat2) error(E_NOMEM); if (matinp) { /* if matrix version */ /* --- process patterns --- */ if (fn_in && *fn_in) /* if an file name is given, */ in = fopen(fn_in, "r"); /* open the file for reading */ else { /* if no fine name is given, */ in = stdin; fn_in = "<stdin>"; } /* use std. input */ fprintf(stderr, "\nreading %s ... ", fn_in); if (!in) error(E_FOPEN, fn_in); tscan = ts_create(); /* create a table scanner and */ if (!tscan) error(E_NOMEM); /* set the separator characters */ if (blanks) ts_chars(tscan, TS_BLANK, blanks); if (fldseps) ts_chars(tscan, TS_FLDSEP, fldseps); if (recseps) ts_chars(tscan, TS_RECSEP, recseps); if (comment) ts_chars(tscan, TS_COMMENT, comment); ts_chars(tscan,TS_NULL,""); /* remove the null value characters */ tse = ts_info(tscan); /* get the error information */ pat = vec_readx(tscan, in, &attcnt); if (!pat) { /* read the first training pattern */ if (tse->code >= 0) error(E_FREAD, fn_in); error(tse->code, fn_in, 1, tse->s, tse->fld, tse->exp); } /* check for success */ do { /* pattern read loop */ cls_exec(clset1,pat,NULL);/* execute the first cluster set */ if (pm_addcol(pmat1, clset1) < 0) error(E_NOMEM); /* store the membership degrees */ cls_exec(clset2,pat,NULL);/* execute the second cluster set */ if (pm_addcol(pmat2, clset2) < 0) error(E_NOMEM); /* store the membership degrees */ patcnt++; /* count pattern and read next */ } while (vec_read(pat, attcnt, tscan, in) == 0); if (tse->code < 0) /* check for an error */ error(tse->code, fn_in, patcnt +1, tse->s, tse->fld, tse->exp); if (ts_delim(tscan) != TS_EOF) /* check for end of file */ error(E_VALUE, fn_in, patcnt +1, "\"\"", 1); if (in != stdin) fclose(in);/* close the input file and */ in = NULL; /* clear the file variable */ fprintf(stderr, "[%d pattern(s)] done.\n", patcnt); } else { /* if table version */ /* --- process patterns --- */ fprintf(stderr, "\n"); /* terminate previous log message */ as_chars(attset, recseps, fldseps, blanks, "", comment); in = io_hdr(attset, fn_hdr, fn_in, flags|AS_NOXATT, 1); if (!in) error(1); /* read the table header */ k = AS_INST | (k & ~AS_ATT);/* write the attribute names */ f = AS_INST | (flags & ~(AS_ATT|AS_DFLT)); i = ((flags & AS_DFLT) && !(flags & AS_ATT)) ? 0 : as_read(attset, in, f); while (i == 0) { /* record read loop */ tplcnt++; /* count tuple and sum its weight */ tplwgt += w = as_getwgt(attset); cls_valuex(clset1, NULL); /* set the data from a tuple and */ cls_exec(clset1, NULL, NULL); /* execute first cluster set */ if (pm_addcol(pmat1, clset1) < 0) error(E_NOMEM); /* store the membership degrees */ cls_valuex(clset2, NULL); /* set the data from a tuple and */ cls_exec(clset2, NULL, NULL); /* execute second cluster set */ if (pm_addcol(pmat2, clset2) < 0) error(E_NOMEM); /* store the membership degrees */ i = as_read(attset, in, f); } /* try to read the next record */ if (i < 0) { /* if an error occurred, */ tse = as_err(attset); /* get the error information */ tplcnt += (flags & (AS_ATT|AS_DFLT)) ? 1 : 2; io_error(i, fn_in, tplcnt, tse->s, tse->fld, tse->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 */ fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt); } /* if (matinp) .. else .. */ /* --- compute and print evaluations --- */ for (i = 8; --i >= 0; ) eval[i] = 0; if (pm_cmp(pmat1, pmat2, cfc1, cfc2, eval) < 0) error(E_NOMEM); /* compare the partition matrices */ printf("mean squared difference : %18.16g\n", eval[PM_DIFF]); if (!donly) { /* check for other measures */ printf("cross-classification accuracy: %18.16g\n", eval[PM_ACC]); printf("F1 measure : %18.16g\n", eval[PM_F1]); printf("Rand statistic : %18.16g\n", eval[PM_RAND]); printf("Jaccard coefficient : %18.16g\n", eval[PM_JACCARD]); printf("Folkes-Mallows index : %18.16g\n", eval[PM_FOLKES]); printf("Hubert index : %18.16g\n", eval[PM_HUBERT]); } /* --- clean up --- */ #ifndef NDEBUG pm_delete(pmat1); /* delete the partition matrices */ pm_delete(pmat2); if (matinp) { /* if matrix version */ cls_delete(clset1); /* delete the cluster sets */ cls_delete(clset2); ts_delete(tscan); } /* delete the table scanner */ else { /* if table version */ cls_deletex(clset2, 0); /* delete the cluster sets */ cls_deletex(clset1, 1); /* and the attribute set */ } #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -