Chinaunix
标题:
C语言 词频统计
[打印本页]
作者:
laoliuya
时间:
2013-09-17 16:21
标题:
C语言 词频统计
任意一段英文,写一个算法,统计这段英文中的任意单词的词频,并按频率高低顺序打印出来。
想了半天没写出来,新人求高人指点
作者:
fender0107401
时间:
2013-09-17 17:15
lz懂数据结构吗?
作者:
laoliuya
时间:
2013-09-17 18:53
回复
2#
fender0107401
懂点吧。这就是数据结构的题目。。。
作者:
laoliuya
时间:
2013-09-18 09:39
怎么没人啊
作者:
shentar
时间:
2013-09-18 10:48
我正好写过这道题目,不过重点不在排序上面,排序写得很糟糕。
http://shentar.me/%E4%B8%80%E9%8 ... %E6%9E%90%E9%A2%98/
作者:
laoliuya
时间:
2013-09-18 21:29
回复
5#
shentar
太复杂了,看不懂
作者:
windoze
时间:
2013-09-18 23:14
你得先定义下什么叫“单词”,比如“100”算不算“单词”?“C++”算不算“单词”?“AT&T”算几个“单词”?拼错的算不算“单词”?“こんにちは means hello”这句话有几个“单词”?
作者:
linux_c_py_php
时间:
2013-09-18 23:17
基础题目还是好好上学吧.
作者:
lost_templar
时间:
2013-09-18 23:47
map<string, unsigned long>
作者:
沧海游龙天下
时间:
2013-09-20 01:30
很简单,定义一个char型的数组,例如char a[128],注意,必须是128,和ASII表数目对应,然后遇到字母就以它为下表的元素加1,值到全部弄完,然后打印数组元素大小不为0的,就是出现过的,下表是字母,大小是出现次数。
作者:
cobras
时间:
2013-09-20 08:56
下面的算法按空格分词。
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_TYPE 1024
#define FILE_NAME "cntword.c"
struct word
{
const char *word;
int word_len;
int count;
};
char *read_file(const char *file, int *o_len)
{
FILE *fp;
char *buf;
long len;
fp = fopen(file, "rb");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
buf = (char *)malloc(len);
if (buf != NULL) {
if (fread(buf, len, 1, fp) == 1) {
*o_len = len;
fclose(fp);
return buf;
}
free(buf);
}
fclose(fp);
}
return NULL;
}
int nstr_ltrim(const char *s, int off, int lmt)
{
int i;
for (i = off; i < lmt; ++i) {
if (!isspace(s[i])) {
break;
}
}
return i;
}
int nstr_get_next_space(const char *s, int off, int lmt)
{
int i;
for (i = off; i < lmt; ++i) {
if (isspace(s[i])) {
break;
}
}
return i;
}
int get_word(const char *s, int *io_off, int lmt, int *o_word_off, int *o_word_lmt)
{
int off;
int word_off;
int word_lmt;
off = *io_off;
off = nstr_ltrim(s, off, lmt);
if (off < lmt) {
word_off = off;
off = nstr_get_next_space(s, off, lmt);
word_lmt = off;
*io_off = off;
*o_word_off = word_off;
*o_word_lmt = word_lmt;
return 0;
}
return -1;
}
int add_word(struct word *table, int table_len, const char *word, int word_len)
{
int i;
for (i = 0; i < table_len; ++i) {
if (table[i].word == NULL) {
break;
}
if (table[i].word_len == word_len && strncmp(table[i].word, word, word_len) == 0) {
++table[i].count;
return 0;
}
}
if (i < table_len) {
table[i].word = word;
table[i].word_len = word_len;
table[i].count = 1;
return 0;
}
return -1;
}
int count_word(char *file, int file_len, struct word *table, int table_len)
{
int file_off;
int word_off, word_lmt;
file_off = 0;
while (get_word(file, &file_off, file_len, &word_off, &word_lmt) == 0) {
add_word(table, table_len, file + word_off, word_lmt - word_off);
}
return 0;
}
int print_word(struct word *table, int table_len)
{
int i;
for (i = 0; i < table_len; ++i) {
if (table[i].word == NULL) {
break;
}
fwrite(table[i].word, 1, table[i].word_len, stdout);
printf(": %d\n", table[i].count);
}
return 0;
}
int get_table_len(struct word *table, int table_len)
{
int i;
for (i = 0; i < table_len; ++i) {
if (table[i].word == NULL) {
break;
}
}
return i;
}
int compare_word(const struct word *word1, const struct word *word2)
{
if (word1->count < word2->count) {
return 1;
}else if (word1->count > word2->count) {
return -1;
}
return 0;
}
int main(void)
{
struct word table[MAX_WORD_TYPE];
char *file;
int len;
int table_len;
file = read_file(FILE_NAME, &len);
if (file != NULL) {
memset(table, 0, sizeof(table));
count_word(file, len, table, MAX_WORD_TYPE);
table_len = get_table_len(table, MAX_WORD_TYPE);
qsort(table, table_len, sizeof(struct word), compare_word);
print_word(table, table_len);
free(file);
return 0;
}
return -1;
}
复制代码
作者:
laoliuya
时间:
2013-09-20 17:36
回复
11#
cobras
哇!辛苦了。我读这估计比较困难。我来仔细读读
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2