UNPKG

compromise

Version:

modest natural language processing

52 lines (48 loc) • 1.65 kB
import { blue, grey, dim, i, b, ul } from '../_color.js' import debug from './debug.js' const colorSpec = doc => { return doc.docs .map((terms, index) => { const plain = terms.map(t => t.pre + t.text + t.post).join('').trim() const text = dim("'" + plain + "'") const spec = doc.update([[index]]).out('spec') return text + grey(i(spec.slice(plain.length))) }) .join('\n') } // Trace the actual pipeline once, including the initial tokenized document. const hooksDebug = headings => (doc, hooks) => { const width = Math.max('tokenize'.length, ...hooks.map(hook => hook.length)) const snapshot = (hook, nested = false) => { const output = colorSpec(doc) output.split('\n').forEach((line, index, lines) => { if (nested) { const branch = index === lines.length - 1 ? ' ╰─' : '│ ' console.log(` ${dim(branch)} ${line}`) // eslint-disable-line no-console return } const label = (index === 0 ? hook : '').padEnd(width) console.log(` ${dim(label)} ${line}`) // eslint-disable-line no-console }) } snapshot('tokenize') hooks.forEach(hook => { const nested = headings const prefix = debug.prefix const rule = debug.rule debug.rule = undefined if (nested) { console.log(` ${blue(b(ul(hook + ':')))}`) // eslint-disable-line no-console debug.prefix = dim(' │ ') } // Restore the log prefix even when a plugin hook throws. try { doc.compute(hook) snapshot(hook, nested) } finally { debug.prefix = prefix debug.rule = rule } }) } export default hooksDebug