Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1.    async def markovGen(self):
  2.             result = ""
  3.             randomized_int = random.randint(1, 602)
  4.             with open("modules/markov/markov ({}).txt".format(randomized_int), ) as file:
  5.                 #return returnable_file
  6.                 length = 50
  7.                 #print(file.name)
  8.                 word_dictionary = self.learn(file.read())
  9.                 punctuation = False
  10.                 last_word = "~~~~~~~~~~~~~~~~"
  11.  
  12.                 while not punctuation:
  13.                     new_word = self.get_next_word(last_word, word_dictionary).rstrip()
  14.                     result = result + " " + new_word
  15.                     result.replace("\r\n", '')
  16.                     last_word = new_word
  17.  
  18.                     if len(result.split(" ")) > random.randint(4,9) and any(punct in result for punct in ["!", ".", "?", "\n", "\r\n"]):
  19.                         punctuation = True
  20.             await self.client.say(result)
  21.  
  22.  
  23.     def learn(self, input):  
  24.         dict = {}
  25.         word_tokens = input.split(" ")
  26.  
  27.         for i in range(0, len(word_tokens)-1):
  28.             current_word = word_tokens[i]
  29.             next_word = word_tokens[i+1]
  30.  
  31.             if current_word not in dict:
  32.                 # Create new entry in dictionary
  33.                 dict[current_word] = {next_word : 1}
  34.             else:
  35.                 # Current word already exists
  36.                 all_next_words = dict[current_word]
  37.  
  38.                 if next_word not in all_next_words:
  39.                     # Add new next state (word)
  40.                     dict[current_word][next_word] = 1
  41.                 else:
  42.                     # Already exists, just increment
  43.                     dict[current_word][next_word] = dict[current_word][next_word] + 1
  44.  
  45.         return dict
  46.    
  47.     def get_next_word(self, last_word, dict):
  48.         if last_word not in dict:
  49.             # Random
  50.             new_word = self.pick_random(dict)
  51.             return new_word
  52.         else:
  53.             # Pick next word from list
  54.             candidates = dict[last_word]
  55.             candidates_normalised = []
  56.  
  57.             for word in candidates:
  58.                 freq = candidates[word]
  59.                 for i in range(0, freq):
  60.                     candidates_normalised.append(word)
  61.  
  62.             rnd = random.randint(0, len(candidates_normalised)-1)
  63.             return candidates_normalised[rnd]
  64.  
  65.  
  66.     def pick_random(self, dict):
  67.         random_num = random.randint(0, len(dict)-1)
  68.         new_word = list(dict.keys())[random_num]
  69.         return new_word
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement