python问题描述】统计中文文档中词频信息 【输入形式】 【输出形式】最多的前十位词汇和次数的信息
时间: 2023-12-04 07:03:10 浏览: 28
可以使用Python中的jieba库来进行中文分词和统计词频。
以下是一个示例代码:
```python
import jieba
from collections import Counter
# 读取文件
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 分词
words = jieba.cut(text)
# 统计词频
word_counts = Counter(words)
# 输出前十位词汇和次数的信息
for word, count in word_counts.most_common(10):
print(word, count)
```
其中,`jieba.cut()`函数可以将输入的文本进行分词,返回一个可迭代的生成器对象。`Counter()`函数可以统计生成器中每个元素出现的次数,并返回一个字典对象,其中键为元素,值为出现次数。`most_common()`方法可以返回字典中出现次数最多的前n个元素,以列表形式返回,其中每个元素是一个元组,第一个元素为键,第二个元素为值。
相关问题
python【问题描述】统计中文文档中词频信息 【输入形式】 【输出形式】最多的前十位词汇和次数的信息
以下是一个简单的 Python 程序,可以实现输入中文文档,统计词频信息,并输出最多的前十位词汇和次数的信息:
```python
import jieba
from collections import Counter
# 读取文档
with open('document.txt', 'r', encoding='utf-8') as f:
document = f.read()
# 分词,过滤停用词
words = [w for w in jieba.lcut(document) if w.strip() and w not in {',', '。', '!', '?', '\n'}]
# 统计词频
word_count = Counter(words)
# 输出最多的前十位词汇和次数的信息
most_common = word_count.most_common(10)
for word, count in most_common:
print(f'{word}\t{count}')
```
其中,需要注意以下几点:
1. 需要使用 `jieba` 库进行中文分词,需要先安装该库。
2. 可以根据实际情况自定义停用词列表,这里只是简单地过滤了一些常见的标点符号和换行符。
3. 输出格式可以根据实际需要进行调整,这里使用了 f-string 进行格式化输出。
希望这个程序能够帮助到你。
trie树统计词频输出前n个次数最多的,C语言
以下是使用 Trie 树实现词频统计并输出前 n 个出现次数最多的单词的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_LEN 50
#define MAX_N 100
// 定义 Trie 树结点
typedef struct trie_node {
int count; // 记录单词出现次数
struct trie_node* next[26]; // 指向下一个字符的指针数组
} trie_node;
// 插入单词到 Trie 树中
void insert_trie(trie_node* root, char* word) {
trie_node* cur = root;
for (int i = 0; i < strlen(word); i++) {
int index = word[i] - 'a';
if (!cur->next[index]) {
cur->next[index] = (trie_node*)malloc(sizeof(trie_node));
cur->next[index]->count = 0;
for (int j = 0; j < 26; j++) {
cur->next[index]->next[j] = NULL;
}
}
cur = cur->next[index];
}
cur->count++;
}
// 比较函数,用于排序
int cmp(const void* a, const void* b) {
return (*(trie_node**)b)->count - (*(trie_node**)a)->count;
}
// 获取 Trie 树中前 n 个出现次数最多的单词
void get_top_n(trie_node* root, trie_node** top_n, int* n) {
if (!root) {
return;
}
// 遍历 Trie 树,找到出现次数前 n 大的单词
for (int i = 0; i < 26; i++) {
if (root->next[i]) {
trie_node* cur = root->next[i];
if (cur->count > top_n[*n - 1]->count) {
top_n[*n - 1] = cur;
qsort(top_n, *n, sizeof(trie_node*), cmp);
}
get_top_n(cur, top_n, n);
}
}
}
// 打印前 n 个出现次数最多的单词
void print_top_n(trie_node** top_n, int n) {
printf("Top %d words:\n", n);
for (int i = 0; i < n; i++) {
if (top_n[i]->count == 0) {
break;
}
printf("%d: ", i + 1);
trie_node* cur = top_n[i];
char word[MAX_WORD_LEN];
int len = 0;
// 从 Trie 树中回溯到根节点,构造出单词
while (cur != NULL) {
word[len++] = 'a' + (cur - cur->next[0]);
cur = cur->parent;
}
for (int j = len - 2; j >= 0; j--) {
printf("%c", word[j]);
}
printf(" (%d occurrences)\n", top_n[i]->count);
}
}
int main() {
trie_node* root = (trie_node*)malloc(sizeof(trie_node));
root->count = 0;
for (int i = 0; i < 26; i++) {
root->next[i] = NULL;
}
// 读取文件中的单词并插入到 Trie 树中
FILE* fp = fopen("input.txt", "r");
char word[MAX_WORD_LEN];
while (fscanf(fp, "%s", word) != EOF) {
insert_trie(root, word);
}
fclose(fp);
// 获取前 n 个出现次数最多的单词
int n = 10;
trie_node** top_n = (trie_node**)malloc(n * sizeof(trie_node*));
for (int i = 0; i < n; i++) {
top_n[i] = root;
}
get_top_n(root, top_n, &n);
// 打印结果
print_top_n(top_n, n);
// 释放内存
for (int i = 0; i < n; i++) {
free(top_n[i]);
}
free(top_n);
return 0;
}
```
该代码使用 Trie 树统计文本中单词出现次数,并输出前 n 个出现次数最多的单词。其中,将单词插入到 Trie 树中的函数 `insert_trie` 使用了循环遍历单词的每一个字符,依次将其插入到 Trie 树中。获取前 n 个出现次数最多的单词的函数 `get_top_n` 使用了深度优先遍历 Trie 树的方法,递归查找出现次数前 n 大的单词。在打印前 n 个出现次数最多的单词时,将每个单词从 Trie 树的叶子节点回溯到根节点,构造出单词并打印。注意,该代码假定输入文件中所有单词都是小写字母构成的,且单词长度不超过 50。