forked from LazzaAU/skill_UnitConverter
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitConverter.py
269 lines (213 loc) · 8.02 KB
/
UnitConverter.py
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
import pint
from core.base.model.AliceSkill import AliceSkill
from core.dialog.model.DialogSession import DialogSession
from core.util.Decorators import IntentHandler
class UnitConverter(AliceSkill):
"""
Author: Lazza
Description: Converts between two given units of measurement.
Also deals with gas oven gas mark conversion
"""
def __init__(self):
self._comparing = False
super().__init__()
@IntentHandler('convertUnit')
def convertBetweenUnits(self, session: DialogSession):
self.processRequest(session=session)
@IntentHandler('compareUnit')
def compareBetweenUnits(self, session: DialogSession):
self._comparing = True
self.processRequest(session=session)
def processRequest(self, session):
"""
Process the incoming request and convert it to the desired unit
:param session: The dialog session
:return:
"""
# failsafe just in case this method catches a gasmark request
if 'GasMark' in session.slots and 'TemperatureType' in session.slots:
return
# Check both units are specified for proper conversion
if self.checkForInvalidInput(session):
return
requestedNumber: int = session.slotValue('UnitNumber')
if not requestedNumber:
requestedNumber = 1
firstUnit, secondUnit = self.setFirstAndSecondUnits(session=session)
# Get Raw value to dodge the need to convert to plurals from pints naming scheme
rawFirstUnit: str = session.slotRawValue('FirstUnit')
rawSecondUnit: str = session.slotRawValue('SecondUnit')
# If it's a temperature conversion request then use another method
if self.checkIfTemperatureRequest(session=session, firstUnit=firstUnit, requestedNumber=requestedNumber):
return
try:
convertedValue, dimension1, dimension2 = self.returnCalulationResults(
requestedNumber=requestedNumber,
firstUnit=firstUnit,
secondUnit=secondUnit
)
if convertedValue == 'cancel':
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk(text="dialogMessage1", replace=[dimension1,dimension2 ]),
deviceUid=session.deviceUid)
return
if self._comparing:
answer = f"For every {requestedNumber} {rawSecondUnit} there is {convertedValue} {rawFirstUnit} "
else:
answer = f"{requestedNumber} {rawFirstUnit} equals {convertedValue} {rawSecondUnit}"
self.replyWithResults(session=session, answer=answer)
except:
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk(text='dialogMessage2'),
deviceUid=session.deviceUid
)
@IntentHandler('informGasMark')
def gasMarkIntent(self, session):
"""
Get the gas mark level for ovens
:param session: The dialog session
:return:
"""
if 'Number' not in session.slotsAsObjects:
self.endDialog(session.sessionId, self.randomTalk(text='respondNoIdea'))
return
# Spokeninput is the users requested temperature
spokenInput = session.slotValue('Number')
fahrenheitList = ['degF', 'Fahrenheit', 'f', 'F']
if session.slotValue('TemperatureType') in fahrenheitList:
spokenInput = self.convertToCelsius(spokenInput)
if spokenInput < 135:
self.endDialog(session.sessionId, self.randomTalk(text='respondOutOfRange'))
return
elif 135 <= spokenInput <= 148:
correctGasMark = 1
elif 149 <= spokenInput <= 162:
correctGasMark = 2
elif 163 <= spokenInput <= 176:
correctGasMark = 3
elif 177 <= spokenInput <= 190:
correctGasMark = 4
elif 191 <= spokenInput <= 203:
correctGasMark = 5
elif 204 <= spokenInput <= 217:
correctGasMark = 6
elif 218 <= spokenInput <= 231:
correctGasMark = 7
elif 232 <= spokenInput <= 245:
correctGasMark = 8
elif 246 <= spokenInput <= 269:
correctGasMark = 9
elif 270 <= spokenInput <= 290:
correctGasMark = 10
else:
self.endDialog(session.sessionId, self.randomTalk(text='respondAboveRange'))
return
self.endDialog(session.sessionId, self.randomTalk(text='respondGasMark', replace=[correctGasMark]))
def setFirstAndSecondUnits(self, session):
"""
Create the first and second units depepnding if it's a compare or a convert request
:param session: the dialog Session
:return:
"""
if self._comparing:
firstUnit: str = self.joinMultpileWords(stringg=session.slotValue('SecondUnit'))
secondUnit: str = self.joinMultpileWords(stringg=session.slotValue('FirstUnit'))
else:
firstUnit: str = self.joinMultpileWords(stringg=session.slotValue('FirstUnit'))
secondUnit: str = self.joinMultpileWords(stringg=session.slotValue('SecondUnit'))
return firstUnit, secondUnit
@staticmethod
def convertToFahrenheit(temperature: int) -> int:
return int((9 * temperature) / 5 + 32)
@staticmethod
def convertToCelsius(temperature: int) -> int:
return int((temperature - 32) * 5 / 9)
@staticmethod
def joinMultpileWords(stringg: str):
"""
Place the underscore back into the slot for re use with pint
:param stringg: the slot value
:return: stringg joined with a underscore if required
"""
return stringg.replace(" ", "_")
def checkIfTemperatureRequest(self, session, firstUnit, requestedNumber) -> bool:
"""
Processes a temperature request as the procedure is a little different than a normal conversion
:param session: The dialog session
:param firstUnit: The first requested temperture Unit IE: celsius
:param requestedNumber: The second rquested temperature unit IE: Fahrenhiet
:return: True if user request was a temperature conversion
"""
degList = ['degF', 'degC']
if firstUnit in degList and session.slotValue('SecondUnit') in degList:
ureg = pint.UnitRegistry()
converterInstance = ureg.Quantity
ureg.default_format = '.1f'
if firstUnit == "degF":
temperatureUnit = ureg.degF
outputUnit = "degC"
else:
temperatureUnit = ureg.degC
outputUnit = "degF"
requestedTemperature = converterInstance(requestedNumber, temperatureUnit)
answer = f"That would be {str(requestedTemperature.to(outputUnit)).replace('_', ' ')} "
self.replyWithResults(session=session, answer=answer)
return True
return False
def returnCalulationResults(self, requestedNumber, firstUnit, secondUnit):
"""
Do the conversion calculation and return required values
:param requestedNumber: The number requested from the user
:param firstUnit: The first unit the user spoke
:param secondUnit: The unit the user wants converted into
:return: value, modified unitName, modified RequestedName
"""
ureg = pint.UnitRegistry()
converterInstance = ureg.Quantity
converterInstance(requestedNumber, firstUnit)
userInput = f'{requestedNumber} * {firstUnit} to {secondUnit}'
src, dst = userInput.split(' to ')
# Capture Error before a exception occurs
if converterInstance(src).dimensionality != converterInstance(dst).dimensionality:
return 'cancel', converterInstance(src).dimensionality, converterInstance(dst).dimensionality
# Check if converted value is a whole number, adjust decimal places as required
convertedValue = self.isWhole(number=converterInstance(src).to(dst).magnitude)
return convertedValue, converterInstance(src).dimensionality, converterInstance(dst).dimensionality
def replyWithResults(self, session, answer):
""" End dialog with the converted answer"""
self._comparing = False
self.endDialog(
sessionId=session.sessionId,
text=f"{answer}",
deviceUid=session.deviceUid
)
@staticmethod
def isWhole(number):
"""
Adjust decimal positions based on the converted value
:param number: The converted number
:return: Modified value with required decimal place
"""
if number % 1 == 0:
return int(number)
elif 0.01 <= number % 1 <= 0.09:
return round(number, 3)
elif 0.001 <= number % 1 <= 0.009:
return round(number, 4)
elif 0.0001 <= number % 1 <= 0.0009:
return round(number, 5)
else:
return round(number, 1)
def checkForInvalidInput(self, session) -> bool:
if not session.slotValue('FirstUnit') or not session.slotValue('SecondUnit'):
self.logWarning(f"Sorry but I require two different units to compare")
self.endDialog(
sessionId=session.sessionId,
text=self.randomTalk(text='dialogMessage4'),
deviceUid=session.deviceUid
)
return True
else:
return False