dev
zj3D 8 months ago
parent b86f626e94
commit 44c1f9eb1e

@ -0,0 +1,30 @@
import re
from cppy.cp_util import *
def extractwords(str_data):
pattern = re.compile('[\W_]+')
word_list = pattern.sub(' ', str_data).lower().split()
stop_words = get_stopwords()
return [w for w in word_list if not w in stop_words]
def frequencies(word_list):
word_freqs = {}
for word in word_list:
word_freqs[word] = word_freqs.get(word, 0) + 1
return word_freqs
def sort(word_freq):
return sorted( word_freq.items(), key=lambda x: x[1], reverse=True )
def printall(word_freqs, n = 10 ):
for word, freq in word_freqs[ :n ]:
print(word, '-', freq)
if __name__ == "__main__":
txtcontent = read_file( testfilepath )
word_list = extractwords( txtcontent )
word_freqs = frequencies( word_list )
word_sort = sort ( word_freqs )
printall(word_sort)

@ -3,6 +3,7 @@ import aiofiles
from collections import Counter from collections import Counter
from cppy.cp_util import * from cppy.cp_util import *
########## 不太合适的一个场景 ######################
# #
# 协程 # 协程
# #

@ -1,7 +1,7 @@
import sys import sys
from cppy.cp_util import * from cppy.cp_util import *
## 切分任务这个工作,可以统一为一个通用函数。做成一个生成器 ## 切分任务这个工作,可以统一为一个通用函数。做成一个生成器
script_dir = os.path.dirname(os.path.abspath(__file__)) script_dir = os.path.dirname(os.path.abspath(__file__))
testfile = os.path.join(script_dir, 'test.txt') testfile = os.path.join(script_dir, 'test.txt')

@ -2,7 +2,11 @@ import concurrent.futures
from collections import Counter from collections import Counter
import cppy.cp_util as util import cppy.cp_util as util
# 价值不大,就是多线程的一个表现,说明松耦合不如消息驱动的组件 '''
concurrent.futures模块为Python中的并发编程提供了一个统一接口,
这个模块隐藏了低层次的线程和进程创建同步和清理的细节,提供了一个更高层次的API来处理并发任务
当前版本推荐它与asyncio模块结合使用完成Python中的各种异步编程任务
'''
class WordFrequencyAgent: class WordFrequencyAgent:
def __init__(self, words): def __init__(self, words):
Loading…
Cancel
Save