编写一个名为 word_count 的函数,统计该文件中包含的各单词词频。输 入参数为字符串,不同单词之间使用空格分开,统计每个单词出现的次数,并将 结果作为字典返回,其中单词为键,次数为值。
时间: 2024-03-18 19:42:32 浏览: 34
好的,可以使用Python中的`split()`方法将输入的字符串按空格分割成单词列表,然后使用`collections`模块中的`Counter`类统计每个单词出现的次数。最后将统计结果以字典的形式返回即可。下面是示例代码:
```python
from collections import Counter
def word_count(sentence):
words = sentence.split()
word_count = Counter(words)
return dict(word_count)
```
其中`sentence`为输入的字符串,`Counter`类可以直接对列表进行统计,返回的是一个字典,字典的键为单词,值为出现次数。
相关问题
C语言编程【问题描述】 编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词出现频率 由高至低输出到指定文件中。
这是一个比较典型的文本处理问题,需要进行字符串的分割和哈希表的统计。具体思路如下:
1. 打开输入文件和输出文件,读取输入文件中的内容。
2. 对输入文件中的内容进行分割,将每个单词存储到哈希表中,并统计每个单词的出现次数。
3. 对哈希表中的单词按照出现频率从高到低进行排序。
4. 将排序后的结果输出到指定文件中。
以下是对应的 C 语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_WORD 128
#define MAX_HASH 10007
typedef struct WordNode {
char* word;
int count;
struct WordNode* next;
} WordNode;
WordNode* hashTable[MAX_HASH] = { NULL };
// 哈希函数
unsigned int hash(char* str) {
unsigned int hash = 0;
while (*str) {
hash = hash * 31 + (*str++);
}
return hash % MAX_HASH;
}
// 插入单词到哈希表中
void insert(char* word) {
unsigned int key = hash(word);
WordNode* node = hashTable[key];
while (node != NULL) {
if (strcmp(node->word, word) == 0) {
node->count++;
return;
}
node = node->next;
}
node = (WordNode*)malloc(sizeof(WordNode));
node->word = (char*)malloc(strlen(word) + 1);
strcpy(node->word, word);
node->count = 1;
node->next = hashTable[key];
hashTable[key] = node;
}
// 释放哈希表中的内存
void freeHashTable() {
for (int i = 0; i < MAX_HASH; i++) {
WordNode* node = hashTable[i];
while (node != NULL) {
WordNode* next = node->next;
free(node->word);
free(node);
node = next;
}
}
}
// 比较函数,用于排序
int compare(const void* a, const void* b) {
WordNode* node1 = *(WordNode**)a;
WordNode* node2 = *(WordNode**)b;
return node2->count - node1->count;
}
int main() {
char inputFileName[MAX_WORD], outputFileName[MAX_WORD];
printf("Input file name: ");
scanf("%s", inputFileName);
printf("Output file name: ");
scanf("%s", outputFileName);
FILE* inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("Failed to open input file.\n");
return 1;
}
char line[MAX_LINE];
while (fgets(line, MAX_LINE, inputFile)) {
char* word = strtok(line, " \t\n\r\f.,:;?!()");
while (word != NULL) {
insert(word);
word = strtok(NULL, " \t\n\r\f.,:;?!()");
}
}
fclose(inputFile);
WordNode* wordList[MAX_HASH];
int count = 0;
for (int i = 0; i < MAX_HASH; i++) {
WordNode* node = hashTable[i];
while (node != NULL) {
wordList[count++] = node;
node = node->next;
}
}
qsort(wordList, count, sizeof(WordNode*), compare);
FILE* outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
printf("Failed to open output file.\n");
freeHashTable();
return 1;
}
for (int i = 0; i < count; i++) {
fprintf(outputFile, "%s %d\n", wordList[i]->word, wordList[i]->count);
}
fclose(outputFile);
freeHashTable();
return 0;
}
```
需要注意的是,这里使用了链表法来解决哈希冲突,同时也需要在程序结束之后释放哈希表中的内存。另外,为了方便起见,这里使用了标准库中的快速排序函数 qsort 来对单词按照出现频率进行排序。
请编写py3程序,对一段英文文本,统计其中所有不同单词的个数,以及词频最大的前10%的单词。 所谓“单词”,是指由不超过80个单词字符组成的连续字符串,但长度超过15的单词将只截取保留前15个单词字符。而合法的“单词字符”为大小写字母、数字和下划线,其它字符均认为是单词分隔符。
好的,以下是一段实现你所需功能的 Python 3 代码,可以将其保存为一个.py文件并执行:
```python
import re
from collections import Counter
def get_words(text):
# 格式化文本,将所有非单词字符替换为空格
text = re.sub(r'\W+', ' ', text)
# 将文本中所有单词转换为小写
words = text.lower().split()
# 截取长度超过15的单词
words = [w[:15] if len(w) > 15 else w for w in words]
return words
def main():
with open('text.txt', 'r') as f:
text = f.read()
words = get_words(text)
word_count = Counter(words)
total_words = len(word_count)
top_words_count = int(total_words * 0.1)
top_words = word_count.most_common(top_words_count)
print(f'Total words: {total_words}')
print(f'Top {top_words_count} words:')
for word, count in top_words:
print(f'{word}: {count}')
if __name__ == '__main__':
main()
```
这段代码使用了正则表达式和Python标准库中的`Counter`类来完成你所需的功能。具体来说,它定义了一个`get_words`函数来将文本中的单词提取出来并进行格式化处理,然后使用`Counter`类来统计单词出现的频率,并找出出现频率最高的前10%的单词。最后,它输出了总单词数和词频最高的前10%单词。
请将代码中的`text.txt`替换为你要统计的文本文件名,并确保该文件与.py文件在同一目录中。