0
0
Fork 0

更新 'app/wordfreqCMD.py'

Bug474-RenYinJie
任殷杰 2022-06-20 13:07:27 +08:00
parent e48008550a
commit 3ca66d147c
2 changed files with 18 additions and 11 deletions

View File

@ -20,6 +20,6 @@ class WordFreq:
if __name__ == '__main__': if __name__ == '__main__':
f = WordFreq('BANANA; Banana, apple ORANGE Banana banana.') f = WordFreq('i\'m BANANA; Banana, apple ORANGE Bana-na/////,./;\'[]-=---\'/-banana.')
print(f.get_freq()) print(f.get_freq())

View File

@ -8,6 +8,7 @@ import string
import operator import operator
import os, sys # 引入模块sys因为我要用里面的sys.argv列表中的信息来读取命令行参数。 import os, sys # 引入模块sys因为我要用里面的sys.argv列表中的信息来读取命令行参数。
import pickle_idea import pickle_idea
import re
def freq(fruit): def freq(fruit):
''' '''
@ -18,7 +19,7 @@ def freq(fruit):
''' '''
result = [] result = []
fruit = fruit.lower() # 字母转小写 fruit = fruit.lower() # 字母转小写
flst = fruit.split() # 字符串转成list flst = fruit.split() # 字符串转成list
c = collections.Counter(flst) c = collections.Counter(flst)
@ -39,19 +40,26 @@ def file2str(fname):#文件转字符
def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。 def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。
special_characters = '_©~=+[]*&$%^@.,?!:;#()"“”—‘’' # 把里面的字符都去掉 special_characters = '_©~=+[]*&$%^@.,?!:;#()"“”—‘’' #把里面的字符都去掉
for c in special_characters: for c in special_characters:
s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况 s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况
s = s.replace('--', ' ') s = s.replace('--', ' ')
'''cop = re.compile("[^a-z^A-Z^\\']")#通过正则表达式保留英文字符和-、'两个特殊字符
s=cop.sub(' ' , s)'''
s = s.strip() # 去除前后的空格 s = s.strip() # 去除前后的空格
#s=keep_need_punctuation(s,'-')
if '\'' in s: s=keep_need_punctuation(s,'\'')
return s
def keep_need_punctuation(s,need):#用于保留特定的字符
if need in s:
n = len(s) n = len(s)
t = '' # 用来收集我需要保留的字符 t = '' # 用来收集我需要保留的字符
for i in range(n): # 只有单引号前后都有英文字符,才保留 for i in range(n): # 只有单引号前后都有英文字符,才保留
if s[i] == '\'': if s[i] == need:
i_is_ok = i - 1 >= 0 and i + 1 < n i_is_ok = i - 1 >= 0 and i + 1 < n
if i_is_ok and s[i-1] in string.ascii_letters and s[i+1] in string.ascii_letters: if i_is_ok and s[i - 1] in string.ascii_letters and s[i + 1] in string.ascii_letters:
t += s[i] t += s[i]
else: else:
t += s[i] t += s[i]
@ -59,7 +67,6 @@ def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用
else: else:
return s return s
def sort_in_descending_order(lst):# 单词按频率降序排列 def sort_in_descending_order(lst):# 单词按频率降序排列
lst2 = sorted(lst, reverse=True, key=lambda x: (x[1], x[0])) lst2 = sorted(lst, reverse=True, key=lambda x: (x[1], x[0]))
return lst2 return lst2
@ -104,7 +111,7 @@ if __name__ == '__main__':
print('%s\t%d\t%s' % (x[0], x[1], youdao_link(x[0])))#函数导出 print('%s\t%d\t%s' % (x[0], x[1], youdao_link(x[0])))#函数导出
# 把频率的结果放result.html中 # 把频率的结果放result.html中
make_html_page(sort_in_descending_order(L), 'result.html') make_html_page(sort_in_descending_order(L), 'result.html')
print('\nHistory:\n') print('\nHistory:\n')
if os.path.exists('frequency.p'): if os.path.exists('frequency.p'):