forked from mrlan/EnglishPal
				
			通过正则表达式保留英文字符和'特殊字符
							parent
							
								
									688a198768
								
							
						
					
					
						commit
						d136c93df6
					
				|  | @ -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,10 +40,10 @@ def file2str(fname):#文件转字符 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。 | def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用时才给s赋值。 | ||||||
|     special_characters = '\_©~<=>+/[]*&$%^@.,?!:;#()"“”—‘’{}|' # 把里面的字符都去掉 | 
 | ||||||
|     for c in special_characters: |     cop = re.compile("[^-^a-z^A-Z^\\']")  # 通过正则表达式保留英文字符和'特殊字符 | ||||||
|         s = s.replace(c, ' ') # 防止出现把 apple,apple 移掉逗号后变成 appleapple 情况 |     s = cop.sub(' ', s) | ||||||
|     s = s.replace('--', ' ') |     s = s.strip('-')     #去除头尾连字符 | ||||||
|     s = s.strip()  # 去除前后的空格 |     s = s.strip()  # 去除前后的空格 | ||||||
| 
 | 
 | ||||||
|     if '\'' in s: |     if '\'' in s: | ||||||
|  | @ -60,6 +61,7 @@ def remove_punctuation(s): # 这里是s是形参 (parameter)。函数被调用 | ||||||
|         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