EnglishPal/app/wordfreqCMD.py

121 lines
3.9 KiB
Python
Raw Normal View History

2021-04-06 16:22:03 +08:00
###########################################################################
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
# Written permission must be obtained from the author for commercial uses.
###########################################################################
import collections
import string
import operator
2023-06-01 19:02:28 +08:00
import os, sys # 引入模块sys因为我要用里面的sys.argv列表中的信息来读取命令行参数。
2021-04-06 16:22:03 +08:00
import pickle_idea
2023-06-01 19:02:28 +08:00
def freq(s):
2021-04-06 16:22:03 +08:00
'''
功能 把字符串转成列表 目的是得到每个单词的频率
输入 字符串
输出 列表 列表里包含一组元组每个元组包含单词与单词的频率 比如 [('apple', 2), ('banana', 1)]
注意事项 首先要把字符串转成小写原因是
'''
result = []
2023-06-01 19:02:28 +08:00
s = s.lower() # 字母转小写
word_lst = s.split() # 字符串转成list
c = collections.Counter(word_lst)
2021-04-06 16:22:03 +08:00
result = c.most_common()
return result
2023-06-01 19:02:28 +08:00
def youdao_link(word): # 有道链接
link = 'http://youdao.com/w/eng/' + word + '/#keyfrom=dict2.index' # 网址
2021-04-06 16:22:03 +08:00
return link
2023-06-01 19:02:28 +08:00
def file_to_str(f_name): # 文件转字符
f = open(f_name) # 打开
f_str = f.read() # 读取
f.close() # 关闭
return f_str
2021-04-06 16:22:03 +08:00
2023-06-01 19:02:28 +08:00
def remove_punctuation(s): # 这里是words_text是形参 (parameter)。函数被调用时才给words_text赋值。
special_characters = '\_©~<=>+-/[]*&$%^@.,?!:;#()"“”—‘’{}|' # 把里面的字符都去掉
2021-04-06 16:22:03 +08:00
for c in special_characters:
2023-06-01 19:02:28 +08:00
s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况
2021-04-06 16:22:03 +08:00
s = s.replace('--', ' ')
2023-06-01 19:02:28 +08:00
s = s.strip() # 去除前后的空格
2021-04-06 16:22:03 +08:00
if '\'' in s:
n = len(s)
2023-06-01 19:02:28 +08:00
characters = '' # 用来收集我需要保留的字符
for i in range(n): # 只有单引号前后都有英文字符,才保留
2021-04-06 16:22:03 +08:00
if s[i] == '\'':
i_is_ok = i - 1 >= 0 and i + 1 < n
2023-06-01 19:02:28 +08:00
if i_is_ok and s[i - 1] in string.ascii_letters and s[i + 1] in string.ascii_letters:
characters += s[i]
2021-04-06 16:22:03 +08:00
else:
2023-06-01 19:02:28 +08:00
characters += s[i]
return characters
2021-04-06 16:22:03 +08:00
else:
return s
2023-06-01 19:02:28 +08:00
def sort_in_descending_order(lst): # 单词按频率降序排列
2021-04-06 16:22:03 +08:00
lst2 = sorted(lst, reverse=True, key=lambda x: (x[1], x[0]))
return lst2
2023-06-01 19:02:28 +08:00
def sort_in_ascending_order(lst): # 单词按频率降序排列
2021-04-06 16:22:03 +08:00
lst2 = sorted(lst, reverse=False, key=lambda x: (x[1], x[0]))
return lst2
def make_html_page(lst, fname): # 只是在wordfreqCMD.py中的main函数中调用所以不做修改
2021-04-06 16:22:03 +08:00
'''
功能把lst的信息存到fname中以html格式
'''
s = ''
count = 1
for x in lst:
# <a href="">word</a>
s += '<p>%d <a href="%s">%s</a> (%d)</p>' % (count, youdao_link(x[0]), x[0], x[1])
count += 1
f = open(fname, 'w')
f.write(s)
f.close()
## main程序入口
if __name__ == '__main__':
num = len(sys.argv)
2023-06-01 19:02:28 +08:00
if num == 1: # 从键盘读入字符串
2021-04-06 16:22:03 +08:00
s = input()
2023-06-01 19:02:28 +08:00
elif num == 2: # 从文件读入字符串
2021-04-06 16:22:03 +08:00
fname = sys.argv[1]
2023-06-01 19:02:28 +08:00
s = file_to_str(fname)
2021-04-06 16:22:03 +08:00
else:
print('I can accept at most 2 arguments.')
2023-06-01 19:02:28 +08:00
sys.exit() # 结束程序运行, 下面的代码不会被执行了。
2021-04-06 16:22:03 +08:00
2023-06-01 19:02:28 +08:00
s = remove_punctuation(s) # 这里是s是实参(argument),里面有值
2021-04-06 16:22:03 +08:00
L = freq(s)
for x in sort_in_descending_order(L):
2023-06-01 19:02:28 +08:00
print('%s\t%d\t%s' % (x[0], x[1], youdao_link(x[0]))) # 函数导出
2021-04-06 16:22:03 +08:00
# 把频率的结果放result.html中
2023-06-01 19:02:28 +08:00
make_html_page(sort_in_descending_order(L), 'result.html')
2021-04-06 16:22:03 +08:00
print('\nHistory:\n')
if os.path.exists('frequency.p'):
d = pickle_idea.load_record('frequency.p')
else:
d = {}
2023-06-01 19:02:28 +08:00
print(sort_in_descending_order(pickle_idea.dict_to_lst(d)))
2021-04-06 16:22:03 +08:00
# 合并频率
2023-06-01 19:02:28 +08:00
lst_history = pickle_idea.dict_to_lst(d)
2021-04-06 16:22:03 +08:00
d = pickle_idea.merge_frequency(L, lst_history)
pickle_idea.save_frequency_to_pickle(d, 'frequency.p')