第 0004 题: 任一个英文的纯文本文件,统计其中的单词出现的个数。
随便从网上拉了一篇英语文章。
文章中除了正常单词外,还包括链接,感叹符号,句号等等。思路是先将文章中除单词以外的符号均清除干净,然后使用字典模式存储单词出现的次数,最后,通过获取用户想要查询的单词去搜索对应单词出现的次数。
引用re模块,使用正则表达式查找除字符数字以外出现的符号。替换成空格符号
代码如下
import re
def cleanarticle(file):
with open(file,'r') as f:
content = f.read()
print(type(content))
match=re.findall(r'[^a-zA-Z0-9]+',content)
for i in match:
content=content.replace(i,' ')
return makedict(content)
def makedict(content):
d={}
words=content.split(' ')
for word in words:
if word not in d.keys():
d.setdefault(word,1)
else:
d[word]+=1
return d
if __name__ == '__main__':
filePath = r'C:\\Users\\Desktop\\Personal\\en.txt'
words=cleanarticle(filePath)
search_for = input('输入你想查找的英文单词\n')
if search_for in words.keys():
print(words[search_for])
else:
print('文中不存再在该单词!')
生成的结果如下:
{'Every': 1, 'single': 1, 'time': 3, 'you': 12, 'access': 2, 'a': 4, 'website': 1, 'leave': 1, 'tracks': 3, 'Tracks': 6, 'that': 4, 'others': 1, 'can': 4, 'If': 1, 'don': 1, 't': 1, 'like': 1, 'the': 10, 'idea': 1, 'find': 1, 'out': 1, 'what': 1, 'software': 1, 'help': 1, 'cover': 1, 'them': 1, 'Anti': 5, 'is': 2, 'complete': 1, 'solution': 1, 'to': 8, 'protect': 2, 'your': 16, 'privacy': 5, 'and': 16, 'enhance': 1, 'PC': 1, 'performance': 1, 'With': 1, 'simple': 1, 'click': 1, 'securely': 1, 'erase': 2, 'internet': 1, 'computer': 5, 'activities': 1, 'programs': 3, 'history': 2, 'information': 6, 'stored': 2, 'in': 2, 'many': 1, 'hidden': 1, 'files': 5, 'on': 3, 'support': 4, 'Internet': 8, 'Explorer': 4, 'AOL': 1, 'Netscape': 2, 'Mozilla': 1, 'Opera': 2, 'browsers': 2, 'It': 1, 'also': 1, 'include': 2, 'more': 2, 'than': 1, '85': 1, 'free': 1, 'plug': 1, 'ins': 1, 'extend': 1, 'erasing': 3, 'features': 2, 'popular': 3, 'such': 2, 'as': 2, 'ACDSee': 1, 'Acrobat': 1, 'Reader': 1, 'KaZaA': 1, 'PowerDVD': 1, 'WinZip': 1, 'iMesh': 2, 'Winamp': 1, 'much': 1, 'Also': 1, 'easily': 3, 'schedule': 1, 'tasks': 1, 'at': 3, 'specific': 1, 'intervals': 1, 'or': 4, 'Windows': 5, 'stat': 1, 'up': 1, '': 6, 'shutdown': 1, 'To': 1, 'ensure': 1, 'maximum': 1, 'protection': 2, 'implements': 1, 'US': 2, 'Department': 3, 'of': 8, 'Defense': 3, 'DOD': 2, '5220': 2, '22': 2, 'M': 1, 'Gutmann': 1, 'NSA': 1, 'secure': 1, 'methods': 3, 'making': 1, 'any': 1, 'erased': 1, 'unrecoverable': 1, 'even': 4, 'when': 1, 'using': 1, 'advanced': 1, 'recovery': 2, 'tools': 1, 'Free': 3, 'Download': 3, 'http': 3, 'www': 3, 'deprice': 3, 'com': 3, 'antitracks': 1, 'htm': 3, 'East': 3, 'Tec': 3, 'Eraser': 4, 'goes': 1, 'beyond': 2, 'U': 1, 'S': 1, 'standards': 2, 'for': 7, 'permanent': 1, 'erasure': 2, 'digital': 1, 'removes': 1, 'every': 2, 'trace': 1, 'sensitive': 2, 'data': 5, 'from': 3, 'Completely': 1, 'destroy': 1, 'without': 1, 'knowledge': 1, 'approval': 1, 'Web': 1, 'pages': 1, 'pictures': 1, 'sites': 1, 'visited': 1, 'unwanted': 1, 'cookies': 1, 'chatroom': 1, 'conversations': 1, 'deleted': 2, 'e': 2, 'mail': 1, 'messages': 1, 'temporary': 1, 'swap': 1, 'file': 2, 'Recycle': 1, 'Bin': 1, 'previously': 1, 'valuable': 1, 'corporate': 1, 'trade': 1, 'secrets': 1, 'Business': 1, 'plans': 1, 'personal': 2, 'photos': 1, 'confidential': 1, 'letters': 1, 'etc': 3, '2005': 1, 'offers': 1, 'full': 1, 'Navigator': 1, 'America': 1, 'Online': 1, 'MSN': 1, 'Peer2Peer': 1, 'applications': 1, 'Kazaa': 2, 'Lite': 1, 'Napster': 1, 'Morpheus': 1, 'Direct': 1, 'Connect': 1, 'Limewire': 1, 'Shareaza': 1, 'other': 1, 'Media': 1, 'Player': 1, 'RealPlayer': 1, 'Yahoo': 1, 'Messenger': 1, 'ICQ': 1, 'has': 1, 'an': 2, 'intuitive': 1, 'interface': 1, 'wizards': 1, 'guide': 1, 'through': 2, 'all': 1, 'necessary': 1, 'steps': 1, 'needed': 1, 'Other': 1, 'custom': 1, 'needs': 2, 'user': 1, 'defined': 1, 'command': 1, 'line': 1, 'parameters': 1, 'integration': 1, 'with': 2, 'password': 1, 'eastteceraserstandard': 1, 'Ghostsurf': 1, 'Platinum': 2, 'GhostSurf': 4, 'ensures': 1, 'safety': 1, 'online': 1, 'by': 1, 'providing': 1, 'anonymous': 2, 'encrypted': 1, 'connection': 2, 'stops': 1, 'spyware': 1, 'eliminates': 1, 'ads': 1, 'erases': 1, 'lets': 1, 'customize': 1, 'level': 1, 'real': 1, 'suit': 1, 'surfing': 1, 'A': 1, 'variety': 1, 'options': 1, 'enable': 1, 'block': 2, 'mask': 1, 'IP': 1, 'address': 1, 'route': 1, 'hubs': 1, 'encrypt': 1, 's': 1, 'Privacy': 1, 'Control': 1, 'Center': 1, 'allows': 2, 'see': 1, 'piece': 1, 'emits': 1, 'over': 1, 'preventing': 1, 'Service': 1, 'Provider': 1, 'ISP': 1, 'creating': 1, 'profile': 1, 'ghostsurfplatinum': 1, 'CyberScrub': 2, 'Pro': 1, 'Sensitive': 1, 'fall': 1, 'into': 1, 'wrong': 1, 'hands': 1, 'And': 1, 'because': 1, 'not': 1, 'capable': 1, 'deleting': 1, 'are': 2, 'risk!': 1, 'Passwords': 1, 'financial': 1, 'documents': 1, 'those': 1, 'about': 1, 'last': 1, 'night': 1, 'mails': 1, 'fair': 1, 'game': 1, 'IT': 1, 'professional': 1, 'technician': 1, 'hacker': 1, 'purge': 1, 'wipe': 1, 'far': 1, 'exceed': 1, 'deletion': 1}
D:\python>python clean_article.py
<class 'str'>
输入你想查找的英文单词
every
2
D:\python>python clean_article.py
<class 'str'>
输入你想查找的英文单词
mermaid
文中不存再在该单词!
版权声明:本文为博主原创文章,未经博主允许不得转载。
https://blog.youkuaiyun.com/weixin_43248061/article/details/84145491