更新 'app/wordfreqCMD.py'
parent
e48008550a
commit
3ca66d147c
|
@ -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())
|
||||||
|
|
||||||
|
|
|
@ -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):
|
||||||
'''
|
'''
|
||||||
|
@ -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,'-')
|
||||||
|
s=keep_need_punctuation(s,'\'')
|
||||||
|
return s
|
||||||
|
|
||||||
if '\'' in 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
|
||||||
|
|
Loading…
Reference in New Issue