题目:海量数据处理,从大文件中找出频数最高的词

题目:有一个1G大小的一个文件,里面每一行是一个词,词的大小不超过16字节,内存限制大小是1M。返回频数最高的100个词.

首先,我们看到这个题目应该做一下计算,大概的计算,因为大家都清楚的知道1G的文件不可能用1M的内存空间处理。所以我们要按照1M的上线来计算,假设每个单词都为16个字节,那么1M的内存可以处理多少个单词呢? 1M = 1024 KB = 1024 * 1024 B 。然后1M / 16B = 2^16个单词,那么1G大概有多少个单词呢? 有2^26个单词,但是实际中远远不止这些,因为我们是按照最大单词长度算的。我们需要把这1G的单词分批处理,根据上面的计算,可以分成大于2^10个文件。索性就分成2000个文件吧,怎么分呢,不能随便分,不能简单的按照单词的顺序然后模2000划分,因为这样有可能相同的单词被划分到不同的文件中去了。这样在统计个数的时候被当成的不同的单词,因为我们没有能力把在不同文件中相同单词出现的次数跨越文件的相加,这就迫使我们要把不同序号的同一个单词划分到同一个文件中:应用hash统计吧。稍后代码会给出方法。然后呢,我们队每个文件进行分别处理。按照key-value的方法处理每个单词,最终得出每个文件中包含每个单词和单词出现的次数。然后再建立大小为100的小根堆。一次遍历文件进行处理。我没有弄1G的文件,弄1M的,简单的实现了一下,不过原理就是这样的。这是单词:http://download.csdn.net/detail/zzran/4934173

  1. #include<iostream>  
  2. #include<string>  
  3. using namespace std;  
  4.   
  5. #define FILE_NUM 10  
  6. #define WORDLEN 30  
  7. #define HASHLEN 7303  
  8.   
  9. typedef struct node_no_space{  
  10.     char *word;  
  11.     int count;  
  12.     struct node_no_space *next;  
  13. }node_no_space, *p_node_no_space;  
  14.   
  15. typedef struct node_has_space{  
  16.     char word[WORDLEN];  
  17.     int count;  
  18.     struct node_has_space *next;  
  19. }node_has_space, *p_node_has_space;  
  20.   
  21. p_node_no_space bin[HASHLEN] = {NULL};   
  22.   
  23. void swap(int *a, int *b) {  
  24.     int temp;  
  25.     temp = *a;  
  26.     *a = *b;  
  27.     *b = temp;  
  28. }  
  29.   
  30. unsigned int hash(char *p_word) {  
  31.     unsigned int index = 0;  
  32.     while(*p_word) {  
  33.         index += index * 31 + *p_word;  
  34.         p_word++;  
  35.     }  
  36.     return index % HASHLEN;  
  37. }  
  38.   
  39. int trim_word(char *word) {  
  40.     int n = strlen(word) - 1;  
  41.     int i = 0;  
  42.     if(n < 0)  
  43.         return 0;  
  44.     while(word[n] < '0' || (word[n] > '9' && word[n] < 'A') || (word[n] > 'Z' && word[n] < 'a') || word[n] > 'z') {  
  45.         word[n] = '\0';  
  46.         n--;  
  47.     }  
  48.     if(n < 0)  
  49.         return 0;  
  50.     while(word[i] < '0' || (word[i] > '9' && word[i] < 'A') || (word[i] > 'Z' && word[i] < 'a') || word[i] > 'z') {  
  51.         i++;  
  52.     }  
  53.     strcpy(word, word + i);  
  54.     return 1;  
  55. }  
  56.   
  57. void insert_word(char *p_word) {  
  58.     unsigned int index = hash(p_word);  
  59.     node_no_space *p;  
  60.     for(p = bin[index]; p != NULL; p = p->next) {  
  61.         if(strcmp(p_word, p->word) == 0) {  
  62.             (p->count)++;  
  63.             return;  
  64.         }  
  65.     }  
  66.   
  67.     p = (node_no_space*)malloc(sizeof(node_no_space));  
  68.     p->count = 1;  
  69.     p->word = (char*)malloc(strlen(p_word) + 1);  
  70.     strcpy(p->word, p_word);  
  71.     p->next = bin[index];  
  72.     bin[index] = p;  
  73. }  
  74.   
  75. void min_heap(node_has_space *heap, int i, int len) {  
  76.     int left = 2 * i;  
  77.     int right = 2 * i + 1;  
  78.     int min_index = 0;  
  79.   
  80.     if(left <= len && heap[left].count < heap[i].count) {  
  81.         min_index = left;  
  82.     } else {  
  83.         min_index = i;  
  84.     }  
  85.   
  86.     if(right <= len && heap[right].count < heap[min_index].count) {  
  87.         min_index = right;  
  88.     }  
  89.     if(min_index != i) {  
  90.         swap(&heap[min_index].count, &heap[i].count);  
  91.         char buffer[WORDLEN];  
  92.         strcpy(buffer, heap[min_index].word);  
  93.         strcpy(heap[min_index].word, heap[i].word);  
  94.         strcpy(heap[i].word, buffer);  
  95.         min_heap(heap, min_index, len);  
  96.     }  
  97. }  
  98.   
  99. void build_min_heap(node_has_space *heap, int len) {  
  100.     int index = len / 2;  
  101.     int i;  
  102.     for(i = index; i >= 1; i--) {  
  103.         min_heap(heap, i, len);  
  104.     }  
  105. }  
  106.   
  107. void destroy_bin() {  
  108.     node_no_space *p, *q;  
  109.     int i = 0;  
  110.     while(i < HASHLEN) {  
  111.         p = bin[i];  
  112.         while(p) {  
  113.             q = p->next;  
  114.             if(p->word) {  
  115.                 free(p->word);  
  116.                 p->word = NULL;  
  117.             }  
  118.             free(p);  
  119.             p = NULL;  
  120.             p = q;  
  121.         }  
  122.         bin[i] = NULL;  
  123.         i++;  
  124.     }  
  125. }  
  126.   
  127. void write_to_file(char *path) {  
  128.     FILE *out;  
  129.     if((out = fopen(path, "w")) == NULL) {  
  130.         cout << "error, open " << path << " failed!" << endl;  
  131.         return;  
  132.     }  
  133.     int i;  
  134.     node_no_space *p;  
  135.     i = 0;  
  136.     while(i < HASHLEN) {  
  137.         for(p = bin[i]; p != NULL; p = p->next) {  
  138.             fprintf(out, "%s %d\n", p->word, p->count);  
  139.         }  
  140.         i++;  
  141.     }  
  142.     fclose(out);  
  143.     destroy_bin();  
  144. }  
  145.   
  146. void main() {  
  147.     char word[WORDLEN];  
  148.     char path[20];  
  149.     int count;  
  150.     int n = 10;  
  151.     unsigned int index = 0;  
  152.     int i;  
  153.     FILE *fin[10];  
  154.     FILE *fout;  
  155.     FILE *f_message;  
  156.     node_has_space *heap = (node_has_space*)malloc(sizeof(node_has_space) * (n + 1));  
  157.     // divide word into n files  
  158.     if((f_message = fopen("words.txt""r")) == NULL) {  
  159.         cout << "error, open source file failed!" << endl;  
  160.         return;  
  161.     }  
  162.     for(i = 0; i < n; i++) {  
  163.         sprintf(path, "tmp%d.txt", i);  
  164.         fin[i] = fopen(path, "w");  
  165.     }  
  166.     while(fscanf(f_message, "%s", word) != EOF) {  
  167.         if(trim_word(word)) {  
  168.             index = hash(word) % n;  
  169.             fprintf(fin[index], "%s\n", word);  
  170.         }  
  171.     }  
  172.     for(i = 0; i < n; i++) {  
  173.         fclose(fin[i]);  
  174.     }  
  175.     // do hash count  
  176.     for(i = 0; i < n; i++) {  
  177.         sprintf(path, "tmp%d.txt", i);  
  178.         fin[i] = fopen(path, "r");  
  179.         while(fscanf(fin[i], "%s", word) != EOF) {  
  180.             insert_word(word);  
  181.         }  
  182.         fclose(fin[i]);  
  183.         write_to_file(path);  
  184.     }  
  185.     // heap find   
  186.     for(i = 1; i <= n; i++) {  
  187.         strcpy(heap[i].word, "");  
  188.         heap[i].count = 0;  
  189.     }  
  190.     build_min_heap(heap, n);  
  191.     for(i = 0; i < n; i++) {  
  192.         sprintf(path, "tmp%d.txt", i);  
  193.         fin[i] = fopen(path, "r");  
  194.         while(fscanf(fin[i], "%s %d", word, &count) != EOF) {  
  195.             if(count > heap[1].count) {  
  196.                 heap[1].count = count;  
  197.                 strcpy(heap[1].word, word);  
  198.                 min_heap(heap, 1, n);  
  199.             }  
  200.         }  
  201.         fclose(fin[i]);  
  202.     }  
  203.   
  204.     for(i = 1; i <= n; i++)  
  205.         cout << heap[i].word << ":" << heap[i].count << endl;  
  206. }  


运行结果,虽然与用文本文件查找的有差别,但是还是差不了多少的,因为文件中有不规范的单词。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值