词频统计(word frequency counting)是自然语言处理(NLP)领域中一种常用的技术。它的原理是统计一个文本中每个词出现的次数,并按照词频降序排列。
词频统计的基本流程如下:
分词:将文本分解为独立的词语。
去重:去掉重复的词语,以保证统计的词语数量是唯一的。
统计:统计每个词语出现的次数。
排序:将词语按照词频降序排列。
词频统计是一种简单但有效的方法,常用于文本挖掘,文本分类,情感分析等NLP任务中。它可以提供有关文本内容的重要信息,并帮助研究人员更好地理解文本内容。
下面是一种实现方案。实际上,英文词频识别的难度要远低于中文。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORDS 100 // 单词的最大数量
#define MAX_WORD_LENGTH 50 // 单词的最大长度
struct Word { // 单词结构体
char text[MAX_WORD_LENGTH]; // 单词的内容
int count; // 单词的出现次数
};
// 读取单词的函数
int read_word(char *word, int max_word_length, FILE *file) {
int c, len = 0; // c 为当前读入的字符,len 为已读入字符的数量
// 跳过非字母字符
while ((c = getc(file)) != EOF && !isalpha(c))
;
if (c == EOF) {
return EOF;
}
// 读入字母字符
word[len++] = (char)c;
while ((c = getc(file)) != EOF && isalpha(c) && len < max_word_length - 1) {
word[len++] = (char)c;
}
word[len] = '\0';
return len;
}
int main(void) {
char word[MAX_WORD_LENGTH]; // 当前读入的单词
struct Word words[MAX_WORDS]; // 存储单词的数组
int num_words = 0; // 单词数量
int i, j; // 循环变量
int word_length; // 单词长度
FILE *file = fopen("text.txt", "r"); // 打开文件
// 判断文件是否成功打开
if (file == NULL) {
printf("Failed to open text.txt\n");
return 1;
}
// 循环读取单词
while ((word_length = read_word(word, MAX_WORD_LENGTH, file)) != EOF) {
// 查找单词是否已经存在
for (i = 0; i < num_words; i++) {
if (strcmp(word, words[i].text) == 0) {
break;
}
}
if (i < num_words) {
words[i].count++;
} else {
strcpy(words[num_words].text, word);
words[num_words].count = 1;
num_words++;
}
}
fclose(file);
for (i = 0; i < num_words; i++) {
for (j = i + 1; j < num_words; j++) {
if (words[i].count < words[j].count) {
struct Word temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
for (i = 0; i < num_words; i++) {
printf("%s: %d\n", words[i].text, words[i].count);
}
return 0;
}