在JavaScript中将Dialogfow持续时间系统实体从分钟转换为秒

编程入门 行业动态 更新时间:2024-10-23 21:29:46
本文介绍了在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 系统实体将向您发送一个具有两个属性的对象,"amount"包含一个整数,而"unit"包含一个字符串.

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持续时间系统实体从分钟转换为秒

本文发布于:2023-10-16 12:11:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   持续时间   实体   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!