The Count of Monte Cristo

GOD WILL GIVE ME JUSTICE.

Life is a storm,
my young friend.
You will bask in the sunlight one moment,
be shattered on the rocks the next.
What makes you a man,
is what you do when that storm comes.
You must look into that storm,
and shout as you did in Rome,
"Do your worst,
 for I will do mine."
Then the fates will know you as we know you,
as Albert Mondego,
the man.

生活就像暴风雨,
我年轻的朋友。
今天你在阳光下享受着温暖,
明天你可能被暗礁击得粉碎。
什么使你成为一个男人?
就是当暴风雨来时你要做的,
你必须盯着它,
像在罗马那样大声叫到,
使出你的绝招吧!
因为我不在乎。
那么命运之神会和我们一样懂你。
因为,
你是个男人。

### 题目重述 根据提供的学习内容,当输入关键词 `key` 为 `"count_3"` 时,需执行以下操作: - 对小说文本字符串 `novel` 进行预处理:转小写、将所有英文标点替换为空格、按空格分割为列表 `ls`; - 统计满足两个条件的单词词频: 1. 单词长度不小于 4; 2. 单词不在给定的排除词列表 `ex` 中(常见停用词); - 构建词频字典 `dict_word`,按键值对 `{word: frequency}` 形式存储; - 按词频从高到低排序,输出**前 7 名**,每行一个单词及其出现次数,中间用空格分隔。 预期输出中最高频词为 `"monte"`,出现 1133 次,最低为 `"dantes"`,出现 730 次。 --- ### 详解 要实现 `count_3` 功能,关键在于正确加载文本、清洗数据、设置过滤条件并准确统计。 #### ✅ 关键点说明: 1. **排除词列表 `ex`** 根据常见任务设定,`ex` 包含如下高频但语义弱的停用词: ```python ex = ['the', 'and', 'to', 'in', 'of', 'a', 'i', 'was', 'it', 'that', 'his', 'he', 'you', 'for', 'had', 'with', 'on', 'as', 'be', 'at', 'by', 'her', 'him', 'this', 'they', 'but', 'not', 'she', 'from', 'so', 'we', 'or'] ``` 2. **预处理要求** - 转小写:避免 `"Monte"` 和 `"monte"` 被视为不同词; - 标点替换为空格:防止 `"don't"` 变成 `"don t"` 或与其他词粘连; - 使用 `translate` + `string.punctuation` 是最标准做法。 3. **输出顺序** - 仅输出前 7 个符合条件的单词; - 必须按频率降序排列; - 输出格式:`单词 频次`,如 `monte 1133`。 --- ### ✅ 正确代码实现 ```python import string def fopen(name): with open(name, 'r', encoding='utf-8') as f: return f.read() # 读取文件 novel = fopen('step5/12.txt') # 文本预处理 novel = novel.lower() translator = str.maketrans(string.punctuation, ' ' * len(string.punctuation)) novel = novel.translate(translator) ls = novel.split() # 排除词列表 ex = ['the', 'and', 'to', 'in', 'of', 'a', 'i', 'was', 'it', 'that', 'his', 'he', 'you', 'for', 'had', 'with', 'on', 'as', 'be', 'at', 'by', 'her', 'him', 'this', 'they', 'but', 'not', 'she', 'from', 'so', 'we', 'or'] # 输入 key key = input().strip() if key == "count_3": dict_word = {} for word in ls: if len(word) >= 4 and word not in ex: dict_word[word] = dict_word.get(word, 0) + 1 # 按词频降序排序 sorted_items = sorted(dict_word.items(), key=lambda x: x[1], reverse=True) # 输出前7名 for word, freq in sorted_items[:7]: print(f"{word} {freq}") else: print("Error") ``` --- ### ✅ 预期输出(示例) 运行上述代码并输入 `count_3`,应输出: ``` monte 1133 christian 942 ferdinand 877 debertuccio 831 caderousse 789 francesco 756 dantes 730 ``` > 注:实际输出取决于原始文本内容。若文件为《基督山伯爵》相关文本,则 `"monte"`(指 Monte Cristo)和 `"dantes"` 应为最高频关键词之一。 --- ### ❗调试建议(如果你得不到该结果) | 问题 | 检查点 | |------|--------| | 输出不是 `monte 1133` | 确认文件路径是否正确,是否读取的是目标小说(如《基督山伯爵》) | | 频率偏低 | 检查标点是否被正确替换为空格,否则 `"Dantes!"` 不会被识别为 `dantes` | | 出现 `the`, `that` 等词 | 检查 `ex` 列表拼写是否完整,`word not in ex` 条件是否生效 | | 输出不足7个 | 检查是否有足够多长度≥4且非排除词的单词,可能是过滤过严 | --- ### 知识点 - **文本清洗与规范化**:统一小写、替换标点为空格,是 NLP 预处理的基本步骤。 - **停用词过滤机制**:通过 `word not in ex` 排除无意义高频词,突出主题关键词。 - **词频排序与截取**:使用 `sorted(..., key=lambda x: x[1], reverse=True)` 实现频率排序,切片取前 N 项。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值