《谁动了我的奶酪?》是美国作家斯宾塞·约翰逊创作的一个寓言故事,该书首次出版于1998年。书中主要讲述4个“人物”—两只小老鼠“嗅嗅(Sniff)”、“匆匆(Scurry)”和两个小矮人“哼哼(Hem)”、“唧唧(Haw)”找寻奶酪的故事。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    文件“Who Moved My Cheese.txt”中包含这个故事的中英文,格式如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    image.png‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    请按照函数的注释,补充程序中缺失部分语句,按要求实现如下程序功能:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    read_file()函数将文件中的内容读为字符串,过滤掉中文,只保留文件中的英文字母和西文符号(只保留ASCII码字符)。所有字符转为小写,将其中所有标点、符号替换为空格。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    count_of_words()函数统计read_file()函数返回的字符串中的单词数量和不重复的单词数量。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    top_ten_words()函数分行依次输出出现次数最多的n个单词及其出现次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    word_frequency()函数统计并以字典类型返回每个单词出现的次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    top_ten_words_no_excludes()函数统计并输出去除常见的冠词、代词、系动词和连接词后,出现次数最多的 cnt个单词及其出现次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    根据用户输入的指令和任务完成程序:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

    1. 若输入指令为“1”,则在下一行中输入一个非负整数n,并输出read_file()函数返回值的前n个字符。
    2. 若输入指令为“2”,并调用count_of_words()函数统计输出read_file()函数返回的字符串中的单词数量和不重复的单词数量,格式参考下面的输入输出示例,其中的XXXX需用统计出的实际数值替换。
    3. 若输入指令为“3”,则在下一行中输入一个正整数n,并调用top_ten_words()函数分行依次输出出现次数最多的n个单词及其出现次数,单词和次数之间以空格间隔。
    4. 若输入指令为“4”,则在下一行中输入一个非负整数n,并调用top_ten_words_no_excludes()函数从词频统计结果中去除常见的冠词、代词、系动词和连接词后统计词频,再输出出现次数最多的n个单词及其出现次数,单词和次数之间以空格间隔。需排除的单词包括:[‘a’, ‘an’, ‘the’, ‘i’, ‘he’, ‘she’, ‘his’, ‘my’, ‘we’,’or’, ‘is’, ‘was’, ‘do’, and’, ‘at’, ‘to’, ‘of’, ‘it’, ‘on’, ‘that’, ‘her’, ‘c’,’in’, ‘you’, ‘had’, ‘s’, ‘with’, ‘for’, ‘t’, ‘but’, ‘as’, ‘not’, ‘they’, ‘be’, ‘were’, ‘so’, ‘our’, ‘all’, ‘would’, ‘if’, ‘him’, ‘from’, ‘no’, ‘me’, ‘could’, ‘when’, ‘there’, ‘them’, ‘about’, ‘this’, ‘their’, ‘up’, ‘been’, ‘by’, ‘out’, ‘did’, ‘have’]

    参考资料:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    【ASCII 码表】‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    0000-007F(0-127):C0控制符及基本拉丁文 (C0 Control and Basic Latin)
    0080-00FF(128-255):C1控制符及拉丁文补充-1 (C1 Control and Latin 1 Supplement) ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
    image.png

    1. import string
    2. def read_file(file):
    3. """接收文件名为参数,将文件中的内容读为字符串,
    4. 只保留文件中的英文字母和西文符号,
    5. 过滤掉中文(中文字符及全角符号Unicode编码都大于256)
    6. 将所有字符转为小写,
    7. 将其中所有标点、符号替换为空格,返回字符串
    8. """
    9. with open(file, 'r', encoding='utf-8') as novel:
    10. txt = novel.read()
    11. english_only_txt = ''.join(x for x in txt if ord(x) < 256)
    12. english_only_txt = english_only_txt.lower()
    13. for character in string.punctuation:
    14. english_only_txt = english_only_txt.replace(character, ' ')
    15. return english_only_txt
    16. def count_of_words(txt):
    17. """接收去除标点、符号的字符串,统计并返回其中单词数量和不重复的单词数量"""
    18. words_list = txt.split()
    19. amount_of_words = len(txt.split())
    20. amount_of_words_no_repeat = len(set(words_list))
    21. return amount_of_words, amount_of_words_no_repeat
    22. def word_frequency(txt):
    23. """接收去除标点、符号的字符串,统计并返回每个单词出现的次数
    24. 返回值为字典类型,单词为键,对应出现的次数为值"""
    25. frequency = dict()
    26. words_list = txt.split()
    27. for word in words_list:
    28. frequency[word] = frequency.get(word, 0) + 1
    29. return frequency
    30. def top_ten_words(frequency, cnt):
    31. """接收词频字典,输出出现次数最多的cnt个单词及其出现次数"""
    32. frequency_items = sorted(frequency.items(), key=lambda x: x[1], reverse=True)
    33. for word, counts in frequency_items[:cnt]:
    34. print(word, counts)
    35. def top_ten_words_no_excludes(frequency, cnt):
    36. """接收词频字典,去除常见的冠词、代词、系动词和连接词后,输出出现次数最多的
    37. cnt个单词及其出现次数,需排除的单词如下:
    38. excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we',
    39. 'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it', 'on', 'that', 'her',
    40. 'c','in', 'you', 'had','s', 'with', 'for', 't', 'but', 'as', 'not', 'they',
    41. 'be', 'were', 'so', 'our','all', 'would', 'if', 'him', 'from', 'no', 'me',
    42. 'could', 'when', 'there','them', 'about', 'this', 'their', 'up', 'been',
    43. 'by', 'out', 'did', 'have']
    44. """
    45. excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we',
    46. 'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it',
    47. 'on', 'that', 'her', 'c','in', 'you', 'had','s', 'with',
    48. 'for', 't', 'but', 'as', 'not', 'they', 'be', 'were',
    49. 'so', 'our','all', 'would', 'if', 'him', 'from', 'no',
    50. 'me', 'could', 'when', 'there','them', 'about', 'this',
    51. 'their', 'up', 'been', 'by', 'out', 'did', 'have']
    52. for word in excludes_words:
    53. frequency.pop(word)
    54. frequency_items = sorted(frequency.items(), key=lambda x: x[1], reverse=True)
    55. for word, counts in frequency_items[:cnt]:
    56. print(word, counts)
    57. # 取消这段和代码最后二行注释可以绘制词云,仅供参考
    58. def draw_cloud(frequency):
    59. """绘制词云,传入参数为词频,设定图片的宽度600,高度400,背景白色、字体最大值150、图片边缘为5。"""
    60. wc = WordCloud(max_words=80, # 设置显示高频单词数量
    61. width=600, # 设置图片的宽度
    62. height=400, # 设置图片的高度
    63. background_color='White', # 设置背景颜色
    64. max_font_size=150, # 设置字体最大值
    65. margin=5, # 设置图片的边缘
    66. scale=1.5) # 按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
    67. wc.generate_from_frequencies(frequency) # 根据文本内容直接生成词云
    68. plt.imshow(wc) # 负责对图像进行处理,并显示其格式,但是不能显示。
    69. plt.axis("off") # 不显示坐标轴
    70. wc.to_file('My Cheese.png') # 词云保存为图片
    71. plt.show() # 显示图像
    72. if __name__ == '__main__':
    73. filename = 'Who Moved My Cheese.txt' # 文件名
    74. content = read_file(filename) # 调用函数返回字典类型的数据
    75. frequency_result = word_frequency(content) # 统计词频
    76. cmd = input()
    77. if cmd == '1':
    78. n = int(input())
    79. print(content[:n])
    80. elif cmd == '2':
    81. amount_results = count_of_words(content)
    82. print('文章共有单词{}个,其中不重复单词{}个'.format(*amount_results))
    83. elif cmd == '3':
    84. n = int(input())
    85. top_ten_words(frequency_result, n)
    86. elif cmd == '4':
    87. n = int(input())
    88. top_ten_words_no_excludes(frequency_result, n)
    89. # frequency_no_excludes = top_ten_words_no_excludes(frequency_result)
    90. # draw_cloud(frequency_no_excludes)