From d136c93df604849e07e8b1aee0094eeb17833991 Mon Sep 17 00:00:00 2001 From: ghaa0920 <1554137355@qq.com> Date: Thu, 1 Jun 2023 16:16:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=AD=A3=E5=88=99=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E4=BF=9D=E7=95=99=E8=8B=B1=E6=96=87=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E5=92=8C'=E7=89=B9=E6=AE=8A=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/wordfreqCMD.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/wordfreqCMD.py b/app/wordfreqCMD.py index dcee74e..501b0c8 100644 --- a/app/wordfreqCMD.py +++ b/app/wordfreqCMD.py @@ -8,6 +8,7 @@ import string import operator import os, sys # 引入模块sys,因为我要用里面的sys.argv列表中的信息来读取命令行参数。 import pickle_idea +import re def freq(fruit): ''' @@ -39,12 +40,12 @@ def file2str(fname):#文件转字符 def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。 - special_characters = '\_©~<=>+/[]*&$%^@.,?!:;#()"“”—‘’{}|' # 把里面的字符都去掉 - for c in special_characters: - s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况 - s = s.replace('--', ' ') - s = s.strip() # 去除前后的空格 - + + cop = re.compile("[^-^a-z^A-Z^\\']") # 通过正则表达式保留英文字符和'特殊字符 + s = cop.sub(' ', s) + s = s.strip('-') #去除头尾连字符 + s = s.strip() # 去除前后的空格 + if '\'' in s: n = len(s) t = '' # 用来收集我需要保留的字符 @@ -60,6 +61,7 @@ def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用 return s + def sort_in_descending_order(lst):# 单词按频率降序排列 lst2 = sorted(lst, reverse=True, key=lambda x: (x[1], x[0])) return lst2