9
9
import * as msRest from '@azure/ms-rest-js' ;
10
10
import * as os from 'os' ;
11
11
import { LuisApplication , LuisRecognizerOptionsV2 } from './luisRecognizer' ;
12
+ import { CompositeChildModel , CompositeEntityModel , EntityModel , LuisResult } from './luisV2-models/luisResult' ;
13
+ import { LUISRuntimeClientV2 as LuisClient } from './luisV2-models/luisRuntimeClientV2' ;
12
14
import { LuisRecognizerInternal } from './luisRecognizerOptions' ;
13
15
import { NullTelemetryClient , TurnContext , RecognizerResult } from 'botbuilder-core' ;
14
-
15
- import {
16
- LUISRuntimeClient as LuisClient ,
17
- LUISRuntimeModels as LuisModels ,
18
- } from '@azure/cognitiveservices-luis-runtime' ;
19
16
import { DialogContext } from 'botbuilder-dialogs' ;
20
17
21
18
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -116,7 +113,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
116
113
117
114
const luisPredictionOptions = this . options ;
118
115
119
- const luisResult : LuisModels . LuisResult = await this . luisClient . prediction . resolve (
116
+ const luisResult : LuisResult = await this . luisClient . prediction . resolve (
120
117
this . application . applicationId ,
121
118
utterance ,
122
119
{
@@ -155,7 +152,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
155
152
}
156
153
157
154
// Get Intents from a LuisResult object.
158
- private getIntents ( luisResult : LuisModels . LuisResult ) : Record < string , Record < 'score' , number > > {
155
+ private getIntents ( luisResult : LuisResult ) : Record < string , Record < 'score' , number > > {
159
156
const intents : { [ name : string ] : { score : number } } = { } ;
160
157
if ( luisResult . intents ) {
161
158
luisResult . intents . reduce ( ( prev , curr ) => {
@@ -171,8 +168,8 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
171
168
}
172
169
173
170
private getEntitiesAndMetadata (
174
- entities : LuisModels . EntityModel [ ] ,
175
- compositeEntities : LuisModels . CompositeEntityModel [ ] | undefined ,
171
+ entities : EntityModel [ ] ,
172
+ compositeEntities : CompositeEntityModel [ ] | undefined ,
176
173
verbose : boolean
177
174
// eslint-disable-next-line @typescript-eslint/no-explicit-any
178
175
) : any {
@@ -183,14 +180,14 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
183
180
// We start by populating composite entities so that entities covered by them are removed from the entities list
184
181
if ( compositeEntities ) {
185
182
compositeEntityTypes = compositeEntities . map (
186
- ( compositeEntity : LuisModels . CompositeEntityModel ) => compositeEntity . parentType
183
+ ( compositeEntity : CompositeEntityModel ) => compositeEntity . parentType
187
184
) ;
188
- compositeEntities . forEach ( ( compositeEntity : LuisModels . CompositeEntityModel ) => {
185
+ compositeEntities . forEach ( ( compositeEntity : CompositeEntityModel ) => {
189
186
entities = this . populateCompositeEntity ( compositeEntity , entities , entitiesAndMetadata , verbose ) ;
190
187
} ) ;
191
188
}
192
189
193
- entities . forEach ( ( entity : LuisModels . EntityModel ) => {
190
+ entities . forEach ( ( entity : EntityModel ) => {
194
191
// we'll address composite entities separately
195
192
if ( compositeEntityTypes . indexOf ( entity . type ) > - 1 ) {
196
193
return ;
@@ -213,35 +210,33 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
213
210
}
214
211
215
212
private populateCompositeEntity (
216
- compositeEntity : LuisModels . CompositeEntityModel ,
217
- entities : LuisModels . EntityModel [ ] ,
213
+ compositeEntity : CompositeEntityModel ,
214
+ entities : EntityModel [ ] ,
218
215
entitiesAndMetadata : any , // eslint-disable-line @typescript-eslint/no-explicit-any
219
216
verbose : boolean
220
- ) : LuisModels . EntityModel [ ] {
217
+ ) : EntityModel [ ] {
221
218
// eslint-disable-next-line @typescript-eslint/no-explicit-any
222
219
const childrenEntities : any = verbose ? { $instance : { } } : { } ;
223
220
// eslint-disable-next-line @typescript-eslint/no-explicit-any
224
221
let childrenEntitiesMetadata : any = { } ;
225
222
226
223
// This is now implemented as O(n^2) search and can be reduced to O(2n) using a map as an optimization if n grows
227
- const compositeEntityMetadata : LuisModels . EntityModel | undefined = entities . find (
228
- ( entity : LuisModels . EntityModel ) => {
229
- // For now we are matching by value, which can be ambiguous if the same composite entity shows up with the same text
230
- // multiple times within an utterance, but this is just a stop gap solution till the indices are included in composite entities
231
- return entity . type === compositeEntity . parentType && entity . entity === compositeEntity . value ;
232
- }
233
- ) ;
224
+ const compositeEntityMetadata : EntityModel | undefined = entities . find ( ( entity : EntityModel ) => {
225
+ // For now we are matching by value, which can be ambiguous if the same composite entity shows up with the same text
226
+ // multiple times within an utterance, but this is just a stop gap solution till the indices are included in composite entities
227
+ return entity . type === compositeEntity . parentType && entity . entity === compositeEntity . value ;
228
+ } ) ;
234
229
235
- const filteredEntities : LuisModels . EntityModel [ ] = [ ] ;
230
+ const filteredEntities : EntityModel [ ] = [ ] ;
236
231
if ( verbose ) {
237
232
childrenEntitiesMetadata = this . getEntityMetadata ( compositeEntityMetadata ) ;
238
233
}
239
234
240
235
// This is now implemented as O(n*k) search and can be reduced to O(n + k) using a map as an optimization if n or k grow
241
236
const coveredSet = new Set ( ) ;
242
- compositeEntity . children . forEach ( ( childEntity : LuisModels . CompositeChildModel ) => {
237
+ compositeEntity . children . forEach ( ( childEntity : CompositeChildModel ) => {
243
238
for ( let i = 0 ; i < entities . length ; i ++ ) {
244
- const entity : LuisModels . EntityModel = entities [ i ] ;
239
+ const entity : EntityModel = entities [ i ] ;
245
240
if (
246
241
! coveredSet . has ( i ) &&
247
242
childEntity . type === entity . type &&
@@ -291,7 +286,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
291
286
}
292
287
293
288
// eslint-disable-next-line @typescript-eslint/no-explicit-any
294
- private getEntityValue ( entity : LuisModels . EntityModel ) : any {
289
+ private getEntityValue ( entity : EntityModel ) : any {
295
290
if ( entity . type . startsWith ( 'builtin.geographyV2.' ) ) {
296
291
return {
297
292
type : entity . type . substring ( 20 ) ,
@@ -359,7 +354,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
359
354
}
360
355
}
361
356
362
- private getEntityMetadata ( entity : LuisModels . EntityModel ) : Record < string , string | number > {
357
+ private getEntityMetadata ( entity : EntityModel ) : Record < string , string | number > {
363
358
const res : Record < string , string | number > = {
364
359
startIndex : entity . startIndex ,
365
360
endIndex : entity . endIndex + 1 ,
@@ -374,7 +369,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
374
369
return res ;
375
370
}
376
371
377
- private getNormalizedEntityName ( entity : LuisModels . EntityModel ) : string {
372
+ private getNormalizedEntityName ( entity : EntityModel ) : string {
378
373
// Type::Role -> Role
379
374
let type = entity . type . split ( ':' ) . pop ( ) ;
380
375
if ( type . startsWith ( 'builtin.datetimeV2.' ) ) {
@@ -405,7 +400,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
405
400
}
406
401
}
407
402
408
- private getSentiment ( luis : LuisModels . LuisResult ) : Record < 'label' | 'score' , unknown > | undefined {
403
+ private getSentiment ( luis : LuisResult ) : Record < 'label' | 'score' , unknown > | undefined {
409
404
if ( luis . sentimentAnalysis ) {
410
405
return {
411
406
label : luis . sentimentAnalysis . label ,
@@ -427,7 +422,7 @@ export class LuisRecognizerV2 extends LuisRecognizerInternal {
427
422
428
423
private emitTraceInfo (
429
424
context : TurnContext ,
430
- luisResult : LuisModels . LuisResult ,
425
+ luisResult : LuisResult ,
431
426
recognizerResult : RecognizerResult
432
427
) : Promise < unknown > {
433
428
const traceInfo = {
0 commit comments