from collections import defaultdict
import re
d=defaultdict(lambda :0)
with open(r'e:/bb.txt',mode='rt+',encoding='utf8') as f:
for line in f:
for sub in re.split('[^\w-]+',line):
if len(sub)>0:
d[sub]+=1
b=0
for p in sorted(d,key=lambda x:d[x],reverse=True):
if b<10:
print(p,d[p])
b+=1
b=0
for p in sorted(d.items(),key=lambda x:x[1],reverse=True):
if b<10:
print(p[0],p[1])
b+=1
import re
from collections import defaultdict
d=defaultdict(lambda :0)
regex=re.compile('[^\w-]+',flags=re.S|re.I)
with open('e:/bb.txt',mode='rt+',encoding='utf8') as f:
for line in f:
for sub in regex.split(line):
if len(sub)>0:
d[sub.lower()]+=1
print(d)
v=0
for p in sorted(d,key=lambda u:d[u],reverse=True):
if v<10:
print(p,d[p])
v+=1
def wordcount(path:str='.'):
d=defaultdict(lambda :0)
with open(path,mode='rt+',encoding='utf8') as f:
for line in f:
for sub in regex.split(line):
if len(sub)>0:
d[sub.upper()]+=1
return d
v=0
for p in sorted(wordcount('e:/bb.txt').items(),key=lambda m:m[1],reverse=True):
if v<10:
print(p)
v+=1