📄 clx.c
字号:
printf("-m# name/prefix of membership degree field(s)\n"); printf("-o# output format for numbers " "(default: \"%s\")\n", fmt); printf("-a align fields (default: do not align)\n"); printf("-w do not write field names to output file\n"); 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"); printf("outfile file to write extended tuples 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 'c': optarg = &clsname; break; case 'm': optarg = &msdname; break; case 'a': outflags |= AS_ALIGN; break; case 'w': outflags &= ~AS_ATT; break; case 'd': inflags |= AS_DFLT; break; case 'h': optarg = &fn_hdr; break; case 'o': optarg = &fmt; break; case 'b': optarg = &blanks; break; case 'f': optarg = &fldseps; break; case 'r': optarg = &recseps; break; case 'C': optarg = &comment; 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_cls = s; break; case 1: fn_in = s; break; case 2: fn_out = 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_cls || !*fn_cls) ? 1 : 0; if (!fn_in || !*fn_in ) i++; if (fn_hdr) { /* set the header file flag */ inflags = AS_ATT | (inflags & ~AS_DFLT); if (strcmp(fn_hdr, "-") == 0) fn_hdr = ""; if (!*fn_hdr) i++; /* convert "-" to "" and */ } /* count assignment of stdin */ if ((outflags & AS_ATT) && (outflags & AS_ALIGN)) outflags |= AS_ALNHDR; /* set align to header flag */ if (i > 1) error(E_STDIN); /* stdin must not be used twice */ if (clsname) clsidx = 1; /* set flags for */ if (msdname) msdeg = 1; /* the matrix version */ /* --- read cluster set --- */ scan = sc_create(fn_cls); /* create a scanner */ if (!scan) error((!fn_cls || !*fn_cls) ? E_NOMEM : E_FOPEN, fn_cls); 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 */ clset = cls_parse(scan); /* parse the 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; ) { att = as_att(attset, i); /* traverse the parsed attributes */ if (att_getdir(att) != DIR_IN) att_setmark(att, -1); } /* use only the input attributes */ attmap = am_create(attset, AM_MARKED, 1.0); if (!attmap) error(E_NOMEM);/* create an attribute map */ clset = cls_parsex(scan, attmap, 1); } /* parse the cluster set */ if (!clset || !sc_eof(scan)) error(E_PARSE, sc_fname(scan)); attcnt = (matinp) /* get the number of attributes */ ? cls_incnt(clset) : as_attcnt(attset); clscnt = cls_clscnt(clset); /* 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(clset); /* set up the cluster set */ if (matinp) { /* if matrix version */ /* --- process data tuples --- */ if (!fn_in || !*fn_in) { /* if no file is given, use stdin */ fn_in = "<stdin>"; in = stdin; } else { /* if a pattern file is given, */ in = fopen(fn_in, "r"); /* open it for reading */ if (!in) error(E_FOPEN, fn_in); } fprintf(stderr, "\nreading %s ... ", fn_in); if (fn_out && *fn_out) /* if a file name is given, */ out = fopen(fn_out, "w"); /* open the file for writing */ else { /* if no output file is given, */ out = stdout; fn_out = "<stdout>"; } /* use std. output */ if (!out) error(E_FOPEN, fn_out); tscan = ts_create(); /* create a table scanner and */ if (!tscan) error(E_NOMEM); /* set the separator characters */ if (blanks) seps[0] = ts_chars(tscan, TS_BLANK, blanks); if (fldseps) seps[1] = ts_chars(tscan, TS_FLDSEP, fldseps); if (recseps) seps[2] = 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 */ for (i = 0; i < attcnt; i++) { if (i > 0) fputc(seps[1], out); fprintf(out, fmt, pat[i]); } /* print the pattern elements */ fputc(seps[0], out); /* print a separating blank */ i = cls_exec(clset, pat, NULL); /* execute the cluster set */ if (clsidx) { /* if to print a cluster index */ fputc(seps[1], out); /* print the index of the cluster */ fprintf(out, "%d", i); /* with the highest m.s. degree */ if (msdeg) { /* if the m.s. degree flag is set */ fputc(seps[1], out); /* print a separator */ fprintf(out, fmt, cls_msdeg(clset, i)); } } /* print the membership degree */ else { /* if to print all m.s. degrees */ for (k = 0; k < clscnt; k++) { fputc(seps[1], out); /* traverse the clusters */ fprintf(out, fmt, cls_msdeg(clset, k)); } /* print the membership degrees */ } /* to the different clusters */ fputc(seps[2], out); /* and terminate the record */ 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 */ if (out != stdout) { /* if not written to stdout, */ k = fclose(out); out = NULL; /* close the output file */ if (k != 0) error(E_FWRITE, fn_out); } /* check for successful writing */ fprintf(stderr, "[%d pattern(s)] done.\n", patcnt); } else { /* if table version */ /* --- read table header --- */ fprintf(stderr, "\n"); /* terminate previous log message */ as_chars(attset, recseps, fldseps, blanks, "", comment); in = io_hdr(attset, fn_hdr, fn_in, inflags, 1); if (!in) error(1); /* read the table header */ 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 */ fprintf(stderr,"done.\n");/* and print a success message */ in = io_hdr(attset, fn_hdr, fn_in, inflags, 1); if (!in) error(1); /* reread the table header */ } /* (necessary because of first tuple) */ /* --- process data tuples --- */ if (fn_out && *fn_out) /* if a file name is given, */ out = fopen(fn_out, "w"); /* open the file for writing */ else { /* if no output file is given, */ out = stdout; fn_out = "<stdout>"; } /* use std. output */ if (!out) error(E_FOPEN, fn_out); k = 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 */ 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 */ tplcnt++; /* count tuple and sum its weight */ tplwgt += as_getwgt(attset); cls_valuex(clset, NULL); /* set the data from a tuple */ if (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, */ tse = as_err(attset); /* get the error information */ tplcnt += (inflags & (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 */ if (out != stdout) { /* if not written to stdout, */ i = fclose(out); out = NULL; /* close the output file */ if (i) error(E_FWRITE, fn_out); } /* print a success message */ fprintf(stderr, "[%d/%g tuple(s)] done.\n", tplcnt, tplwgt); } /* if (matinp) .. else .. */ /* --- clean up --- */ #ifndef NDEBUG if (!matinp) /* if table version */ cls_deletex(clset, 1); /* delete the cluster set */ else { /* if matrix version */ cls_delete(clset); /* delete the cluster set */ ts_delete(tscan); /* and the table scanner */ } #endif return 0; /* return 'ok' */} /* main() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -