-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (92 loc) · 3.4 KB
/
main.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
from numpy import ones
def complement(line):
half = []
for el in line:
half.append(1 - el)
return half
def create_complement(matrix):
for line in matrix:
line.extend(complement(line))
return matrix
def run(vector, weights):
responses = []
for weight in weights:
total = 0
for pos, element in enumerate(vector):
total = total + min(element, weight[pos])
responses.append(total / (alpha + sum(weight)))
return responses
def choose_category(start, responses):
chosen = start
for index, response in enumerate(responses):
if response > responses[chosen]:
chosen = index
return chosen
def is_category_valid(line, weights, rho):
total = 0
for current, element in enumerate(line):
total = total + min(element, weights[current])
x = total / sum(line)
return x >= rho
def get_valid_category(vector, weights, responses, rho):
current = choose_category(0, responses)
while not is_category_valid(vector, weights[current], rho):
responses[current] = 0
current = choose_category(current + 1, responses)
return current
def update(matrix, weights, res_pos):
weights[res_pos] = beta * [min(element, weights[res_pos][pos])for pos, element in enumerate(matrix[res_pos])] \
+ (1 - beta) * weights[res_pos]
# Função para definir se letras estão lado a lado no teclado
def is_lateral(fchar, schar):
for line in KEYBOARD:
for pos, char in enumerate(line):
if char == fchar:
if schar == line[pos - 1] or schar == line[pos + 1]:
return 1
return 0
# Tratamento das entradas
def input_words(words, weights, rho):
first_length = len(words[0])
if first_length != len(words[1]):
raise Exception('Palavras devem ter o mesmo tamanho!')
first_input = (first_length - 1) / first_length
for pos, char in enumerate(words[0]):
if char != words[1][pos]:
second_input = is_lateral(char.upper(), words[1][pos].upper())
inputs = [first_input, second_input]
inputs.extend(complement(inputs))
print(f'Teste: {words[0]} vs {words[1]}')
print(f'Input 1: {first_input}, Input 2: {second_input}')
res = run(inputs, weights)
cat = get_valid_category(inputs, weights, res, rho)
print(f'Output: {res[cat]}({data[cat]})')
KEYBOARD = [['', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', ''],
['', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ''],
['', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '']]
data = [[1, 1],
[0.3, 0]]
data = create_complement(data)
ya = []
pa = 0.7
alpha = 0.1
beta = 1
wa = ones((len(data), len(data[0])))
for pos in range(len(data)):
res_a = 0
# ARTa
res_a = run(data[pos], wa)
J = get_valid_category(data[pos], wa, res_a, pa)
ya.append([0 for _ in data])
ya[pos][J] = 1
update(data, wa, J)
print("Training:")
print(wa)
# Teste 1: soja vs soka. Entradas resultantes 0,75 e 1.
input_words(['soja', 'soka'], wa, pa)
# Teste 2: soja vs soda
input_words(['soja', 'soda'], wa, pa)
# Teste 3: pneumoultramicroscopicossilicovulcanoconiotico vs pneumoultramicroscopicossilicovulcanoconioticp
input_words(['pneumoultramicroscopicossilicovulcanoconiotico', 'pneumoultramicroscopicossilicovulcanoconioticp'], wa, pa)
# Teste 4: james vs jimes
input_words(['james', 'jsmes'], wa, pa)