Last active
May 10, 2018 20:29
-
-
Save Soupstraw/bd662290bc6518d26a128b558bc4a3c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| from functools import reduce | |
| from collections import defaultdict | |
| from random import randint | |
| usr_dict = defaultdict(list) | |
| data = [] | |
| # Parse chat logs | |
| for filename in ['94.html', '61.html', '36.html']: | |
| file_data = re.findall(r'<span class="user">(.*?)</span>.*?<p>(.*?)<\/p>', | |
| reduce(str.__add__, | |
| reversed(open(filename).readlines()))) | |
| data += list(reduce(list.__add__, map(lambda x: list(map(lambda y: (x[0], y), x[1].split())), file_data))) | |
| # Generate markov chain | |
| last_msg = data[0] | |
| for entry in data[1:]: | |
| usr_dict[last_msg].append(entry) | |
| last_msg = entry | |
| last_sender = None | |
| last_word = None | |
| while True: | |
| if last_word is None: | |
| keys = list(usr_dict.keys()) | |
| last_word = keys[randint(0, len(keys))] | |
| # Draw next word from pool | |
| msg_pool = usr_dict[last_word] | |
| last_word = msg_pool[randint(0, len(msg_pool)-1)] | |
| # Check whether sender has changed | |
| if last_word[0] != last_sender: | |
| # Print sender name | |
| print('\n\n'+last_word[0]) | |
| last_sender = last_word[0] | |
| print(last_word[1] + ' ', end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment