Skip to content

Commit

Permalink
slightly smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Jan 24, 2023
1 parent cbfba3a commit d7afe8d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
4 changes: 1 addition & 3 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { learn, reverse, test, compress, uncompress, classify, fingerprint, tiny
import data from '/Users/spencer/mountain/it-compromise/data/models/verbs/conditional.js'

// let pairs = Object.keys(data).map(k => [k, data[k][0]])
let pairs = Object.keys(data).map(k => [data[k][0], k]).slice(0, 200)
let pairs = Object.keys(data).map(k => [data[k][0], k])//.slice(0, 200)
// pairs = [
// ['sottomettere', 'sottometterei'],
// ['teletrasmettere', 'teletrasetterei'],
Expand All @@ -12,13 +12,11 @@ let pairs = Object.keys(data).map(k => [data[k][0], k]).slice(0, 200)

// console.log(compress(learn(pairs)))
let res = tiny(pairs)
// res = compress(res)
console.log(res)
console.log('\n\n')
let small = pack(res)
console.log(small)
let again = unpack(small)
console.log(again)
test(pairs, again)

// { ei: 'e' },
Expand Down
25 changes: 25 additions & 0 deletions src/pack/key-val.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// longest common prefix
const findOverlap = (from, to) => {
let all = []
for (let i = 0; i < from.length; i += 1) {
if (from[i] === to[i]) {
all.push(from[i])
} else {
break
}
}
return all.join('')
}

// run-length encode any shared prefix
let compress = function (key, val) {
let prefix = findOverlap(key, val)
if (prefix.length < 1) {
return val
}
let out = prefix.length + val.substr(prefix.length)
return out
}

export default compress
// console.log(compress('fixture', 'fixturing'))
18 changes: 14 additions & 4 deletions src/pack/pack.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import keyVal from './key-val.js'

const pack = function (model) {
let out = ''
model.rules.forEach(obj => {
let r = []
Object.keys(obj).forEach(k => {
r.push(k + '|' + obj[k] + ',')
let val = keyVal(k, obj[k])// compress any shared prefix
if (!val) {
console.log('=-=-=-= here -=-=-=-')
console.log(k, obj[k])
}
r.push(k + ':' + val + ',')
})
out += r.join('')
})
out += '=='
let r = []
Object.keys(model.exceptions).forEach(k => {
r.push(k + '|' + model.exceptions[k])
Object.keys(model.exceptions || {}).forEach(k => {
let val = keyVal(k, model.exceptions[k])// compress any shared prefix
r.push(k + ':' + val)
})
out += r.join(',')
return out
}
export default pack
export default pack

// rei:ere, erei:are, rrei:nere, arei:3, herei:are, lirei:4, nirei:4, tirei:4,==
22 changes: 18 additions & 4 deletions src/pack/unpack.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
const prefix = /^([0-9]+)/

const expand = function (key = '', val = '') {
val = String(val)
let m = val.match(prefix)
if (m === null) {
return val
}
let num = Number(m[1]) || 0
let pre = key.substring(0, num)
let full = pre + val.replace(prefix, '')
return full
}

const unpack = function (str) {
let out = { rules: [], exceptions: {} }
let [rules, exceptions] = str.split('==')
// unpack rules
rules.split(',').forEach(txt => {
let [a, b] = txt.split('|')
let [a, b] = txt.split(':')
let len = a.length
if (len) {
out.rules[len] = out.rules[len] || {}
out.rules[len][a] = b
out.rules[len][a] = expand(a, b)
}
})
// clean empties up a bit
Expand All @@ -17,8 +31,8 @@ const unpack = function (str) {

// unpack exceptions
exceptions.split(',').forEach(txt => {
let [a, b] = txt.split('|')
out.exceptions[a] = b
let [a, b] = txt.split(':')
out.exceptions[a] = expand(a, b)
})
return out
}
Expand Down
2 changes: 1 addition & 1 deletion tmp2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d7afe8d

Please sign in to comment.