-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
89 lines (65 loc) · 1.61 KB
/
app.rb
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
require 'sinatra'
require 'matrix'
require 'erb'
require 'mongo'
require 'json'
require_relative 'Models/Block.rb'
require_relative 'Models/Weights.rb'
enable :sessions
set :session_secret, '*&(^B234'
get '/' do
@blocks = Block.new.find_all
session[:async] = 0
update_weight_matrix
erb :index
end
post '/get_image_data' do
id = params[:id]
block = Block.new.find_by_id(id)
{data: block['data']}.to_json
end
post '/add_block' do
data = params[:data]
Block.new.add(data)
end
post '/recognize' do
data = params[:data]
recognize(data.scan(/-*\d/))
end
def update_weight_matrix
blocks = Block.new.find_all
weights = Matrix.build(100, 100) { 0 }
blocks.each do |block|
n = block['data'].size + 1
data = block['data'].scan(/-*\d/)
input_x = Matrix[data].map { |el| el.to_i }.t
weights += input_x * input_x.t
end
weights = weights.to_a
weights.size.times do |i|
weights[i][i] = 0
end
Weights.new.change_weights(weights)
end
def recognize(data)
session[:async] = 0 unless session[:async]
async = session[:async]
weights = Matrix.rows(Weights.new.find_one['data'].slice(async * 10, 10))
x = Matrix[data.map { |el| el.to_i }.flatten]
answer = activation(x * weights.t)
session[:async] = inc_async(async)
array = answer.to_a.flatten
data = x.to_a.flatten.map.with_index do |el, index|
(async * 10...async * 10 + 10) === index ? array.shift : el
end
{
data: data.join(''),
async: session[:async]
}.to_json
end
def inc_async(async)
async < 10 ? async + 1 : 0
end
def activation(vector)
vector.map { |element| element >= 0 ? 1 : -1 }
end