-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
94 lines (62 loc) · 2.17 KB
/
markov.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""A Markov chain generator that can tweet random messages."""
import sys
from random import choice
import os
import discord
def open_and_read_file(filenames):
"""Take list of files. Open them, read them, and return one long string."""
body = ''
for filename in filenames:
text_file = open(filename)
body = body + text_file.read()
text_file.close()
return body
def make_chains(text_string):
"""Take input text as string; return dictionary of Markov chains."""
chains = {}
words = text_string.split()
for i in range(len(words) - 2):
key = (words[i], words[i + 1])
value = words[i + 2]
if key not in chains:
chains[key] = []
chains[key].append(value)
return chains
def make_text(chains):
"""Take dictionary of Markov chains; return random text."""
keys = list(chains.keys())
key = choice(keys)
words = [key[0], key[1]]
while key in chains:
# Keep looping until we have a key that isn't in the chains
# (which would mean it was the end of our original text).
# Note that for long texts (like a full book), this might mean
# it would run for a very long time.
word = choice(chains[key])
words.append(word)
key = (key[1], word)
return ' '.join(words)
# Get the filenames from the user through a command line prompt, ex:
# python markov.py green-eggs.txt shakespeare.txt
filenames = sys.argv[1:]
if not filenames:
print("Please provide a filename on the command line! Ex. python3 markov.py green-eggs.txt")
exit(1)
# Open the files and turn them into one long string
text = open_and_read_file(filenames)
# Get a Markov chain
chains = make_chains(text)
# Bot code
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Successfully connected! Logged in as {client.user}.')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run(os.environ['DISCORD_TOKEN'])