Skip to content

Instantly share code, notes, and snippets.

@rikyperdana
Last active June 7, 2019 15:46
Show Gist options
  • Select an option

  • Save rikyperdana/0695f28d980b20a0d67eb628ce6fb938 to your computer and use it in GitHub Desktop.

Select an option

Save rikyperdana/0695f28d980b20a0d67eb628ce6fb938 to your computer and use it in GitHub Desktop.
Re-engineered version of adadgio:neural-data-normalizer
# Neural Data Normalizer by utilizing recursion
# Works well for Synaptic JS
# But require list of strings to be defined beforehand
# Written in LiveScript and with lodash methods
obj = name: \workout, duration: 120, tags: <[gym weights]>
opts =
<[sleep lunch workout]>
<[romance bed wine salad weights gym]>
normalize = (opts = [[]], obj) ->
binarize = (word) ->
selections = opts.find -> it.find -> it is word
[til selections.length]map (i) ->
if selections[i] is word then 1 else 0
combine = (arr) -> [til arr.0.length]map (i, j) ->
cond = arr.find -> it.find (k, l) ->
_.every [(k is 1), (l is j)]
if cond then 1 else 0
decimalize = -> it / Math.pow 10, 10
recurse = (val) ->
if _.isString val then binarize val
else if _.isNumber val then decimalize val
else if _.isArray val then combine val.map -> recurse it
else if _.isObject val then _.map val, -> recurse it
recurse obj
console.log _.flatten normalize opts, obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment