forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marakov_Chain.py
203 lines (199 loc) · 6.45 KB
/
Marakov_Chain.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pickle
import random
import os
import sys
import time
import CORE_DATA
def merge(d1, d2, merger=lambda x,y:x+y):
#http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-as-a-single-expression
result = dict(d1)
for k,v in d2.iteritems():
if k in result:
result[k] = merger(result[k], v)
else:
result[k] = v
return result
full_data = {}
imported_data = {}
try:
tiedostot = os.listdir("Marakov")
except:
os.mkdir("Marakov")
tiedostot = os.listdir("Marakov")
else:
pass
listaus = []
for i in tiedostot:
if "marakov." not in i.lower():
pass
else:
listaus.append(i)
for i in listaus:
tiedosto = open("Marakov/"+i,"r")
old_size = len(full_data.keys())
if i != "Marakov.Cache":
imported_data = merge(imported_data,pickle.load(tiedosto))
print "Added contents of "+i+" (Import)"
print "Entries: "+str(len(imported_data))
else:
full_data = merge(full_data,pickle.load(tiedosto))
new_size = len(full_data.keys())
print "Added contents of "+i
print "Entries: "+str(new_size-old_size)
time.sleep(0.1)
def give_data(data):
state = False
for a,b in zip(data.split(" "),data.split(" ")[1:]):
a = a.lower().replace(",","").replace(".","").replace("?","").replace("!","").replace("(","").replace(")","").replace("[","").replace("]","").replace('"',"").replace("'","")
b = b.lower().replace(",","").replace(".","").replace("?","").replace("!","").replace("(","").replace(")","").replace("[","").replace("]","").replace('"',"").replace("'","")
if a not in [CORE_DATA.prefix+"marakov"]+CORE_DATA.SName:
state = True
if a[:7] == "http://" or a[:7] == "http:\\\\" or a[:4] == "www.":
pass
else:
try:
if b not in full_data[a]:
full_data[a].append(b)
except:
try:
if b not in imported_data[a]:
pass
except:
full_data[a] = []
full_data[a].append(b)
if state == True:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def form_sentence(argument=None):
length = 0
attempts = 0
while attempts < 20:
sentence = []
if argument != None:
a = argument
else:
try:
a = random.choice(full_data.keys())
except IndexError:
try:
b = random.choice(imported_data.keys())
except IndexError:
attempts = 999
return "No sentences formable at all"
sentence.append(a)
length = 0
attempts += 1
while length < 12 or sentence[-1].lower() in ["but","who","gets","im","most","is","it","if","then","after","over","every","of","on","or","as","the","wheather","whether","a","to","and","for"] and length < 24:
try:
b = random.choice(full_data[a])
except:
try:
b = random.choice(imported_data[a])
except IndexError:
break
except KeyError:
break
else:
sentence.append(b)
length += 1
a = b
else:
sentence.append(b)
length += 1
a = b
if len(sentence) > 5:
argument = None
return sentence
else:
pass
argument = None
return sentence
def remdata(arg):
try:
del(full_data[arg])
except:
print "There is no such data"
else:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def remobject(arg1,arg2):
try:
del(full_data[arg1][full_data[arg1].index(arg2)])
except ValueError:
print "No such object"
except KeyError:
print "No such data"
else:
tiedosto = open("Marakov/Marakov.Cache","w")
pickle.dump(full_data,tiedosto)
tiedosto.close()
def convert(filename_from,filename_to):
try:
tiedosto = open(filename_from,"r")
data = pickle.load(tiedosto)
tiedosto.close()
except:
try:
tiedosto.close()
except:
pass
print "Error!"
else:
for lista in data.keys():
try:
a = lista[-1]
except IndexError:
pass
else:
if lista[-1] in """",.?!'()[]{}""" and not lista.islower():
if lista[:-1].lower() in data.keys():
data[lista[:-1].lower()] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista[:-1].lower()
del(data[lista])
else:
data[lista[:-1].lower()] = data[lista]
print lista+" Is now "+lista[:-1].lower()
del(data[lista])
elif lista[-1] in """",.?!'()[]{}""" and lista.islower():
if lista[:-1] in data.keys():
data[lista[:-1]] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista[:-1]
del(data[lista])
else:
data[lista[:-1]] = data[lista]
print lista+" Is now "+lista[:-1]
del(data[lista])
elif not lista.islower():
if lista.lower() in data.keys():
data[lista.lower()] += data[lista]
print "Added "+str(len(data[lista]))+" Objects from "+lista+" To "+lista.lower()
del(data[lista])
else:
data[lista.lower()] = data[lista]
print lista+" Is now "+lista.lower()
del(data[lista])
for a in data.keys():
for b in data[a]:
if b.lower()[:7] == "http://" or b.lower()[:7] == "http:\\\\" or b.lower()[:4] == "www.":
data[a].pop(b)
else:
try:
if b[-1] in """",.?!'()[]{}""" and not b.islower() and not b.isdigit():
data[a].pop(data[a].index(b))
data[a].append(b[:-1].lower())
print a+" | "+b +" -> "+b[:-1].lower()
elif b[-1] in """",.?!'()[]{}""" and b.islower():
data[a].pop(data[a].index(b))
data[a].append(b[:-1].lower())
print a+" | "+b +" -> "+b[:-1]
elif not b.islower() and not b.isdigit():
data[a].pop(data[a].index(b))
data[a].append(b.lower())
print a+" | "+b +" -> "+b.lower()
except IndexError: #If it has no letters.. well.. yeah.
data[a].pop(data[a].index(b))
print "Removed a NULL object"
tiedosto = open(filename_to,"w")
pickle.dump(data,tiedosto)