/* ÇÑ±Û À½Àý ºóµµ Á¶»ç ÇÁ·Î±×·¥ ÀÔ·Â ÆÄÀÏ : KS ¿Ï¼ºÇü ÅؽºÆ® ÆÄÀÏ(ÇѱÛÀ» Á¦¿ÜÇÑ ¹®ÀÚµéÀº ¹«½ÃÇÔ) ±¹¹Î´ëÇб³ ÄÄÇ»ÅÍÇкΠ°­½Â½Ä */ #include #include #define GET_INDEX(c1, c2) ((c1-0xB0)*94 + (c2-0xA1)) /* '°¡³ª´Ù' ¼øÀ¸·Î Ãâ·Â */ void put_syl_ABC(FILE *fp, unsigned long count[], unsigned long total) { int i, nsyl=0; double percent, accpercent=0.0; fprintf(fp, "À½Àý ÃâÇöȽ¼ö ¹éºÐÀ² ´©Àû¹éºÐÀ²\n"); fprintf(fp, "=====================================\n"); for (i=0; i < 2350; i++) { if (count[i]) { /* 1¹ø ÀÌ»ó ÃâÇöÇÑ °Í¸¸ Ãâ·Â */ percent = (double)count[i] / (double)total * 100.0; accpercent += percent; fprintf(fp, "%c%c %10ld %10.4f %10.4f\n", i/94+0xB0, (i%94)+0xA1, count[i], percent, accpercent); nsyl++; } } fprintf(fp, "=====================================\n"); fprintf(fp, "À½Àý °³¼ö = %ld, ÃÑ À½Àý¼ö = %ld\n", nsyl, total); } /* '°¡³ª´Ù' ¼ø¼­ && header ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ void put_syl_ABC_header(FILE *fp, unsigned long count[], unsigned long total) { int i, nsyl=0; double percent, accpercent=0.0; fprintf(fp, "#define N_TOTAL\t%ld\n\n", total); fprintf(fp, "unsigned long SYL_FREQ[25][94] = { {\n"); for (i=0; i < 2350; i++) { if (i != 2349) { fprintf(fp, "\t%6ld,\t/*%c%c*/\n", count[i], i/94+0xB0, i%94+0xA1); if (i % 94 == 93) printf("}, {\n"); } else fprintf(fp, "\t%6ld\t/*%c%c*/\n} };\n", count[i], i/94+0xB0, i%94+0xA1); } } double put_one_syllable(FILE *fp, unsigned long count[], int i, long total, double accpercent) { double percent; percent = (double)count[i] / (double)total * 100.0; accpercent += percent; fprintf(fp, "%c%c %10ld %10.4f %10.4f\n", i/94+0xB0, (i%94)+0xA1, count[i], percent, accpercent); return accpercent; } /* °íºóµµ ¼øÀ¸·Î Ãâ·Â */ void put_syl_sort(FILE *fp, unsigned long count[], unsigned long total) { int i, j, temp; int nsyl=0; double accpercent=0.0; int index[2350]; for (i = 0; i < 2350; i++) index[i] = i; for (i = 0; i < 2350-1; i++) { /* °íºóµµ ¼øÀ¸·Î sorting */ for (j = i+1; j < 2350; j++) { if (count[index[i]] < count[index[j]]) { temp = index[i]; index[i] = index[j]; index[j] = temp; } } } fprintf(fp, "À½Àý ÃâÇöȽ¼ö ¹éºÐÀ² ´©Àû¹éºÐÀ²\n"); fprintf(fp, "=====================================\n"); for (i = 0; i < 2350; i++) { if (count[index[i]]) { /* 1¹ø ÀÌ»ó ÃâÇöÇÑ °Í¸¸ Ãâ·Â */ accpercent = put_one_syllable(fp, count, index[i], total, accpercent); nsyl++; } } fprintf(fp, "=====================================\n"); fprintf(fp, "À½Àý °³¼ö = %ld, ÃÑ À½Àý¼ö = %ld\n", nsyl, total); } /* °íºóµµ ¼ø¼­ && header ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ void put_syl_sort_header(FILE *fp, unsigned long count[], unsigned long total) { int i, j, temp; int nsyl=0; double accpercent=0.0; int index[2350]; for (i = 0; i < 2350; i++) index[i] = i; for (i = 0; i < 2350-1; i++) { for (j = i+1; j < 2350; j++) { if (count[index[i]] < count[index[j]]) { temp = index[i]; index[i] = index[j]; index[j] = temp; } } } fprintf(fp, "#define N_TOTAL\t%ld\n\n", total); fprintf(fp, "unsigned SYL_FREQ[25][94][2] = { {\n"); for (i=0; i < 2350; i++) { if (i != 2349) { fprintf(fp, "\t{ 0x%x%x, %6ld },\t/*%c%c*/\n", index[i]/94+0xB0, index[i]%94+0xA1, count[index[i]], index[i]/94+0xB0, index[i]%94+0xA1); if (i % 94 == 93) printf("}, {\n"); } else fprintf(fp, "\t{ 0x%x%x, %6ld }\t/*%c%c*/\n} };\n", index[i]/94+0xB0, index[i]%94+0xA1, count[index[i]], index[i]/94+0xB0, index[i]%94+0xA1); } } int main(int argc, char *argv[]) { int i, j, k=1, c1, c2, outflag=0; unsigned short syl; FILE *fpin=stdin, *fpout=stdout; unsigned long total_syllable=0; unsigned long count[2350] = { 0 }; if (argc == 1 || !strcmp(argv[1], "-h")) { puts("C> run [-s|-s2|-n2] "); return 0; } if (argv[1][0] == '-') { k = 2; if (!strcmp(argv[1], "-s")) outflag = 1; /* ºóµµ¼øÀ¸·Î ¼ÒÆà */ else if (!strcmp(argv[1], "-s2")) outflag = 2; /* ºóµµ¼øÀ¸·Î ¼ÒÆà && Çì´õ ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ else if (!strcmp(argv[1], "-n2")) outflag = 3; /* °¡³ª´Ù¼ø && Çì´õ ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ } if (k < argc) { if (!(fpin = fopen(argv[k++], "r"))) return -1; } if (k < argc) { if (fpout = fopen(argv[k], "r")) { fclose(fpout); fprintf(stderr, "File <%s> exists. Overwrite it? ", argv[k]); if (getchar() != 'y') return 0; } fpout = fopen(argv[k], "w"); } c1 = getc(fpin); while(c1 != EOF) { if (c1 & 0x80) { c2 = getc(fpin); if (c1 >= 0xB0 && c1 <= 0xC8 && c2 >= 0xA1 && c2 <= 0xFE) { syl = GET_INDEX(c1, c2); count[syl]++; total_syllable++; } } c1 = getc(fpin); } switch (outflag) { case 0: /* °¡³ª´Ù¼øÀ¸·Î Ãâ·Â */ put_syl_ABC(fpout, count, total_syllable); break; case 1: /* ºóµµ¼øÀ¸·Î ¼ÒÆà */ put_syl_sort(fpout, count, total_syllable); break; case 2: /* ºóµµ¼øÀ¸·Î ¼ÒÆà && Çì´õ ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ put_syl_sort_header(fpout, count, total_syllable); break; case 3: /* °¡³ª´Ù¼ø && Çì´õ ÆÄÀÏ ÇüÅ·ΠÃâ·Â */ put_syl_ABC_header(fpout, count, total_syllable); break; } if (fpin != stdin) fclose(fpin); if (fpout!= stdout) fclose(fpout); return 0; }