/
crf_extractor.ts
277 lines (237 loc) · 8.77 KB
/
crf_extractor.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as sdk from 'botpress/sdk'
import fs, { readFileSync } from 'fs'
import _ from 'lodash'
import kmeans from 'ml-kmeans'
import tmp from 'tmp'
import { BIO, Sequence, SlotExtractor, Token } from '../../typings'
import { generatePredictionSequence } from './pre-processor'
// TODO grid search / optimization for those hyperparams
const K_CLUSTERS = 15
const KMEANS_OPTIONS = {
iterations: 250,
initialization: 'random',
seed: 666 // so training is consistent
}
const CRF_TRAINER_PARAMS = {
c1: '0.0001',
c2: '0.01',
max_iterations: '500',
'feature.possible_transitions': '1',
'feature.possible_states': '1'
}
export default class CRFExtractor implements SlotExtractor {
private _isTrained: boolean = false
private _ftModelFn = ''
private _crfModelFn = ''
private _ft!: sdk.MLToolkit.FastText.Model
private _tagger!: sdk.MLToolkit.CRF.Tagger
private _kmeansModel
constructor(private toolkit: typeof sdk.MLToolkit) {}
async load(traingingSet: Sequence[], languageModelBuf: Buffer, crf: Buffer) {
// load language model
const ftModelFn = tmp.tmpNameSync({ postfix: '.bin' })
fs.writeFileSync(ftModelFn, languageModelBuf)
const ft = new this.toolkit.FastText.Model()
await ft.loadFromFile(ftModelFn)
this._ft = ft
this._ftModelFn = ftModelFn
// load kmeans (retrain because there is no simple way to store it)
await this._trainKmeans(traingingSet)
// load crf model
this._crfModelFn = tmp.tmpNameSync()
fs.writeFileSync(this._crfModelFn, crf)
this._tagger = this.toolkit.CRF.createTagger()
await this._tagger.open(this._crfModelFn)
this._isTrained = true
}
async train(trainingSet: Sequence[]): Promise<{ language: Buffer; crf: Buffer }> {
this._isTrained = false
if (trainingSet.length >= 2) {
await this._trainLanguageModel(trainingSet)
await this._trainKmeans(trainingSet)
await this._trainCrf(trainingSet)
this._tagger = this.toolkit.CRF.createTagger()
await this._tagger.open(this._crfModelFn)
this._isTrained = true
return {
language: readFileSync(this._ftModelFn),
crf: readFileSync(this._crfModelFn)
}
} else {
return {
language: undefined,
crf: undefined
}
}
}
/**
* Returns an object with extracted slots name as keys.
* Each slots under each keys can either be a single Slot object or Array<Slot>
* return value example:
* slots: {
* artist: {
* name: "artist",
* value: "Kanye West",
* entity: [Object] // corresponding sdk.NLU.Entity
* },
* songs : [ multiple slots objects here]
* }
*/
async extract(
text: string,
intentDef: sdk.NLU.IntentDefinition,
entitites: sdk.NLU.Entity[]
): Promise<sdk.NLU.SlotsCollection> {
const seq = generatePredictionSequence(text, intentDef.name, entitites)
const tags = await this._tag(seq)
// notice usage of zip here, we want to loop on tokens and tags at the same index
return (_.zip(seq.tokens, tags) as [Token, string][])
.filter(([token, tag]) => {
if (!token || !tag || tag === BIO.OUT) {
return false
}
const slotName = tag.slice(2)
return intentDef.slots.find(slotDef => slotDef.name === slotName) !== undefined
})
.reduce((slotCollection: any, [token, tag]) => {
const slotName = tag.slice(2)
const slot = this._makeSlot(slotName, token, intentDef.slots, entitites)
if (tag[0] === BIO.INSIDE && slotCollection[slotName]) {
// simply append the source if the tag is inside a slot
slotCollection[slotName].source += ` ${token.value}`
} else if (tag[0] === BIO.BEGINNING && slotCollection[slotName]) {
// if the tag is beginning and the slot already exists, we create need a array slot
if (Array.isArray(slotCollection[slotName])) {
slotName[slotName].push(slot)
} else {
// if no slots exist we assign a slot to the slot key
slotCollection[slotName] = [slotCollection[slotName], slot]
}
} else {
slotCollection[slotName] = slot
}
return slotCollection
}, {})
}
// this is made "protected" to facilitate model validation
async _tag(seq: Sequence): Promise<string[]> {
if (!this._isTrained) {
throw new Error('Model not trained, please call train() before')
}
const inputVectors: string[][] = []
for (let i = 0; i < seq.tokens.length; i++) {
const featureVec = await this._vectorize(seq.tokens, seq.intent, i)
inputVectors.push(featureVec)
}
return this._tagger.tag(inputVectors).result
}
private _makeSlot(
slotName: string,
token: Token,
slotDefinitions: sdk.NLU.SlotDefinition[],
entitites: sdk.NLU.Entity[]
): sdk.NLU.Slot {
const slotDef = slotDefinitions.find(slotDef => slotDef.name === slotName)
const entity =
slotDef &&
entitites.find(e => slotDef.entity === e.name && e.meta.start <= token.start && e.meta.end >= token.end)
const value = _.get(entity, 'data.value', token.value)
const slot = {
name: slotName,
value
} as sdk.NLU.Slot
if (entity) {
slot.entity = entity
}
return slot
}
private async _trainKmeans(sequences: Sequence[]): Promise<any> {
const tokens = _.flatMap(sequences, s => s.tokens)
const data = await Promise.mapSeries(tokens, t => this._ft.queryWordVectors(t.value))
const k = data.length > K_CLUSTERS ? K_CLUSTERS : 2
try {
this._kmeansModel = kmeans(data, k, KMEANS_OPTIONS)
} catch (error) {
throw Error('Error training K-means model')
}
}
private async _trainCrf(sequences: Sequence[]) {
this._crfModelFn = tmp.fileSync({ postfix: '.bin' }).name
const trainer = this.toolkit.CRF.createTrainer()
trainer.set_params(CRF_TRAINER_PARAMS)
trainer.set_callback(str => {
/* swallow training results */
})
for (const seq of sequences) {
const inputVectors: string[][] = []
const labels: string[] = []
for (let i = 0; i < seq.tokens.length; i++) {
const featureVec = await this._vectorize(seq.tokens, seq.intent, i)
inputVectors.push(featureVec)
const labelSlot = seq.tokens[i].slot ? `-${seq.tokens[i].slot}` : ''
labels.push(`${seq.tokens[i].tag}${labelSlot}`)
}
trainer.append(inputVectors, labels)
}
trainer.train(this._crfModelFn)
}
private async _trainLanguageModel(samples: Sequence[]) {
this._ftModelFn = tmp.fileSync({ postfix: '.bin' }).name
const ftTrainFn = tmp.fileSync({ postfix: '.txt' }).name
const ft = new this.toolkit.FastText.Model()
const trainContent = samples.reduce((corpus, seq) => {
const cannonicSentence = seq.tokens
.map(s => {
if (s.tag === BIO.OUT) return s.value
else return s.slot
})
.join(' ')
return `${corpus}${cannonicSentence}\n`
}, '')
fs.writeFileSync(ftTrainFn, trainContent, 'utf8')
await ft.trainToFile('skipgram', this._ftModelFn, {
input: ftTrainFn,
minCount: 2,
dim: 15,
lr: 0.5,
epoch: 50
})
this._ft = ft
}
private async _vectorizeToken(
token: Token,
intentName: string,
featPrefix: string,
includeCluster: boolean
): Promise<string[]> {
const vector: string[] = [`${featPrefix}intent=${intentName}`]
if (token.value === token.value.toLowerCase()) vector.push(`${featPrefix}low`)
if (token.value === token.value.toUpperCase()) vector.push(`${featPrefix}up`)
if (
token.value.length > 1 &&
token.value[0] === token.value[0].toUpperCase() &&
token.value[1] === token.value[1].toLowerCase()
)
vector.push(`${featPrefix}title`)
if (includeCluster) {
const cluster = await this._getWordCluster(token.value)
vector.push(`${featPrefix}cluster=${cluster.toString()}`)
}
const entititesFeatures = (token.matchedEntities.length ? token.matchedEntities : ['none']).map(
ent => `${featPrefix}entity=${ent === 'any' ? 'none' : ent}`
)
return [...vector, ...entititesFeatures]
}
// TODO maybe use a slice instead of the whole token seq ?
private async _vectorize(tokens: Token[], intentName: string, idx: number): Promise<string[]> {
const prev = idx === 0 ? ['w[0]bos'] : await this._vectorizeToken(tokens[idx - 1], intentName, 'w[-1]', true)
const current = await this._vectorizeToken(tokens[idx], intentName, 'w[0]', false)
const next =
idx === tokens.length - 1 ? ['w[0]eos'] : await this._vectorizeToken(tokens[idx + 1], intentName, 'w[1]', true)
return [...prev, ...current, ...next]
}
private async _getWordCluster(word: string): Promise<number> {
const vector = await this._ft.queryWordVectors(word)
return this._kmeansModel.nearest([vector])[0]
}
}