-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
120 lines (110 loc) · 2.75 KB
/
bot.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
import random
import twitter
import config
files = [
'resources/austen.txt',
'resources/cyborg_manifesto.txt',
'resources/glove.txt',
'resources/emily.txt',
'resources/spaaace.txt',
'resources/fungi.txt',
'resources/doppler.txt',
'resources/jane.txt',
'resources/logic.txt',
'resources/differential_equations.txt',
'resources/ethereum.txt',
'resources/georgette.txt'
]
api = twitter.Api(consumer_key=config.consumer_key,
consumer_secret=config.consumer_secret,
access_token_key=config.access_token_key,
access_token_secret=config.access_token_secret)
def get_files_content():
text = ''
for file in files:
with open(file, 'r') as file:
text += file.read()
return text
def build_word_chain(text):
text_list = text.split()
word_chain = {}
for i in range(len(text_list) - 3):
prefix = (text_list[i], text_list[i + 1])
suffix = text_list[i + 2]
if prefix in word_chain:
word_chain[prefix].append(suffix)
else:
word_chain[prefix] = [suffix]
return word_chain
def markov(word_chain):
prefixes = [
'I am',
'He is',
'They found',
'So my',
'It is',
'Unlike the',
'The second',
'To think',
'For the',
'In this',
'There was',
'Meanwhile, the',
'As this',
'But fortunately',
'It appears',
'As to',
'A case',
'The affinity',
'In all',
'A common',
'The reason',
'On that',
'From my',
'It has',
'The only',
'But since',
'A higher',
'For most',
'So was',
'Up the',
'The two',
'This question',
'The form',
'And as',
'From this',
'Nothing therefore',
'This question',
'This example',
'In such',
'The uses',
'The little',
'Since there',
'Yet there',
'Upon pressure',
'So far',
'A world',
'And analogy',
'A steam-engine',
'The next'
]
sentence = random.choice(prefixes)
prefix = tuple(sentence.split())
while True:
suffix = random.choice(word_chain[prefix])
if len(sentence) + len(suffix) + 1 >= 280:
sentence += '.'
break
sentence += ' ' + suffix
prefix = (prefix[1], suffix)
return sentence
def main():
file_content = get_files_content()
word_chain = build_word_chain(file_content)
tweet = markov(word_chain)
status = api.PostUpdate(tweet)
print(tweet)
print(status)
if __name__ == "__main__":
main()