编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词字典序输出到屏幕上。
时间: 2024-05-03 22:02:14 浏览: 26
好的,可以使用 Python 编写程序来实现该功能。以下是一个示例代码:
```python
import re
# 读取文本文件
with open('example.txt', 'r') as f:
text = f.read()
# 将所有非单词字符替换为空格
text = re.sub(r'\W+', ' ', text)
# 将文本转换为小写
text = text.lower()
# 将文本分割为单词列表
words = text.split()
# 统计每个单词出现的次数
word_count = {}
for word in words:
if word not in word_count:
word_count[word] = 0
word_count[word] += 1
# 按单词字典序排序并输出结果
sorted_words = sorted(word_count.keys())
for word in sorted_words:
print(word, word_count[word])
```
在上面的代码中,我们使用正则表达式将所有非单词字符替换为空格,然后将文本转换为小写,最后将文本分割为单词列表。接着,我们使用一个字典来统计每个单词出现的次数。最后,我们按单词字典序排序并输出结果。
你可以将上述代码保存为一个 Python 文件,并将需要统计的英文文本文件放在与该 Python 文件同一目录下,然后在命令行中运行该 Python 文件即可。
相关问题
用c语言编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词字典序输出到屏幕上。
以下是一个简单的C语言程序,可以实现指定英文文本文件的单词词频统计,并按字典序输出结果。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 100
struct node {
char word[MAX_WORD_LEN];
int count;
struct node *left;
struct node *right;
};
struct node *root = NULL;
void insert_word(char *word) {
struct node *p = root, *prev = NULL;
int res;
while (p != NULL) {
prev = p;
res = strcmp(word, p->word);
if (res < 0) {
p = p->left;
} else if (res > 0) {
p = p->right;
} else {
p->count++;
return;
}
}
struct node *new_node = (struct node *)malloc(sizeof(struct node));
strcpy(new_node->word, word);
new_node->count = 1;
new_node->left = new_node->right = NULL;
if (prev == NULL) {
root = new_node;
} else if (res < 0) {
prev->left = new_node;
} else {
prev->right = new_node;
}
}
void traverse(struct node *p) {
if (p == NULL) {
return;
}
traverse(p->left);
printf("%s: %d\n", p->word, p->count);
traverse(p->right);
}
void free_tree(struct node *p) {
if (p == NULL) {
return;
}
free_tree(p->left);
free_tree(p->right);
free(p);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(1);
}
char word[MAX_WORD_LEN];
int c;
int len = 0;
while ((c = fgetc(fp)) != EOF) {
if (isalpha(c)) {
if (len < MAX_WORD_LEN - 1) {
word[len++] = tolower(c);
}
} else {
if (len > 0) {
word[len] = '\0';
insert_word(word);
len = 0;
}
}
}
if (len > 0) {
word[len] = '\0';
insert_word(word);
}
traverse(root);
free_tree(root);
fclose(fp);
return 0;
}
```
程序使用二叉搜索树来统计单词的出现次数,每读取一个单词,就在二叉搜索树中查找该单词是否已经存在,如果存在,则增加它的计数器,如果不存在,则插入一个新节点,并将计数器设为1。最后,使用中序遍历来按字典序输出所有单词及其出现次数,并释放二叉搜索树所占用的内存。
程序运行时需要指定一个文本文件的路径作为参数,例如:
```
$ ./wordcount book.txt
```
其中`book.txt`是要统计的文本文件。
统计一个文本文件中每个单词出现的次数,输出结果按照单词的字典序排序。scala代码
以下是一个统计文本文件中每个单词出现次数,并按字典序排序输出结果的Scala代码:
```scala
import scala.io.Source
object WordCount {
def main(args: Array[String]) {
val filename = "input.txt" // 输入文件名
val words = Source.fromFile(filename).getLines.flatMap(_.split("\\W+")).map(_.toLowerCase).toList // 读取文件并将内容转化为单词列表
val wordCounts = words.groupBy(identity).mapValues(_.size).toList.sortBy(_._1) // 统计单词出现次数并按字典序排序
wordCounts.foreach { case (word, count) => println(word + "\t" + count) } // 输出结果
}
}
```
代码解释:
1. 首先,我们使用Scala的`Source`对象读取输入文件中的文本内容,并使用`getLines`方法将文本内容按行分割为一个迭代器。
2. 接着,我们使用`flatMap`方法将每行文本内容按空格或标点符号分割为一个单词列表,使用`map`方法将单词转化为小写,最后使用`toList`方法将所有单词合并为一个列表。
3. 然后,我们使用`groupBy`方法将单词列表按照单词内容进行分组,并使用`mapValues`方法将每个单词的出现次数统计出来,最后使用`toList`方法将每个单词的统计结果合并为一个列表。
4. 最后,我们使用`sortBy`方法按照单词的字典序对列表进行排序,并使用`foreach`方法遍历列表,将每个单词的统计结果输出到控制台上。
需要注意的是,上述代码中的`input.txt`文件应该放在与Scala源代码相同的目录下。