txlyre 2 ngày trước cách đây
mục cha
commit
0903fa30e5
1 tập tin đã thay đổi với 11 bổ sung1 xóa
  1. 11 1
      markov.py

+ 11 - 1
markov.py

@@ -13,6 +13,8 @@ from config import config
 
 class Markov:
     def __init__(self):
+        self.rebuilding = False
+
         self.counter = 0
 
         self.corpus = []
@@ -60,9 +62,17 @@ class Markov:
         return text.strip()
 
     def _rebuild(self):
-        self.chain = markovify.Chain(self.corpus, config.MARKOV_STATE_SIZE).compile()
+        try:
+            self.chain = markovify.Chain(self.corpus, config.MARKOV_STATE_SIZE).compile()
+        finally:
+            self.rebuilding = False
 
     def rebuild(self):
+        if self.rebuilding:
+            return
+        
+        self.rebuilding = True
+
         self.counter = 0
 
         t = threading.Thread(target=self._rebuild)