词频统计(Word Frequency Count)是自然语言处理(NLP)和文本分析中的一项基础且关键的技术。其基本原理是对一段英文文本进行扫描,提取出所有由字母组成的单词,并统计每个单词在文本中出现的次数。
该技术广泛应用于多个重要领域:
由此可见,利用C语言实现一个功能完整、运行高效且具备扩展能力的英文词频统计程序,对于掌握文本处理的整体逻辑具有重要意义。
本项目旨在开发一个功能完善的英文词频统计工具,具体要求如下:
the : 135 and : 87 hello : 12 computer : 7
该项目涉及多项核心C语言编程技术:
isalpha()
tolower()
整个程序的执行流程可分为以下几个步骤:
fgetc()
wordbuf
/************************************************************
* C语言实现英文词频统计(Word Frequency Count)
* 结构:哈希表 + 链地址法
* 文件:word_count.c
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define HASH_SIZE 10007 // 哈希表大小(质数效果更好)
#define WORD_MAX_LEN 256 // 单词最大长度
/************************************************************
* 单词节点结构体:用于链表存储哈希冲突项
************************************************************/
typedef struct WordNode {
char* word; // 单词
int count; // 出现次数
struct WordNode* next; // 下一个节点
} WordNode;
/************************************************************
* 哈希表(链地址法)
************************************************************/
WordNode* hashtable[HASH_SIZE];
/************************************************************
* BKDR Hash 哈希函数
************************************************************/
unsigned int BKDRHash(const char* str)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*str) {
hash = hash * seed + (*str++);
}
return hash % HASH_SIZE;
}
/************************************************************
* 向哈希表插入一个单词(若存在则 count++)
************************************************************/
void insert_word(const char* word)
{
unsigned int index = BKDRHash(word);
WordNode* p = hashtable[index];
while (p) {
if (strcmp(p->word, word) == 0) {
p->count++;
return;
}
p = p->next;
}
WordNode* newNode = (WordNode*)malloc(sizeof(WordNode));
newNode->word = strdup(word);
newNode->count = 1;
newNode->next = hashtable[index];
hashtable[index] = newNode;
}
/************************************************************
* 从文件读取单词并统计
************************************************************/
void count_words(const char* filename)
{
FILE* fp = fopen(filename, "r");
if (!fp) {
printf("无法打开文件: %s\n", filename);
return;
}
char ch;
char wordbuf[WORD_MAX_LEN];
int wlen = 0;
while ((ch = fgetc(fp)) != EOF) {
if (isalpha(ch)) {
if (wlen < WORD_MAX_LEN - 1) {
wordbuf[wlen++] = tolower(ch);
}
} else {
if (wlen > 0) {
wordbuf[wlen] = '\0';
insert_word(wordbuf);
wlen = 0;
}
}
}
if (wlen > 0) {
wordbuf[wlen] = '\0';
insert_word(wordbuf);
}
fclose(fp);
}
/************************************************************
* 排序结构
************************************************************/
typedef struct {
char* word;
int count;
} WordEntry;
int cmp_count(const void* a, const void* b)
{
return ((WordEntry*)b)->count - ((WordEntry*)a)->count;
}
int cmp_alpha(const void* a, const void* b)
{
return strcmp(((WordEntry*)a)->word, ((WordEntry*)b)->word);
}
/************************************************************
* 打印所有词频(按词频或字典序排序)
************************************************************/
void print_words(int sort_mode)
{
WordEntry* arr = NULL;
int total = 0;
for (int i = 0; i < HASH_SIZE; i++) {
WordNode* p = hashtable[i];
while (p) {
total++;
p = p->next;
}
}
arr = (WordEntry*)malloc(sizeof(WordEntry) * total);
int idx = 0;
for (int i = 0; i < HASH_SIZE; i++) {
WordNode* p = hashtable[i];
while (p) {
arr[idx].word = p->word;
arr[idx].count = p->count;
idx++;
p = p->next;
}
}
if (sort_mode == 0)
qsort(arr, total, sizeof(WordEntry), cmp_alpha);
else
qsort(arr, total, sizeof(WordEntry), cmp_count);
for (int i = 0; i < total; i++) {
printf("%s : %d\n", arr[i].word, arr[i].count);
}
free(arr);
}
/************************************************************
* 主函数:接受文件名并统计
************************************************************/
int main()
{
char filename[256];
printf("请输入英文文本文件名:");
scanf("%s", filename);
count_words(filename);
printf("\n--- 按词频排序 ---\n");
print_words(1);
printf("\n--- 按字典序排序 ---\n");
print_words(0);
return 0;
}
本项目通过C语言实现了一个高性能、结构清晰的英文词频统计器,涵盖了文本解析、字符处理、哈希表设计、内存管理及排序输出等多项核心技术。不仅实现了基础功能,还具备良好的可维护性和扩展潜力,适合作为教学案例或嵌入式文本分析模块的基础原型。
通过对该程序的理解与实践,开发者能够深入掌握C语言在实际工程问题中的应用方式,尤其在资源受限环境下如何平衡效率与功能的设计思路。
本项目构建了一个功能完整的英文词频统计系统,具备以下特点:
系统采用多种高效技术实现核心功能:
利用哈希查找的时间复杂度为 O(1),在单词插入与检索过程中表现出极高的处理速度。
the : 135 and : 87 hello : 12 computer : 7
系统架构支持后续功能拓展,例如:
该项目是深入理解以下技术方向的良好实践基础:
isalpha()tolower()
扫码加好友,拉您进群



收藏
