erinx

Untitled

Jan 29th, 2022 (edited)
338
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ----------- Cortex Requires ------------ //
  2. // ------- Load Cortex Requirements ------- //
  3. // ---------------- Begin: ---------------- //
  4.  
  5. /*------ installing ------//
  6. Synaptic: $ npm install synaptic
  7. Sha.js: $ npm install sha.js
  8. Express: $ npm install express --save
  9. //------------------------*/
  10.  
  11. var synaptic = require('synaptic');
  12. var Neuron = synaptic.Neuron,
  13.     Layer = synaptic.Layer,
  14.     Network = synaptic.Network,
  15.     Trainer = synaptic.Trainer,
  16.     Architect = synaptic.Architect;
  17. var createHash = require('sha.js')
  18. var express = require('express')
  19.  
  20. // ------------ Cortex Define ------------- //
  21. // -------- Cortex Code Begins Here ------- //
  22. // ---------------- Begin: ---------------- //
  23.  
  24. function Cortex(chars, bin){
  25.   this.splash = true;
  26.   this.chars = chars;
  27.   if(bin && bin == true) this.bin = true;
  28.   if(this.bin && this.bin == true){
  29.     this.bits = this.chars;
  30.   }else{
  31.     this.bits = (this.chars * 8);
  32.   }
  33.   this.cortex = new Architect.Hopfield(this.bits)
  34.   this.dataSets = [];
  35. }
  36.  
  37. Cortex.prototype = {
  38.   build : function(){
  39.     var dataSet = [];
  40.     for(key in this.dataSets){
  41.       dataSet.push(this.dataSets[key]['data']);
  42.     }
  43.     if(this.splash == true){
  44.       console.log(" ");
  45.       console.log(' - CognitionLab Cortex application layer © CognitionLab LTD. & E.S.R.MacDonald 2017.');
  46.       console.log(' - More info at https://cognitionlab.ltd/cortex');
  47.       console.log(" ");
  48.       console.log("#############################################");
  49.       console.log(" ");
  50.       console.log("  #####                #                     ");
  51.       console.log(" #     #  ####   ####  #         ##   #####  ");
  52.       console.log(" #       #    # #    # #        #  #  #    # ");
  53.       console.log(" #       #    # #      #       #    # #####  ");
  54.       console.log(" #       #    # #  ### #       ###### #    # ");
  55.       console.log(" #     # #    # #    # #       #    # #    # ");
  56.       console.log("  #####   ####   ####  ####### #    # #####  ");
  57.       console.log("                                             ");
  58.       console.log("  #####                                      ");
  59.       console.log(" #     #  ####  #####  ##### ###### #    #   ");
  60.       console.log(" #       #    # #    #   #   #       #  #    ");
  61.       console.log(" #       #    # #    #   #   #####    ##     ");
  62.       console.log(" #       #    # #####    #   #        ##     ");
  63.       console.log(" #     # #    # #   #    #   #       #  #    ");
  64.       console.log("  #####   ####  #    #   #   ###### #    #   ");
  65.       console.log(" ");
  66.       console.log("#############################################");
  67.       console.log(' - Cortex Info:');
  68.       console.log('   CognitionLab Cortex v.1.0.0 Node.JS Application Layer');
  69.       if(this.bin && this.bin == true){
  70.         console.log('   processing ' + this.chars + ' binary bits with arbitrary tagging.');
  71.       }else{
  72.         console.log('   processing ' + this.chars + ' byte strings with arbitrary tagging.');
  73.       }
  74.       console.log(' - Neural Network Info:');
  75.       console.log('   Synaptic Hopfield core processing ' + this.bits + ' bit patterns.');
  76.       console.log("#############################################");
  77.       console.log("> Training Neural Net...");
  78.     }
  79.    
  80.     this.training = this.cortex.learn(dataSet);
  81.    
  82.     if(this.splash == true){
  83.       console.log("> Trained in " + this.training.time + "ms and " + this.training.iterations + " iterations.");
  84.       console.log("> Stable error rate: " + this.training.error + ".");
  85.       console.log("> Cortex Ready!");
  86.       console.log("#############################################");
  87.       console.log(" ");
  88.     }
  89.   },
  90.  
  91.   feed : function(dat){
  92.     var dataOut = "";
  93.     if(!this.bin) dat = this.cleanInput(dat).split("");
  94.     var feed = this.cortex.feed(dat);
  95.     for(var i = 0; i < feed.length; i++){
  96.       dataOut += feed[i];
  97.     }
  98.     return this.sha(dataOut);
  99.   },
  100.  
  101.   buildDataSet : function(data, tag){
  102.     if(this.bin){
  103.      var ide = data;
  104.     }else{
  105.      var ide = this.cleanInput(data);
  106.     }
  107.     var base = this.sha(ide);
  108.     this.dataSets[base] = [];
  109.     this.dataSets[base]['raw'] = data;
  110.     var dat = ide.split('');
  111.     this.dataSets[base]['data'] = dat;
  112.     this.dataSets[base]['tag'] = tag;
  113.   },
  114.  
  115.   load : function(data){
  116.     for(key in data){
  117.       this.buildDataSet(key, data[key]);
  118.     }
  119.   },
  120.  
  121.   toBin : function(input){
  122.     var output = "";
  123.     for (i=0; i < input.length; i++) {
  124.       var a = input[i].charCodeAt(0).toString(2);
  125.       a = new Array(9 - a.length).join('0') + a;
  126.       output += a;
  127.     }
  128.     return output;
  129.   },
  130.  
  131.   fixBit : function(input){
  132.     var output = "";
  133.     if(input.length > this.bits){
  134.       output += input.substring(this.bits, 0);
  135.     }else{
  136.       output += input;
  137.     }
  138.     while(output.length < this.bits){
  139.       output += "0";
  140.     }
  141.     return output;
  142.   },
  143.  
  144.   cleanInput : function(input){
  145.     var output = "";
  146.     input = this.toBin(input);
  147.     input = this.fixBit(input);
  148.     output += input;
  149.     return output;
  150.   },
  151.  
  152.   sha : function(input){
  153.     var sha1 = createHash('sha1');
  154.     var h = sha1.update(input, 'utf8').digest('hex');
  155.     return h;
  156.   }
  157.  
  158. };
  159.  
  160. module.exports = Cortex;
  161.  
  162. // ------------ Cortex Defined ------------ //
  163. // - You must now load datasets and build - //
  164. // ----------------- Use: ----------------- //
  165. // var cortex = new Cortex(3);
  166. // cortex.load({
  167. //   "AAA" : "some data",
  168. //   "AAB" : "more data"
  169. // });
  170. // cortex.build();
  171. // ------------- Cortex Ready ------------- //
  172.  
  173.  
  174. /* <!-- BEGIN TESTS --> */
  175.  
  176. function parseData(data){
  177.   var obj = {};
  178.   for(key in data){
  179.     var str = "";
  180.    
  181.     if(data[key]['animal'] == 'cat') str += 'a1';
  182.     if(data[key]['animal'] == 'dog') str += 'a2';
  183.    
  184.     if(data[key]['nice'] == 'friendly') str += 'b1';
  185.     if(data[key]['nice'] == 'scary') str += 'b2';
  186.    
  187.     obj[str] = "this is most likely a " + data[key]['nice'] + " " + data[key]['animal'] + ".";
  188.   }
  189.   return obj;
  190. }
  191.  
  192. var inpdata = {
  193.   "1" : {
  194.     "animal" : "cat",
  195.     "nice" : "friendly"
  196.   },
  197.   "2" : {
  198.     "animal" : "dog",
  199.     "nice" : "friendly"
  200.   },  
  201.   "3" : {
  202.     "animal" : "cat",
  203.     "nice" : "scary"
  204.   },
  205.   "4" : {
  206.     "animal" : "dog",
  207.     "nice" : "scary"
  208.   },
  209.   "5" : {
  210.     "animal" : "dog",
  211.     "nice" : "friendly"
  212.   },
  213.   "6" : {
  214.     "animal" : "cat",
  215.     "nice" : "scary"
  216.   }
  217. };
  218.  
  219. var cortex = new Cortex(4);
  220.  
  221. //cortex.splash = false;
  222.  
  223. cortex.load(
  224.   parseData(inpdata)
  225. );
  226.  
  227. cortex.build();
  228.  
  229. function test(input){
  230.   var response = cortex.dataSets[
  231.     cortex.feed(input)
  232.   ];
  233.   console.log(
  234.     input + " > " +
  235.     response['raw'] + ": " +
  236.     response['tag']
  237.   );
  238.   return {
  239.     "input" : input,
  240.     "output" : response['raw'],
  241.     "tag" : response['tag']
  242.   };
  243. }
  244.  
  245. test("a1a1");
  246. test("a2b2");
  247. test("a2b0");
  248. test("a2b3");
  249. test("a3b2");
  250.  
  251. if(cortex.splash == true) console.log(" ");
  252.  
  253. var app = express()
  254.  
  255. app.use(function(req, res, next){
  256.   res.header("Access-Control-Allow-Origin", "*");
  257.   res.header("Content-Type", "application/json");
  258.   next();
  259. });
  260.  
  261. app.get('/:str', function(req, res){
  262.   var $d = test(req.params.str);
  263.   var $j = JSON.stringify($d);
  264.   res.send($j);
  265. });
  266.  
  267. app.listen(9960, function(){
  268.   console.log('> now listening on port 9960.');
  269.   console.log(" ");
  270. });
RAW Paste Data Copied