Skip to content

Instantly share code, notes, and snippets.

@jem-computer
Created November 12, 2015 01:58
Show Gist options
  • Save jem-computer/701ff35e8189d4cabc02 to your computer and use it in GitHub Desktop.
Save jem-computer/701ff35e8189d4cabc02 to your computer and use it in GitHub Desktop.
import trainingSet from './fw.json'
import { __, add, countBy, curry, difference, head, identity, intersection, keys, lensProp, map, mapObjIndexed, mergeAll, multiply, over, pick, pipe, prepend, prop, reduce, split, view, zipWith } from 'ramda'
import { indexBy, log, mergeWith, without } from '../utils'
// Lenses
const titleL = lensProp('title')
const pairingsL = lensProp('pairings')
// String -> String
// ~= input "Helvetica (20)"
// output "Helvetica"
const cleanTitle = pipe(split(/\s\(/), head)
const removeTitleFromPairings = (font) => over(pairingsL, without(view(titleL, font)), font)
const processFonts = pipe(
map(over(titleL, cleanTitle)),
map(removeTitleFromPairings),
map(over(pairingsL, countBy(identity))),
indexBy('title')
)
const fonts = processFonts(trainingSet)
const getFont = prop(__, fonts)
const getSimilar = prop('best_similar')
const getPairings = prop('pairings')
// (a -> Number) -> { k: a } -> { k: a }
const reweight = curry((multiplier, object) => {
return mapObjIndexed(multiply(0.5), object)
})
const thisReducer = (acc, val) => {
const accKeys = keys(acc)
const valKeys = keys(val)
const commonKeys = intersection(valKeys, accKeys)
const newKeys = difference(valKeys, accKeys)
const newObj = pick(newKeys, val)
const newCommon = mergeWith(add, pick(commonKeys, acc), pick(commonKeys, val))
return mergeAll([acc, newObj, newCommon])
}
const pairingsForFont = name => {
const font = getFont(name)
return pipe(
getSimilar,
map(getFont),
map(getPairings),
map(reweight(0.5)),
prepend(getPairings(font)),
reduce(thisReducer, {})
)(font)
}
log(pairingsForFont('Merriweather'))
// import { Graph } from 'algorithms/data_structures'
// //
// const G = new Graph()
// G.addVertex('Akzidenz Grotesk')
// G.addVertex('Helvetica Neue')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment