在JavaScript中将Dialogfow持续时间系统实体从分钟转换为秒 [英] Convert Dialogfow duration system entity from minutes to seconds in JavaScript

查看:110
本文介绍了在JavaScript中将Dialogfow持续时间系统实体从分钟转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将JavaScript代码中Dialogflow的Duration(@ sys.duration)系统实体从几分钟转换为几秒的方法.

I am looking for ways to convert a Duration (@sys.duration) system entity from Dialogflow in JavaScript code from minutes to seconds.

我问用户一定的时间,用户可以在哪里回答,例如:

I ask the user for a certain duration, where the user can answer, e.g.:

  • 20分钟
  • 5分钟

该输入被保存到变量the_duration中.现在要进行某些计算,我需要将其转换为秒.我该如何实现?

that input is saved into a variable the_duration. Now to do certain calculations, I need to convert this into seconds. How can I achieve this?

如果我需要从字符串中提取数字,也许会有所帮助?我已经尝试过寻找这种方式,但是提供的示例实际上并没有在几分钟之内适用于这种特定情况.

Perhaps it would help if I need to extract the number from the string? I've tried looking for this way, but provided examples don't really apply for this specific case with minutes.

推荐答案

@sys.duration

The @sys.duration system entity will send you an Object with two attributes, "amount" which contains an integer, and "unit" which contains a string.

因此在Javascript中,这将表示为类似的内容:

So in Javascript, this would be represented something like:

{
  "amount": 20,
  "unit": "min"
}

要将其转换为秒,您需要查找提供的单位"中有多少秒,然后乘以该数量.

To convert this to seconds, you would lookup how many seconds are in the "unit" provided and multiply it by the amount.

执行此查找的一种好方法是创建一个对象,该对象具有可能的单位名称和值(以秒为单位)作为属性.这适用于大多数单位(长达一周).但是,当您遇到一个月或一年(或更长时间)时,就会遇到麻烦,因为这些时间段的秒数是可变的.为了表示这些,我将其标记为负数,以便您可以检查转换是否失败. (我忽略了时钟更改的问题,例如由于夏令时/夏令时.)

A good way to do this lookup would be to create an object that has, as attributes, the possible unit names and as values the number of seconds. This works well for most units up to a week. When you hit a month or a year (or longer), however, you run into trouble since the number of seconds for these periods can be variable. To represent these, I'll mark these as a negative number, so you can check if the conversion failed. (I'm ignoring issues with clock changes such as due to Daylight Saving Time / Summer Time.)

我尚未完全测试此代码,但它似乎是正确的.此函数使您可以传递在the_duration参数中发送的对象,并将返回秒数:

I haven't fully tested this code, but it appears to be correct. This function lets you pass the object sent in the the_duration parameter and will return the number of seconds:

function durationToSeconds( duration ){
  const mult = {
    "s":      1,
    "min":    60,
    "h":      60*60,
    "day":    60*60*24,
    "wk":     60*60*24*7,
    "mo":     -1,
    "yr":     -1,
    "decade": -1
  };
  return duration.amount * mult[duration.unit];
}

从字符串中提取数字当然是可能的,您可以修改此功能以使其工作,但是由于Dialogflow已经将它作为具有标准化字符串的对象提供给您,因此要困难得多.

Extracting the number from the string is certainly possible, and you can adapt this function to work that way, but since Dialogflow already gives it to you as an object with normalized strings, it would be significantly more difficult.

这篇关于在JavaScript中将Dialogfow持续时间系统实体从分钟转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