File size: 8,156 Bytes
60aeba2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import numpy as np
from numba import njit
from tqdm import tqdm
import math
import random
from matplotlib import pyplot as plt
import pickle


# whitelist = "ёйцукенгшщзхъфывапролджэячсмитьбю "

def text_to_arr(text: str):
    return np.array([ord(x) for x in text.lower()])

@njit
def longest_common_substring(s1, s2):
    current_match_start = -1
    current_match_end = -1

    best_match_start = current_match_start
    best_match_end = current_match_end

    min_len = min(len(s1), len(s2))
    for i in range(min_len):
        if s1[i] == s2[i]:
            current_match_start = current_match_end = i
            j = 0
            while s1[i+j] == s2[i+j] and i+j < min_len:
               j += 1
            current_match_end = current_match_start + j

            if current_match_end - current_match_start > best_match_end - best_match_start:
                best_match_start = current_match_start
                best_match_end = current_match_end

    return s1[best_match_start:best_match_end]

def not_found_in(q, data):
    for l in data:
        count = 0
        lq = len(q)-1
        for v in l:
            if v == q[count]:
                count += 1
            else:
                count = 0
            if count == lq:
                return False
    return True

class Layer:
    def __init__(self, mem_len: int = 100, max_size: int = 6):
        self.mem_len = mem_len
        self.common_strings = []
        self.previously_seen = []
        self.max_size = max_size+1
    def __call__(self, input_arr, training: bool = True):
        o = []
        li = len(input_arr)
        for i in range(li):
            for y, cs in enumerate(self.common_strings):
                if (i+cs.shape[0]) <= li and (input_arr[i:i+cs.shape[0]] == cs).all():
                    o.append(y)
        if training:
            cl = 0
            n = None
            for i, line in enumerate(self.previously_seen):
                t = longest_common_substring(input_arr, line)
                l = len(t)
                if l > cl and l < self.max_size:
                    cl = l
                    n = i
                    r = t
            if self.previously_seen != []:
                if n is not None and len(r) > 1:
                    self.previously_seen.pop(n)
                    if not_found_in(r, self.common_strings):
                        self.common_strings.append(r)
                self.previously_seen = self.previously_seen[-self.mem_len:]
            self.previously_seen.append(input_arr)
        return o

def comparefilter(f1, f2):
    o = 0
    hss = 0.5
    for k in f1:
        if k in f2 and k in f1:
            o += np.sum((f2[k] > hss)==(f1[k] > hss))
    return (o >= len(f1)*hss)

class StrConv:
    def __init__(self, filters: int, size: int = 4):
        self.filter_amount = filters
        self.filters = [{} for _ in range(filters)] # [{43: [3 2 0 3]},]
        self.bias = np.zeros((self.filter_amount,))
        self.size = 3
    def regularize(self):
        for n, f in enumerate(self.filters):
            for f2 in self.filters[:n]:
                if random.randint(0, 100) < 10 and comparefilter(f, f2):
                    self.filters[n] = {}
    def __call__(self, input_arr, training: bool = True, debug=False):
        if len(input_arr) <= self.size:
            return []
        o = np.zeros((input_arr.shape[0]-self.size, self.filter_amount))
        for i in range(input_arr.shape[0]-self.size):
            for n, c in enumerate(input_arr[i:i+self.size]):
                for fn, f in enumerate(self.filters):
                    if c in f:
                        o[i, fn] += f[c][n]
        o += self.bias
        m = np.max(np.abs(o))
        if m != 0: o /= m
        if debug:
            plt.imshow(o)
            plt.show()
        if training:
            for i in range(input_arr.shape[0]-self.size):
                for n, c in enumerate(input_arr[i:i+self.size]):
                    for fn, f in enumerate(self.filters):
                        if c in f:
#                            s = np.sum(f[c])
#                            if s > 1000:
#                                f[c] = (f[c]/(s/(self.size*1000))).astype(np.int64)
                            self.filters[fn][c][n] = o[i, fn]*0.1+f[c][n]*0.9
                        else:
                            f[c] = np.random.uniform(0, 1, (self.size))
                            f[c][n] = o[i, fn]
