<commit_before>fs = require 'fs'
converter = require("color-convert")()
{print} = require './helper.coffee'
brain = require 'brain'
_ = require 'lodash'

data = fs.readFileSync '../data/training.json'
data = JSON.parse data

parse = (colors) ->

    colors.map (color) ->
        # convert rgb to hsl
        hsl = converter.rgb(color).hsl()

        # normalize hsl to [0, 1]
        hsl.map (elem, index) ->
            if index is 0
                elem /= 360
            else
                elem /= 100
            parseFloat elem.toFixed(3)

likes = data.filter (elem) -> elem.score > 0
likes = likes.map (elem) -> parse(elem.colors)

dislikes = data.filter (elem) -> elem.score < 0
dislikes = dislikes.map (elem) -> parse(elem.colors)

console.log 'Likes', likes.length
console.log 'Dislikes', dislikes.length

# train
net = new brain.NeuralNetwork()

net.train likes.map (elem) -> {input: _.flatten(elem), output: {like: 1}}
net.train dislikes.map (elem) -> {input: _.flatten(elem), output: {dislike: 1}}

json = net.toJSON()
print json
<commit_msg>add train() & verify()<commit_after>fs = require 'fs'
converter = require("color-convert")()
{print} = require './helper.coffee'
brain = require 'brain'
_ = require 'lodash'

data = fs.readFileSync '../data/training.json'
data = JSON.parse data

normalize = (colors) ->

    colors.map (color) ->
        # convert rgb to hsl
        hsl = converter.rgb(color).hsl()

        # normalize hsl to [0, 1]
        hsl.map (elem, index) ->
            if index is 0
                elem /= 360
            else
                elem /= 100
            parseFloat elem.toFixed(3)

likes = data.filter (elem) -> elem.score > 0
likes = likes.map (elem) -> normalize(elem.colors)

dislikes = data.filter (elem) -> elem.score < 0
dislikes = dislikes.map (elem) -> normalize(elem.colors)

# train
net = new brain.NeuralNetwork()

opts =
    errorThresh: 0.005 # error threshold to reach
    iterations: 20000 # max training iterations
    log: true
    logPeriod: 10
    learningRate: 0.3

train = (hslMatrixArray, output) ->
    schemeVerctorArray = hslMatrixArray.map (hslMatrix) -> _.flatten hslMatrix
    data = schemeVerctorArray.map (vector) ->
        input: vector
        output: output
    net.train data, opts

verify = (hslMatrix, expectation) ->
    output = net.toFunction()(_.flatten(hslMatrix))
    console.log "Exp: ", expectation
    console.log "Output: ", output

train likes, {like: 1}
train dislikes, {dislike: 1}

verifyData = fs.readFileSync '../data/training.json'
verifyData = JSON.parse verifyData
getType = (score) ->
    if score > 0
        "like"
    else
        if score < 0
            "dislike"
        else
            "normal"
verifyData.forEach  (elem) ->
    verify normalize(elem.colors), getType(elem.score)

{'fraction_non_alphanumeric': 0.13631484794275492, 'fraction_numerical': 0.015384615384615385, 'mean_word_length': 2.543726235741445, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2526903', 'n_tokens_mistral': 962, 'n_tokens_neox': 900, 'n_words': 247}