免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: laoliuya
打印 上一主题 下一主题

[算法] C语言 词频统计 [复制链接]

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
11 [报告]
发表于 2013-09-20 08:56 |只看该作者
下面的算法按空格分词。
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. #define MAX_WORD_TYPE       1024
  6. #define FILE_NAME           "cntword.c"

  7. struct word
  8. {
  9.         const char *word;
  10.         int word_len;
  11.         int count;
  12. };

  13. char *read_file(const char *file, int *o_len)
  14. {
  15.         FILE *fp;
  16.         char *buf;
  17.         long len;

  18.         fp = fopen(file, "rb");
  19.         if (fp != NULL) {
  20.                 fseek(fp, 0, SEEK_END);
  21.                 len = ftell(fp);
  22.                 fseek(fp, 0, SEEK_SET);
  23.                 buf = (char *)malloc(len);
  24.                 if (buf != NULL) {
  25.                         if (fread(buf, len, 1, fp) == 1) {
  26.                                 *o_len = len;
  27.                                 fclose(fp);
  28.                                 return buf;
  29.                         }
  30.                         free(buf);
  31.                 }
  32.                 fclose(fp);
  33.         }
  34.         return NULL;
  35. }

  36. int nstr_ltrim(const char *s, int off, int lmt)
  37. {
  38.         int i;

  39.         for (i = off; i < lmt; ++i) {
  40.                 if (!isspace(s[i])) {
  41.                         break;
  42.                 }
  43.         }
  44.         return i;
  45. }

  46. int nstr_get_next_space(const char *s, int off, int lmt)
  47. {
  48.         int i;
  49.        
  50.         for (i = off; i < lmt; ++i) {
  51.                 if (isspace(s[i])) {
  52.                         break;
  53.                 }
  54.         }
  55.         return i;
  56. }

  57. int get_word(const char *s, int *io_off, int lmt, int *o_word_off, int *o_word_lmt)
  58. {
  59.         int off;
  60.         int word_off;
  61.         int word_lmt;

  62.         off = *io_off;
  63.         off = nstr_ltrim(s, off, lmt);
  64.         if (off < lmt) {
  65.                 word_off = off;
  66.                 off = nstr_get_next_space(s, off, lmt);
  67.                 word_lmt = off;
  68.                 *io_off = off;
  69.                 *o_word_off = word_off;
  70.                 *o_word_lmt = word_lmt;
  71.                 return 0;
  72.         }
  73.         return -1;
  74. }

  75. int add_word(struct word *table, int table_len, const char *word, int word_len)
  76. {
  77.         int i;

  78.         for (i = 0; i < table_len; ++i) {
  79.                 if (table[i].word == NULL) {
  80.                         break;
  81.                 }
  82.                 if (table[i].word_len == word_len && strncmp(table[i].word, word, word_len) == 0) {
  83.                         ++table[i].count;
  84.                         return 0;
  85.                 }
  86.         }
  87.         if (i < table_len) {
  88.                 table[i].word = word;
  89.                 table[i].word_len = word_len;
  90.                 table[i].count = 1;
  91.                 return 0;
  92.         }
  93.         return -1;
  94. }

  95. int count_word(char *file, int file_len, struct word *table, int table_len)
  96. {
  97.         int file_off;
  98.         int word_off, word_lmt;

  99.         file_off = 0;
  100.         while (get_word(file, &file_off, file_len, &word_off, &word_lmt) == 0) {
  101.                 add_word(table, table_len, file + word_off, word_lmt - word_off);
  102.         }
  103.         return 0;
  104. }

  105. int print_word(struct word *table, int table_len)
  106. {
  107.         int i;

  108.         for (i = 0; i < table_len; ++i) {
  109.                 if (table[i].word == NULL) {
  110.                         break;
  111.                 }
  112.                 fwrite(table[i].word, 1, table[i].word_len, stdout);
  113.                 printf(": %d\n", table[i].count);
  114.         }
  115.         return 0;
  116. }

  117. int get_table_len(struct word *table, int table_len)
  118. {
  119.         int i;

  120.         for (i = 0; i < table_len; ++i) {
  121.                 if (table[i].word == NULL) {
  122.                         break;
  123.                 }
  124.         }
  125.         return i;
  126. }

  127. int compare_word(const struct word *word1, const struct word *word2)
  128. {
  129.         if (word1->count < word2->count) {
  130.                 return 1;
  131.         }else if (word1->count > word2->count) {
  132.                 return -1;
  133.         }
  134.         return 0;
  135. }

  136. int main(void)
  137. {
  138.         struct word table[MAX_WORD_TYPE];
  139.         char *file;
  140.         int len;
  141.         int table_len;

  142.         file = read_file(FILE_NAME, &len);
  143.         if (file != NULL) {
  144.                 memset(table, 0, sizeof(table));
  145.                 count_word(file, len, table, MAX_WORD_TYPE);
  146.                 table_len = get_table_len(table, MAX_WORD_TYPE);
  147.                 qsort(table, table_len, sizeof(struct word), compare_word);
  148.                 print_word(table, table_len);
  149.                 free(file);
  150.                 return 0;
  151.         }
  152.         return -1;
  153. }
复制代码

论坛徽章:
0
12 [报告]
发表于 2013-09-20 17:36 |只看该作者
回复 11# cobras


    哇!辛苦了。我读这估计比较困难。我来仔细读读
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP