txlyre 6 days ago
parent
commit
2f2a4f4c29
1 changed files with 18 additions and 8 deletions
  1. 18 8
      markov.py

+ 18 - 8
markov.py

@@ -35,11 +35,13 @@ class Markov:
             init_state = tuple(init_state)
             init_state = tuple(init_state)
 
 
             size = len(init_state)
             size = len(init_state)
-          
-            if size < config.MARKOV_STATE_SIZE:                
-                init_state = (markovify.chain.BEGIN,) * (config.MARKOV_STATE_SIZE - size) + init_state            
+
+            if size < config.MARKOV_STATE_SIZE:
+                init_state = (markovify.chain.BEGIN,) * (
+                    config.MARKOV_STATE_SIZE - size
+                ) + init_state
             elif size > config.MARKOV_STATE_SIZE:
             elif size > config.MARKOV_STATE_SIZE:
-                init_state = init_state[:-config.MARKOV_STATE_SIZE]
+                init_state = init_state[: -config.MARKOV_STATE_SIZE]
 
 
         words = self.chain.walk(init_state)
         words = self.chain.walk(init_state)
         if not words:
         if not words:
@@ -47,7 +49,9 @@ class Markov:
 
 
         text = orig_init_state if orig_init_state is not None else ""
         text = orig_init_state if orig_init_state is not None else ""
         for word in words:
         for word in words:
-            if word in "-–—" or not all(c in string.punctuation or c == "…" for c in word):
+            if word in "-–—" or not all(
+                c in string.punctuation or c == "…" for c in word
+            ):
                 text += " "
                 text += " "
 
 
             text += word
             text += word
@@ -88,12 +92,18 @@ class Markov:
         if text not in self.corpus:
         if text not in self.corpus:
             self.corpus.insert(0, text)
             self.corpus.insert(0, text)
 
 
-        if len(self.corpus) > config.MARKOV_CORPUS_SIZE:
-            self.corpus.pop(-1)
+        if (
+            config.MARKOV_CORPUS_SIZE > 0
+            and len(self.corpus) > config.MARKOV_CORPUS_SIZE
+        ):
+            self.corpus = self.corpus[: config.MARKOV_CORPUS_SIZE]
 
 
         self.counter += 1
         self.counter += 1
 
 
-        if self.counter % config.MARKOV_REBUILD_RATE == 0:
+        if (
+            config.MARKOV_REBUILD_RATE > 0
+            and self.counter % config.MARKOV_REBUILD_RATE == 0
+        ):
             self.rebuild()
             self.rebuild()
 
 
     def load(self):
     def load(self):