#            for t in range(self.size, input_arr.shape[0]):
#                for f in range(self.filter_amount):
#                    self.filters[f] = o[t-self.size, f]
        """
        s = 0
        for a in self.filters:
            for b in a:
                s += np.sum(b)
        if s > 100:
            s /= self.filter_amount
            for a in self.filters:
                for b in a:
                    a[b] = (a[b]/s).astype(dtype=np.int64)
        """
        self.bias -= np.sum(o, axis=0)# / o.shape[0]
#        print(o)
        maxed = np.zeros((o.shape[0],)) # could have different outputs, not only max of o, like o>(self.size//2) or o without processing
        for i in range(maxed.shape[0]):
            maxed[i] = np.argmax(o[i])
        return maxed

with open("dataset.txt", "r") as f:
    lines = f.read().rstrip("\n").split("\n")[:40000]

w = {}
w2 = {}

c = 0

#layer = Layer(mem_len=1000, max_size=4)
#layer2 = Layer(mem_len=1000, max_size=6)

with open("l1_large.pckl", "rb") as f: layer = pickle.load(f)
with open("l2_large.pckl", "rb") as f: layer2 = pickle.load(f)
with open("w1_large.pckl", "rb") as f: w = pickle.load(f)
with open("w2_large.pckl", "rb") as f: w2 = pickle.load(f)
"""
for n, text in tqdm(enumerate(lines[:-1])):
    if text.strip() != "" and lines[n+1].strip() != "" and text != lines[n+1]:
        t = layer(text_to_arr(text), training=True)
        t = layer(text_to_arr(text), training=False)
        c += 1
#        if c == 10:
#            c = 0
#            layer.regularize()
#            layer2.regularize()
        if len(t) != 0:
            t2 = layer2(np.array(t), training=True)
            t2 = layer2(np.array(t), training=False)
            for a in t2:
                if a in w2:
                    w2[a].append(n+1)
                else:
                    w2[a] = [n+1,]
        for a in t:
            if a in w:
                w[a].append(n+1)
            else:
                w[a] = [n+1,]

for n, text in tqdm(enumerate(lines[:200])):
    if text.strip() != "" and lines[n+1].strip() != "" and text != lines[n+1]:
        t = layer(text_to_arr(text), training=True)
        t = layer(text_to_arr(text), training=False)
        c += 1
#        if c == 10:
#            c = 0
#            layer.regularize()
#            layer2.regularize()
        if len(t) != 0:
            t2 = layer2(np.array(t), training=True)
            t2 = layer2(np.array(t), training=False)
            for a in t2:
                if a in w2:
                    w2[a].append(n+1)
                else:
                    w2[a] = [n+1,]
        for a in t:
            if a in w:
                w[a].append(n+1)
            else:
                w[a] = [n+1,]

with open("l1_large.pckl", "wb") as f: pickle.dump(layer, f)
with open("l2_large.pckl", "wb") as f: pickle.dump(layer2, f)
with open("w1_large.pckl", "wb") as f: pickle.dump(w, f)
with open("w2_large.pckl", "wb") as f: pickle.dump(w2, f)
"""
# print(layer.filters)

#for arr in layer.common_strings:
#    print(''.join([chr(a) for a in arr]))

print(len(lines), "responses available")

import threeletterai

while True:
    msg = input("Message: ")
    if len(msg) < 4:
        print(threeletterai.getresp(msg))
        continue
    processed = layer(text_to_arr(msg), training=False)
    processed = np.array(processed)
    processed2 = layer2(processed, training=False)
#    print(processed)
#    print(processed2)
    o = np.zeros(len(lines), dtype=np.int16)
    for a in processed:
        if a in w:
            o[w[a]] += 1
    for a in processed2:
        if a in w2:
            o[w2[a]] += 1
    print(lines[np.argmax(o)], f"       {np.max(o)} sure")