zj3D 3 weeks ago
parent c29f01ece2
commit 6b1604413a

@ -5,6 +5,7 @@ from collections import Counter
stop_words = get_stopwords()
# 待处理数据放一个队列,多个线程轮流计数,最后合并统一计数
class WordFrequencyCounter:
def __init__(self, input_file):
self.word_space = queue.Queue()
@ -48,9 +49,4 @@ if __name__ == '__main__':
'''
在多线程之间传递数据建议使用线程安全的队列如queue.Queue或multiprocessing.Queue后者也适用于多进程环境
这些队列提供了线程安全的数据传输机制可以避免竞态条件和数据损坏
全局变量不可预测
multiprocessing.Queue 利用了操作系统提供的进程间通信IPC, Inter-Process Communication机制具体实现取决于不同操作系统的支持
在Unix/Linux系统中multiprocessing.Queue通常基于管道pipes共享内存和/或消息队列等机制实现
而在Windows系统上可能使用命名管道named pipes或者内存映射文件memory-mapped files以及某些版本的Windows特有的进程间同步对象如MutexesSemaphores和事件
'''

@ -2,6 +2,7 @@ import sys
import re
from collections import Counter
# 提供带参数的运行命令行环境
# 使用 python command_line_1.py testfilepath 10
# 清洗文本,移除标点符号并转换为小写
@ -43,4 +44,5 @@ def main():
print(f"Error: {e}")
if __name__ == "__main__":
main()
main()

@ -1,6 +1,9 @@
import re
from collections import Counter
# 提供一个命令行交互方式来驱动程序运行
# 清洗文本,移除标点符号并转换为小写
def clean_text(text):
return re.sub(r'[^\w\s]', '', text).lower()

@ -1,2 +0,0 @@
" my Some sure acquaintance or other, my dear, sure,other I suppose; I am sure I do not
know. sure "
Loading…
Cancel
Save