-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html-old
222 lines (199 loc) · 7.67 KB
/
index.html-old
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
<html>
<head>
<title>Neural.js</title>
<!--<link rel="stylesheet" href="/css/style.css">-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="result"></div>
<script type="text/javascript">
// Global
var network, Network;
var network_shorthand_map = {
'word-topic': 'wt'
, 'topic-action': 'ta'
};
$(document).ready(function() {
network = new TopicNetwork();
});
var TopicNetwork = function() {
var request = {};
request.database = "Neural-021";
request.description = "Basic MLP neural network."
Network = new Neural( request );
TopicNetwork.prototype.shorthand.map.set( network_shorthand_map );
//TODO: extend shorthand
};
// Adds one node to the Network
TopicNetwork.prototype.node = TopicNetwork.prototype.node || {};
TopicNetwork.prototype.node.add = function( request ) {
var nodes = request.nodes;
var on_success = function( result ) {
console.log( 'TopicNetwork.prototype.node.add success', result );
if( 'function' === typeof request.on_success ) {
request.on_success( result );
}
};
var on_error = function( context ) {
console.log( 'TopicNetwork.prototype.node.add error', context );
if( 'function' === typeof request.on_error ) {
request.on_error( result );
}
};
Network.neurons.add( { 'nodes': nodes, 'on_success': on_success, 'on_error': on_error } );
return this;
}
// Adds one or more topic nodes to the network
TopicNetwork.prototype.nodes = TopicNetwork.prototype.nodes || {};
TopicNetwork.prototype.nodes.populate = function( request ) {
var input = request.input;
var output = request.output;
if( 'undefined' === typeof input || 'undefined' === typeof output ) {
return null;
}
if( 'string' == typeof request.input ) {
request.input = [ request.input ];
}
if( 'string' == typeof request.output ) {
request.output = [ request.output ];
}
TopicNetwork.prototype.nodes.create( { 'input': request.input, 'output': request.output } );
};
/*
* Example:
* input (total: 3) = [ 'iPod', 'competitor', 'Zune' ]
* output (total: 2) = [ 'Apple', 'Microsoft' ]
* hidden = (total: 6) = [ [ 'Apple', 'iPod' ], [ 'iPod', 'Microsoft' ], [ 'Apple', 'competitor' ], [ 'competitor', 'Microsoft' ], [ 'Apple', 'Zune' ], [ 'Microsoft', 'Zune' ] ]
* connections (total: 6) =
iPod (input, total: 2) -> [ [ 'Apple', 'iPod' ], [ 'iPod', 'Microsoft' ] ]
competitor (input, total: 2) -> [ [ 'Apple', 'competitor' ], [ 'competitor', 'Microsoft' ] ]
Zune (input, total: 2) -> [ [ 'Apple', 'Zune' ], [ 'Microsoft', 'Zune' ] ]
//FALSE: Apple (output, total: 3) -> [ [ 'Apple', 'iPod' ], [ 'Apple', 'competitor', ], [ 'Apple', 'Zune' ] ]
//FALSE: Microsoft (output, total: 3) -> [ [ 'iPod', 'Microsoft' ], [ 'competitor', 'Microsoft' ], [ 'Microsoft', 'Zune' ] ]
*/
TopicNetwork.prototype.nodes.create = function( request ) {
var input = request.input;
var input_length = input.length;
var output = request.output;
var output_length = input.length;
if( !( ouput_length > 0 || input_length > 0 ) ) {
return null;
}
var input_nodes = {};
var output_nodes = {};
var nodes = [];
// Add 1 node for each input and output, and a hidden node combination
// Total # of nodes should be input.length + output.length + ( input.length * output.length )
if( input_length > 0 ) {
//change input to keyed dictionary objects key becomes hash, value is payload of node
for( var x = 0; x < input_length; x++ ) {
var val = input[ x ];
var hash = TopicNetwork.prototype.utils.topicToId( val );
nodes.push( { 'network': network_shorthand[ 'word-topic' ], 'type': 'input', 'hash': val, 'payload': val } );
// Add a pairwise hidden node
for( var y = 0; y < input_length; y++ ) {
var value = [ val, input[ y ] ];
var hash = TopicNetwork.prototype.utils.topicsToId( value );
nodes.push( { 'type': 'hidden', 'hash': hash, 'payload': value } );
}
}
}
if( output_length > 0 ) {
for( var x = 0; x < output_length; x++ ) {
//output[ x ];
output_nodes.push( {} );
}
}
// Add 1 connection between each input and hidden node, and each output and hidden node
// Total # of connections should be input.length^2
for( var x = 0; x < hidden_nodes.length; x++ ) {
for( var x = 0; x < input_nodes.length; x++ ) {
//if input is part of hidden node
}
}
return this;
}
TopicNetwork.prototype.nodes.add = function( request ) {
var topics = request.topics || request.topic;
var nodes = [];
if( 'undefined' === typeof topics.length ) {
var hash = topics.hash;
hash = ( 'undefined' !== typeof hash ) ? hash : topics;
var type = topics.type;
type = ( 'undefined' !== typeof type ) ? type : 'hidden';
nodes.push( {
'type': type
, 'hash': hash
} );
} else {
for( var x = 0; x < topics.length; x++ ) {
var topic = topics[ x ];
var hash = topic.hash;
hash = ( 'undefined' !== typeof hash ) ? hash : topic;
var type = topics.type;
type = ( 'undefined' !== typeof type ) ? type : 'hidden';
nodes.push( {
'type': type
, 'hash': hash
} );
}
}
var on_success = function( result ) {
console.log( 'TopicNetwork.prototype.topic.add success', result );
if( 'function' === typeof request.on_success ) {
request.on_success( result );
}
};
var on_error = function( context ) {
console.log( 'TopicNetwork.prototype.topic.add error', context );
if( 'function' === typeof request.on_error ) {
request.on_error( result );
}
};
var on_complete = function() {
console.log( 'TopicNetwork.prototype.topic.add complete' );
if( 'function' === typeof request.on_complete ) {
request.on_complete();
}
};
for( var x = 0; x < nodes.length; x++ ) {
TopicNetwork.prototype.node.add( { 'nodes': nodes, 'on_success': on_success, 'on_error': on_error } );
}
if( 'undefined' !== typeof on_complete ) {
on_complete();
}
return this;
};
TopicNetwork.prototype.utils = TopicNetwork.prototype.utils || {};
TopicNetwork.prototype.utils.alphaSortArray = function( unsorted ) {
return unsorted.sort( TopicNetwork.prototype.utils.alphaSort );
};
TopicNetwork.prototype.utils.alphaSort = function( a, b ) {
// http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
var aA = a.replace(reA, "");
var bA = b.replace(reA, "");
if(aA === bA) {
var aN = parseInt(a.replace(reN, ""), 10);
var bN = parseInt(b.replace(reN, ""), 10);
return aN === bN ? 0 : aN > bN ? 1 : -1;
} else {
return aA > bA ? 1 : -1;
}
}
// Takes an array of topic_ids and returns a unique hash
TopicNetwork.prototype.utils.topicsToId = function( topics ) {
var sorted_topics = TopicNetwork.prototype.utils.alphaSortArray( topics );
return md5( sorted_topics.join("|") );
};
// Takes an array of topic_ids and returns a unique hash
TopicNetwork.prototype.utils.topicToId = function( topic_id ) {
return md5( topic_id );
};
</script>
<script type="text/javascript" src="http://github.com/editor/TopicNetwork.prototype/raw/master/indb.dev.js"></script>
<script type="text/javascript" src="https://github.com/wbond/md5-js/raw/master/md5.js"></script>
<script type="text/javascript" src="https://github.com/editor/Neural/raw/master/neural.dev.js"></script>
</body>
</html>