使用集合重构第6 章的程序,计算《葛底斯堡演说》中不同单词的个数。去掉单词中的标点符号,并且所有字母都小写。
时间: 2023-06-04 07:07:07 浏览: 59
好的,这是一个计算题。可以使用Python编程语言来完成。以下是代码示例:
```python
import string
def count_unique_words(file_path):
word_count = {}
with open(file_path, 'r') as file:
for line in file:
line = line.translate(str.maketrans('', '', string.punctuation)) # 去除标点符号
line = line.lower() # 小写化所有字母
words = line.split()
for word in words:
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
return word_count
file_path = 'Gettysburg_Address.txt'
word_count = count_unique_words(file_path)
print('不同单词的个数为:', len(word_count))
```
其中,文件路径需要根据实际情况进行修改。此程序会去掉文本中的标点符号,并将所有字母转化为小写。最后输出统计结果,即不同单词的个数。