-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathDebugOutputBuilder.js
197 lines (170 loc) · 5.27 KB
/
DebugOutputBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/** This class generates ansi color-coded debugging output from an
* address parse. The string that it builds up is suitable for outputting
* to a console, or, through the use of a javascript library like ansi-up,
* displaying on the web.
*
* The class is generally called like so:
* new DebugOutputBuilder().parse(input).toString()
*/
const util = require('util')
const chalk = require('chalk')
const StringBuffer = require('stringbuffer')
const Tokenizer = require('../tokenization/Tokenizer')
const AddressParser = require('../parser/AddressParser')
class DebugOutputBuilder {
constructor () {
this.sb = new StringBuffer()
const self = this
function write (o) {
if (typeof o === 'object') {
self.sb.append(util.inspect(o, { colors: true }))
} else {
self.sb.append(o)
}
}
this.write = write
}
toString () {
return this.sb.toString()
}
writeLine (...s) {
if (s) {
s.forEach(this.write)
}
this.write('\n')
}
tokenizer (tokenizer, label) {
const spans = (title, s) => {
this.write(title.padEnd(32) + '➜ ')
if (s.length === 0) {
this.writeLine()
}
for (let i = 0; i < s.length; i++) {
this.write(
chalk.bgBlue.bold(util.format(' %s ', s[i].body)) +
chalk.bgWhite.bold.gray(util.format(' %d:%d ', s[i].start, s[i].end))
)
if (i === s.length - 1) {
this.writeLine()
} else {
this.write(' ')
}
}
}
this.writeLine()
this.writeLine('='.repeat(64))
this.writeLine(`TOKENIZATION ${label}`)
this.writeLine('-'.repeat(64))
this.write('INPUT'.padEnd(32) + '➜ ')
this.writeLine(tokenizer.span.body)
spans('SECTIONS', tokenizer.section)
for (let i = 0; i < tokenizer.section.length; i++) {
spans(util.format('S%d TOKENS', i), tokenizer.section[i].graph.findAll('child'))
}
for (let i = 0; i < tokenizer.section.length; i++) {
spans(util.format('S%d PHRASES', i), tokenizer.section[i].graph.findAll('phrase'))
}
this.writeLine()
}
wordClassifications (tokenizer) {
this.writeLine('-'.repeat(64))
this.writeLine('WORDS')
this.writeLine('-'.repeat(64))
for (let i = 0; i < tokenizer.section.length; i++) {
let section = tokenizer.section[i]
let children = section.graph.findAll('child')
for (let j = 0; j < children.length; j++) {
let word = children[j]
let keys = Object.keys(word.classifications)
if (!keys.length) {
continue
}
this.write(word.body.padEnd(32) + '➜ ')
for (let k in word.classifications) {
let classification = word.classifications[k]
let block = chalk.bgGreen.bold(util.format(' %s ', classification.label))
block += chalk.bgWhite.bold.gray(
util.format(' %s ', classification.confidence.toFixed(2))
)
this.write(block)
if (k !== keys.slice(-1)) {
this.write(' ')
}
}
this.writeLine()
}
}
this.writeLine()
}
phraseClassifications (tokenizer) {
this.writeLine('-'.repeat(64))
this.writeLine('PHRASES')
this.writeLine('-'.repeat(64))
for (let i = 0; i < tokenizer.section.length; i++) {
let section = tokenizer.section[i]
let phrases = section.graph.findAll('phrase')
for (let j = 0; j < phrases.length; j++) {
let phrase = phrases[j]
let keys = Object.keys(phrase.classifications)
if (!keys.length) {
continue
}
this.write(phrase.body.padEnd(32) + '➜ ')
for (let k in phrase.classifications) {
let classification = phrase.classifications[k]
let block = chalk.bgRed.bold(util.format(' %s ', classification.label))
block += chalk.bgWhite.bold.gray(
util.format(' %s ', classification.confidence.toFixed(2))
)
this.write(block)
if (k !== keys.slice(-1)) {
this.write(' ')
}
}
this.writeLine()
}
}
this.writeLine()
}
classifications (tokenizer, label) {
this.writeLine('='.repeat(64))
this.writeLine(`CLASSIFICATIONS ${label}`)
this.wordClassifications(tokenizer, label)
this.phraseClassifications(tokenizer, label)
}
solutions (tokenizer, label) {
this.writeLine('='.repeat(64))
this.writeLine(`SOLUTIONS ${label}`)
this.writeLine('-'.repeat(64))
// print all solutions
tokenizer.solution.forEach((s) => {
let score = chalk.yellow.bold('(' + s.score.toFixed(2) + ')')
this.writeLine(
score,
' ➜ ',
s.pair.map((c) => {
return {
[c.classification.label]: c.span.body
// offset: c.span.start
}
})
)
this.writeLine()
})
}
parse (input) {
// tokenizer
var start = new Date()
const t = new Tokenizer(input)
let took = new Date() - start
this.tokenizer(t, util.format('(%sms)', took))
// parser
const parser = new AddressParser()
took = parser.classify(t)
this.classifications(t, util.format('(%sms)', took))
took = parser.solve(t)
this.solutions(t, util.format('(%sms)', took))
return this
}
}
module.exports = DebugOutputBuilder