From 705aa5efcbb2af912bb03e3d50a330a4e52ac229 Mon Sep 17 00:00:00 2001 From: tianqilu <1336601986@qq.com> Date: Thu, 27 Apr 2023 16:00:46 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E6=94=B9,=E5=89=8D=E9=9D=A2?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=9A=84first=20commit=E6=98=AF=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/wordfreqCMD.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/app/wordfreqCMD.py b/app/wordfreqCMD.py index 9ee7e56..8973064 100644 --- a/app/wordfreqCMD.py +++ b/app/wordfreqCMD.py @@ -38,20 +38,42 @@ def file2str(fname):#文件转字符 return s +def str2chararray(str): # 字符串转字符数组 + chararray = [] + for i in range(len(str)): + chararray.append(str[i]) + return chararray + + def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。 - special_characters = '_©~=+[]*&$%^@.,?!:;#()"“”—‘’' # 把里面的字符都去掉 + # carr = str2chararray(s) # 字符串转字符数组 + # print(carr) + special_characters = '&_~=+[]%^@.,?!:;#()"“”—‘’|/\\<>{}' # 把里面的字符都去掉 + special_words = ('$lt', '$gt', '$') # 特殊词汇 for c in special_characters: - s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况 + s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况 + carr = str2chararray(s) # 字符串转字符数组 + for i, value in enumerate(carr): + if value == '&': # 遍历替换 + carr[i] = '\'' + print('sss' + s) + for j in range(1,5): + if carr[i+j] in [' ','3','9']: + carr[i + j]='' + s = ''.join(carr) # 字符数组转字符串 + print('sss'+s) + for w in special_words: # 替换字符串中的剩余特殊字符 + s = s.replace(w, ' ') s = s.replace('--', ' ') - s = s.strip() # 去除前后的空格 - + s = s.strip() # 去除前后的空格 + if '\'' in s: n = len(s) - t = '' # 用来收集我需要保留的字符 - for i in range(n): # 只有单引号前后都有英文字符,才保留 + t = '' # 用来收集我需要保留的字符 + for i in range(n): # 只有单引号前后都有英文字符,才保留 if s[i] == '\'': 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] else: t += s[i] @@ -60,6 +82,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