【问题标题】:Convert Alexa duration slot to number of mins将 Alexa 持续时间槽转换为分钟数
【发布时间】:2019-01-19 18:15:16
【问题描述】:

我只是想知道是否有人有一些我可以回收的代码。

我想将作为字符串传递到我的 python 代码中的持续时间的 alexa 插槽类型转换为分钟数......

我想我可以做到,但是这需要一段时间,因为我的字符串处理不是很好,我想我最终会写大约 30 行代码:)

因此,将PD2T5H25M 之类的内容转换为:

2*24*60  +  5* 60  + 25 = answer.
(i don't need to handle years)

如果有人已经这样做了,我将非常感激?

干杯

【问题讨论】:

标签: python alexa duration slot


【解决方案1】:

您可以使用正则表达式,这里有一个处理特定情况的简单示例:

import re

txt='PD2T5H25M'

re1='.*?'       # Non-greedy match on filler
re2='(D)'       # Single Character D
re3='(\\d+)'    # Integer Number 1
re4='.*?'       # Non-greedy match on filler
re5='(\\d+)'    # Integer Number 2
re6='(H)'       # Single Character H
re7='(\\d+)'    # Integer Number 3
re8='(M)'       # Single Character M

rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8,re.IGNORECASE|re.DOTALL)
m = rg.search(txt)
if m:
    w1   = m.group(1)
    int1 = int(m.group(2))
    int2 = int(m.group(3))
    w2   = m.group(4)
    int3 = int(m.group(5))
    w3   = m.group(6)
    print((int1*24*60) + (int2*60) + int3)

【讨论】:

  • 这很有帮助,谢谢,我找到了一种使用上述 flask-ask 函数的方法,但改天会记住该技术。
【解决方案2】:

所以,经过一番挖掘,以供其他人将来参考,这就是发生的事情。

我发现写flask-ask的聪明人已经完成了所有的艰苦工作。

如果你从 Alexa 传入一个 slot 类型的时长,你可以使用 'timedelta' 选项来转换它:像这样:

@ask.intent("DurationIntent",convert={'dduration': 'timedelta'})
def duration(dduration):

  print dduration
  try:
    totalmins = dduration.total_seconds()/60
  except:
    print "oh no"
    write_log ('Alexa bad duration', str(dduration))
    return statement('the duration you provided could not be determined please speak more proper - check the log for details')


  print totalmins
  return statement('that is ' + str(int(totalmins)) + 'minutes')

。 . . (write_log 是我自己的函数,因此我可以跟踪任何错误并微调意图和表达以更加灵活)

“Alexa 询问转换 - 4 天 7 小时 22 分钟是多少分钟”

她回复了……

“那是 6202 分钟”

结合数年、数周和数小时,它似乎不能 100% 工作,但它足以让我提升一些家庭自动化区域......所以现在我要更换我的具有“持续时间”的 int 插槽类型及其更加灵活...感谢 flask-ask 先生

干杯

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多