当前位置:首页 > 通信资讯 > 正文

这几天写了一个基于C语言对文本词频进行统计的程序,开发及调试环境:mac集成开发环境Xcode;测试文本,马丁.路德金的《I have a dream》原文演讲稿。

主要运行步骤:

1. 打开文本把文本内容读入流中并且开辟相应空间放入内存
2 .对文本内容进行处理,去除大写字母(转化为小写),去除特殊字符
3. 基于单链表对词频进行统计
4. 把统计结果进行归并排序
5.打印输出全部词频或者频率最高的10个单词和其出现次数
6.释放所有结点消耗的内存

废话不多说,上代码!

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 // // main.c // word_frequency_statistic // // Created by tianling on 14-3-16. // Copyright (c) 2014年 tianling. All rights reserved. // 实现英文文本的词频统计 // #include <stdio.h> #include <stdlib.h> #include <string.h> #define ERROR 1 #define OK 0 const int WORD_LENGTH = 250;//定义单个单词最大长度 typedef int status; /* **定义存储单词及其出现次数的结构体 */ typedef struct Node{ char word[WORD_LENGTH]; int time; struct Node *next; }wordNode; wordNode *headNode = NULL;//定义链表头指针 /* **函数声明 */ wordNode *wordSearch(char *word,int *num); status wordCount(char *word,int *num); void printCountList(int *num); void PrintFirstTenTimes(); void mergeSort(wordNode **head); void FrontBackSplit(wordNode *head,wordNode **pre,wordNode **next); void wordJob(char word[]); wordNode *SortedMerge(wordNode *pre,wordNode *next); void release(); status main(int argc,char *argv[]) { char temp[WORD_LENGTH];//定义用以临时存放单词的数组 FILE *file; int count,articleWordNum = 0;//定义统计结点个数的变量 int *num = &articleWordNum,choose; /* **读取文件 */ if((file = fopen("/Users/tianling/Documents/Data_Structure/word_frequency_statistic/word_frequency_statistic/article.txt", "r")) == NULL){ //这里是绝对路径,基于XCode编译器查找方便的需求 printf("文件读取失败!"); exit(1); } while((fscanf(file,"%s",temp))!= EOF){ wordJob(temp); count = wordCount(temp,num); } fclose(file);//关闭文件 printCountList(num); printf("***********请选择***********\n"); printf("*****1. 输出词频最高的10个词**\n"); printf("*****2. 退出****************\n"); scanf("%d",&choose); if(choose == 1){ mergeSort(&headNode); PrintFirstTenTimes(); }else{ release(); exit(0); } release(); return 0; } /* **查找单词所在结点 */ wordNode *wordSearch(char *word,int *num){ wordNode *node;//声明一个新结点 if(headNode == NULL){//若头结点为空 node = (wordNode*)malloc(sizeof(wordNode)); strcpy(node->word, word);//将第一个单词赋值给这个新结点 node->time = 0;//初始化该单词的出现次数 *num+=1; headNode = node;//将头结点指向这个新结点 return node; } wordNode *nextNode = headNode; wordNode *preNode = NULL; while(nextNode != NULL && strcmp(nextNode->word, word) != 0){ preNode = nextNode; nextNode = nextNode->next; } //若该单词不存在,则在链表中生成新结点 if(nextNode == NULL){ node = (wordNode*)malloc(sizeof(wordNode)); strcpy(node->word, word); node->time = 0; node->next = headNode->next; headNode->next = node; *num+=1; return node; }else return nextNode; } /* **词频统计 */ status wordCount(char *word,int *num){ wordNode *tmpNode = NULL; tmpNode = wordSearch(word,num); if(tmpNode == NULL){ return ERROR; } tmpNode->time++; return 0; } /* **打印所有词频 */ void printCountList(int *num){ if(headNode == NULL){ printf("该文件无内容!"); }else{ wordNode *preNode = headNode; printf("总词量 %d \n",*num); while(preNode != NULL){ printf("%s 出现次数 %d\n",preNode->word,preNode->time); preNode = preNode->next; } } } /* **打印词频最高的10个词 */ void PrintFirstTenTimes(){ if(headNode == NULL){ printf("该文件无内容!"); }else{ wordNode *preNode = headNode; int i = 0; printf("出现次数最多的10个词如下: \n"); while (preNode != NULL && i<=10) { printf("%s 出现次数 %d\n",preNode->word,preNode->time); preNode = preNode->next; i++; } } } /* **对词频统计结果进行归并排序 */ void mergeSort(wordNode **headnode){ wordNode *pre,*next,*head; head = *headnode; //若链表长度为0或1则停止排序 if(head == NULL || head->next == NULL){ return; } FrontBackSplit(head,&pre,&next); mergeSort(&pre); mergeSort(&next); *headnode = SortedMerge(pre,next); } /* **将链表进行分组 */ void FrontBackSplit(wordNode *source,wordNode **pre,wordNode **next){ wordNode *fast; wordNode *slow; if(source == NULL || source->next == NULL){ *pre = source; *next = NULL; }else{ slow = source; fast = source->next; while(fast != NULL){ fast = fast->next; if(fast != NULL){ slow = slow->next; fast = fast->next; } } *pre = source; *next = slow->next; slow->next = NULL; } } /* **根据排序结果更换头结点 */ wordNode *SortedMerge(wordNode *pre,wordNode *next){ wordNode *result = NULL; if(pre == NULL) return next; else if(next == NULL) return pre; if(pre->time >= next->time){ result = pre; result->next = SortedMerge(pre->next,next); }else{ result = next; result->next = SortedMerge(pre,next->next); } return result; } /* **处理大写字母及特殊字符 */ void wordJob(char word[]){ int i,k; char *specialChar = ",.;:'?!><+=|*&^%$#@\"";//定义特殊字符集 for(i = 0;i<strlen(word);i++){ //筛选并将字符串中的大写字母转化为小写字母 if(word[i]>='A'&& word[i]<='Z'){ word[i] += 32; } //筛选并去除字符串中的特殊字符 for(k = 0;k<strlen(specialChar);k++){ if(word[i] == specialChar[k]){ while(i<strlen(word)){ word[i] = word[i+1]; i++; } } } } } /* **释放所有结点内存 */ void release(){ if(headNode == NULL) return; wordNode *pre = headNode; while(pre != NULL){ headNode = pre->next; free(pre); pre = headNode; } }

调试结果:(Xcode环境)

c语言单词词频统计(c语言统计英文文章单词个数)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hjtl1992/article/details/21572521

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。