You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
814 B
32 lines
814 B
9 months ago
|
import asyncio
|
||
|
import aiofiles
|
||
|
from collections import Counter
|
||
|
from cppy.cp_util import *
|
||
|
|
||
8 months ago
|
########## 不太合适的一个场景 ######################
|
||
9 months ago
|
#
|
||
|
# 协程
|
||
|
#
|
||
9 months ago
|
async def read_file(file_path):
|
||
|
async with aiofiles.open(file_path, 'r', encoding='utf-8') as file:
|
||
|
content = await file.read()
|
||
|
return content
|
||
|
|
||
|
async def count_words(text):
|
||
|
words = extract_str_words(text.lower())
|
||
|
word_counts = Counter(words)
|
||
|
return word_counts
|
||
|
|
||
|
async def main():
|
||
|
wordfreqs = Counter()
|
||
|
files = [ testfilepath ] * 10
|
||
|
for thisfile in files:
|
||
|
text = await read_file( thisfile )
|
||
|
top_words = await count_words(text)
|
||
|
wordfreqs += top_words
|
||
|
for word, count in wordfreqs.most_common(10):
|
||
|
print(f"{word}: {count//10}")
|
||
|
|
||
9 months ago
|
|
||
9 months ago
|
# 运行异步主函数
|
||
|
asyncio.run(main())
|