|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
################ 待整理
|
|
|
|
|
'''
|
|
|
|
|
多线程各个模块比较乱的但是协作序贯的完成了数据处理
|
|
|
|
|
各个组件完全不能互操作,仅依靠队列发消息进行协作
|
|
|
|
@ -10,15 +11,15 @@ from threading import Thread
|
|
|
|
|
from queue import Queue
|
|
|
|
|
from cppy.cp_util import *
|
|
|
|
|
|
|
|
|
|
class ActiveWFObject(Thread):
|
|
|
|
|
class ThreadObject(Thread):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.queue = Queue()
|
|
|
|
|
self._stopMe = False
|
|
|
|
|
self._over = False
|
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
while not self._stopMe:
|
|
|
|
|
while not self._over:
|
|
|
|
|
message = self.queue.get()
|
|
|
|
|
self._dispatch(message)
|
|
|
|
|
if message[0] == 'over':
|
|
|
|
@ -27,8 +28,7 @@ class ActiveWFObject(Thread):
|
|
|
|
|
def send(receiver, message):
|
|
|
|
|
receiver.queue.put(message)
|
|
|
|
|
|
|
|
|
|
class DataStorageManager(ActiveWFObject):
|
|
|
|
|
""" Models the contents of the file """
|
|
|
|
|
class TxtManager(ThreadObject):
|
|
|
|
|
_data = ''
|
|
|
|
|
|
|
|
|
|
def _dispatch(self, message):
|
|
|
|
@ -37,7 +37,6 @@ class DataStorageManager(ActiveWFObject):
|
|
|
|
|
elif message[0] == 'send_word_freqs':
|
|
|
|
|
self._process_words(message[1:])
|
|
|
|
|
else:
|
|
|
|
|
# forward
|
|
|
|
|
send(self._stop_word_manager, message)
|
|
|
|
|
|
|
|
|
|
def _init(self, message):
|
|
|
|
@ -50,8 +49,8 @@ class DataStorageManager(ActiveWFObject):
|
|
|
|
|
send(self._stop_word_manager, ['filter', w])
|
|
|
|
|
send(self._stop_word_manager, ['topWord', recipient])
|
|
|
|
|
|
|
|
|
|
class StopWordManager(ActiveWFObject):
|
|
|
|
|
""" Models the stop word filter """
|
|
|
|
|
|
|
|
|
|
class FilterManager(ThreadObject):
|
|
|
|
|
_stop_words = []
|
|
|
|
|
|
|
|
|
|
def _dispatch(self, message):
|
|
|
|
@ -60,7 +59,6 @@ class StopWordManager(ActiveWFObject):
|
|
|
|
|
elif message[0] == 'filter':
|
|
|
|
|
return self._filter(message[1:])
|
|
|
|
|
else:
|
|
|
|
|
# forward
|
|
|
|
|
send(self._word_freqs_manager, message)
|
|
|
|
|
|
|
|
|
|
def _init(self, message):
|
|
|
|
@ -72,8 +70,7 @@ class StopWordManager(ActiveWFObject):
|
|
|
|
|
if word not in self._stop_words:
|
|
|
|
|
send(self._word_freqs_manager, ['word', word])
|
|
|
|
|
|
|
|
|
|
class WordFrequencyManager(ActiveWFObject):
|
|
|
|
|
""" Keeps the word frequency data """
|
|
|
|
|
class WFManager(ThreadObject):
|
|
|
|
|
_word_freqs = {}
|
|
|
|
|
|
|
|
|
|
def _dispatch(self, message):
|
|
|
|
@ -91,8 +88,7 @@ class WordFrequencyManager(ActiveWFObject):
|
|
|
|
|
freqs_sorted = sort_dict ( self._word_freqs )
|
|
|
|
|
send(recipient, ['topWord', freqs_sorted])
|
|
|
|
|
|
|
|
|
|
class MyController(ActiveWFObject):
|
|
|
|
|
|
|
|
|
|
class MyController(ThreadObject):
|
|
|
|
|
def _dispatch(self, message):
|
|
|
|
|
if message[0] == 'run':
|
|
|
|
|
self._run(message[1:])
|
|
|
|
@ -109,13 +105,13 @@ class MyController(ActiveWFObject):
|
|
|
|
|
word_freqs, = message
|
|
|
|
|
print_word_freqs( word_freqs)
|
|
|
|
|
send(self._storage_manager, ['over'])
|
|
|
|
|
self._stopMe = True
|
|
|
|
|
self._over = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
word_freq_manager = WordFrequencyManager()
|
|
|
|
|
stop_word_manager = StopWordManager()
|
|
|
|
|
storage_manager = DataStorageManager()
|
|
|
|
|
word_freq_manager = WFManager()
|
|
|
|
|
stop_word_manager = FilterManager()
|
|
|
|
|
storage_manager = TxtManager()
|
|
|
|
|
wfcontroller = MyController()
|
|
|
|
|
|
|
|
|
|
send(storage_manager, ['init', testfilepath, stop_word_manager])
|
|
|
|
|