3
3
4
4
import platform
5
5
from collections import OrderedDict
6
- from typing import Dict , List , Set , Union
6
+ from typing import Dict , List , Union
7
7
8
8
import azure .cognitiveservices .language .luis .runtime .models as runtime_models
9
9
from azure .cognitiveservices .language .luis .runtime .models import (
10
10
CompositeEntityModel ,
11
11
EntityModel ,
12
12
LuisResult ,
13
13
)
14
- from botbuilder .core import IntentScore , RecognizerResult
15
14
from msrest import Serializer
15
+ from botbuilder .core import IntentScore , RecognizerResult
16
16
17
17
from .. import __title__ , __version__
18
18
@@ -35,12 +35,11 @@ def get_intents(luis_result: LuisResult) -> Dict[str, IntentScore]:
35
35
LuisUtil .normalized_intent (i .intent ): IntentScore (i .score or 0 )
36
36
for i in luis_result .intents
37
37
}
38
- else :
39
- return {
40
- LuisUtil .normalized_intent (
41
- luis_result .top_scoring_intent .intent
42
- ): IntentScore (luis_result .top_scoring_intent .score or 0 )
43
- }
38
+ return {
39
+ LuisUtil .normalized_intent (
40
+ luis_result .top_scoring_intent .intent
41
+ ): IntentScore (luis_result .top_scoring_intent .score or 0 )
42
+ }
44
43
45
44
@staticmethod
46
45
def extract_entities_and_metadata (
@@ -58,9 +57,9 @@ def extract_entities_and_metadata(
58
57
if composite_entities :
59
58
composite_entity_types = set (ce .parent_type for ce in composite_entities )
60
59
current = entities
61
- for compositeEntity in composite_entities :
60
+ for composite_entity in composite_entities :
62
61
current = LuisUtil .populate_composite_entity_model (
63
- compositeEntity , current , entities_and_metadata , verbose
62
+ composite_entity , current , entities_and_metadata , verbose
64
63
)
65
64
entities = current
66
65
@@ -90,12 +89,12 @@ def number(value: object) -> Union[int, float]:
90
89
return None
91
90
92
91
try :
93
- s = str (value )
94
- i = int (s )
95
- return i
92
+ str_value = str (value )
93
+ int_value = int (str_value )
94
+ return int_value
96
95
except ValueError :
97
- f = float (s )
98
- return f
96
+ float_value = float (str_value )
97
+ return float_value
99
98
100
99
@staticmethod
101
100
def extract_entity_value (entity : EntityModel ) -> object :
@@ -108,7 +107,7 @@ def extract_entity_value(entity: EntityModel) -> object:
108
107
resolution = entity .additional_properties ["resolution" ]
109
108
if entity .type .startswith ("builtin.datetime." ):
110
109
return resolution
111
- elif entity .type .startswith ("builtin.datetimeV2." ):
110
+ if entity .type .startswith ("builtin.datetimeV2." ):
112
111
if not resolution ["values" ]:
113
112
return resolution
114
113
@@ -117,32 +116,31 @@ def extract_entity_value(entity: EntityModel) -> object:
117
116
timexes = [val ["timex" ] for val in resolution_values ]
118
117
distinct_timexes = list (OrderedDict .fromkeys (timexes ))
119
118
return {"type" : val_type , "timex" : distinct_timexes }
120
- else :
121
- if entity .type in {"builtin.number" , "builtin.ordinal" }:
122
- return LuisUtil .number (resolution ["value" ])
123
- elif entity .type == "builtin.percentage" :
124
- svalue = str (resolution ["value" ])
125
- if svalue .endswith ("%" ):
126
- svalue = svalue [:- 1 ]
127
-
128
- return LuisUtil .number (svalue )
129
- elif entity .type in {
130
- "builtin.age" ,
131
- "builtin.dimension" ,
132
- "builtin.currency" ,
133
- "builtin.temperature" ,
134
- }:
135
- units = resolution ["unit" ]
136
- val = LuisUtil .number (resolution ["value" ])
137
- obj = {}
138
- if val is not None :
139
- obj ["number" ] = val
140
-
141
- obj ["units" ] = units
142
- return obj
143
- else :
144
- value = resolution .get ("value" )
145
- return value if value is not None else resolution .get ("values" )
119
+
120
+ if entity .type in {"builtin.number" , "builtin.ordinal" }:
121
+ return LuisUtil .number (resolution ["value" ])
122
+ if entity .type == "builtin.percentage" :
123
+ svalue = str (resolution ["value" ])
124
+ if svalue .endswith ("%" ):
125
+ svalue = svalue [:- 1 ]
126
+
127
+ return LuisUtil .number (svalue )
128
+ if entity .type in {
129
+ "builtin.age" ,
130
+ "builtin.dimension" ,
131
+ "builtin.currency" ,
132
+ "builtin.temperature" ,
133
+ }:
134
+ units = resolution ["unit" ]
135
+ val = LuisUtil .number (resolution ["value" ])
136
+ obj = {}
137
+ if val is not None :
138
+ obj ["number" ] = val
139
+
140
+ obj ["units" ] = units
141
+ return obj
142
+ value = resolution .get ("value" )
143
+ return value if value is not None else resolution .get ("values" )
146
144
147
145
@staticmethod
148
146
def extract_entity_metadata (entity : EntityModel ) -> Dict :
@@ -202,10 +200,10 @@ def populate_composite_entity_model(
202
200
# 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
203
201
composite_entity_metadata = next (
204
202
(
205
- e
206
- for e in entities
207
- if e .type == composite_entity .parent_type
208
- and e .entity == composite_entity .value
203
+ ent
204
+ for ent in entities
205
+ if ent .type == composite_entity .parent_type
206
+ and ent .entity == composite_entity .value
209
207
),
10AB2
td>210
208
None ,
211
209
)
@@ -310,7 +308,7 @@ def recognizer_result_as_dict(
310
308
for name , intent_score in recognizer_result .intents .items ()
311
309
} if recognizer_result .intents is not None else None
312
310
313
- d : Dict [str , object ] = {
311
+ dictionary : Dict [str , object ] = {
314
312
"text" : recognizer_result .text ,
315
313
"alteredText" : recognizer_result .altered_text ,
316
314
"intents" : intents ,
@@ -319,13 +317,13 @@ def recognizer_result_as_dict(
319
317
320
318
if recognizer_result .properties is not None :
321
319
for key , value in recognizer_result .properties .items ():
322
- if key not in d :
320
+ if key not in dictionary :
323
321
if isinstance (value , LuisResult ):
324
- d [key ] = LuisUtil .luis_result_as_dict (value )
322
+ dictionary [key ] = LuisUtil .luis_result_as_dict (value )
325
323
else :
326
- d [key ] = value
324
+ dictionary [key ] = value
327
325
328
- return d
326
+ return dictionary
329
327
330
328
@staticmethod
331
329
def intent_score_as_dict (intent_score : IntentScore ) -> Dict [str , float ]:
@@ -343,5 +341,5 @@ def luis_result_as_dict(luis_result: LuisResult) -> Dict[str, object]:
343
341
k : v for k , v in runtime_models .__dict__ .items () if isinstance (v , type )
344
342
}
345
343
serializer = Serializer (client_models )
346
- d = serializer .body (luis_result , "LuisResult" )
347
- return d
344
+ result = serializer .body (luis_result , "LuisResult" )
345
+ return result
0 commit comments