forked from p46318075/CodePattern
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.
46 lines
1.1 KiB
46 lines
1.1 KiB
8 months ago
|
# -*- coding: utf-8 -*-
|
||
|
import multiprocessing
|
||
|
from collections import Counter
|
||
|
from cppy.cp_util import *
|
||
|
|
||
|
|
||
|
#
|
||
8 months ago
|
# 多进程: 因为创建进程相比计算过程开销太大,结果最慢
|
||
8 months ago
|
#
|
||
8 months ago
|
stop_words = get_stopwords()
|
||
|
|
||
8 months ago
|
def process_chunk(chunk):
|
||
8 months ago
|
# 过滤停用词
|
||
8 months ago
|
words = [ w for w in chunk if ( not w in stop_words ) and len(w) >= 3 ]
|
||
|
return Counter(words)
|
||
|
|
||
|
def merge_counts(counts_list):
|
||
|
# 合并多个Counter对象
|
||
|
total_counts = Counter()
|
||
|
for counts in counts_list:
|
||
|
total_counts += counts
|
||
|
return total_counts
|
||
|
|
||
|
|
||
|
@timing_decorator
|
||
|
def main():
|
||
8 months ago
|
# 读取文件内容,分割文件内容为多个块,每个块由一个进程处理
|
||
|
chunks = get_chunks(testfilepath,1000)
|
||
8 months ago
|
|
||
|
# 使用多进程处理每个块
|
||
|
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
|
||
|
counts_list = pool.map(process_chunk, chunks)
|
||
|
pool.close()
|
||
|
pool.join()
|
||
|
|
||
|
# 合并计数
|
||
|
total_counts = merge_counts(counts_list)
|
||
|
|
||
|
# 输出最高频的n个词
|
||
|
print_word_freqs(total_counts.most_common(10))
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|
||
|
|