Convert Alexa duration slot to number of mins












0















I was just wondering if anyone has a bit of code I could recycle please.



I want to convert an alexa slot type of duration passed into my python code as a string into a number of minutes....



I think I could do it, but it would take a while as my string handling is not great and I think I'd end up writing about 30 lines of code :)



So convert something like PD2T5H25M to :



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


I'd be very grateful if anyone has this already done?



cheers










share|improve this question

























  • Do you know what P,D and T stand for in PD2T5H25M?

    – alec_djinn
    Jan 21 at 13:27











  • its documented here : developer.amazon.com/docs/custom-skills/…

    – cobrachedz
    Jan 21 at 15:38


















0















I was just wondering if anyone has a bit of code I could recycle please.



I want to convert an alexa slot type of duration passed into my python code as a string into a number of minutes....



I think I could do it, but it would take a while as my string handling is not great and I think I'd end up writing about 30 lines of code :)



So convert something like PD2T5H25M to :



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


I'd be very grateful if anyone has this already done?



cheers










share|improve this question

























  • Do you know what P,D and T stand for in PD2T5H25M?

    – alec_djinn
    Jan 21 at 13:27











  • its documented here : developer.amazon.com/docs/custom-skills/…

    – cobrachedz
    Jan 21 at 15:38
















0












0








0








I was just wondering if anyone has a bit of code I could recycle please.



I want to convert an alexa slot type of duration passed into my python code as a string into a number of minutes....



I think I could do it, but it would take a while as my string handling is not great and I think I'd end up writing about 30 lines of code :)



So convert something like PD2T5H25M to :



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


I'd be very grateful if anyone has this already done?



cheers










share|improve this question
















I was just wondering if anyone has a bit of code I could recycle please.



I want to convert an alexa slot type of duration passed into my python code as a string into a number of minutes....



I think I could do it, but it would take a while as my string handling is not great and I think I'd end up writing about 30 lines of code :)



So convert something like PD2T5H25M to :



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


I'd be very grateful if anyone has this already done?



cheers







python alexa duration slot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 21:18









Tiw

2,56621026




2,56621026










asked Jan 19 at 18:15









cobrachedzcobrachedz

122




122













  • Do you know what P,D and T stand for in PD2T5H25M?

    – alec_djinn
    Jan 21 at 13:27











  • its documented here : developer.amazon.com/docs/custom-skills/…

    – cobrachedz
    Jan 21 at 15:38





















  • Do you know what P,D and T stand for in PD2T5H25M?

    – alec_djinn
    Jan 21 at 13:27











  • its documented here : developer.amazon.com/docs/custom-skills/…

    – cobrachedz
    Jan 21 at 15:38



















Do you know what P,D and T stand for in PD2T5H25M?

– alec_djinn
Jan 21 at 13:27





Do you know what P,D and T stand for in PD2T5H25M?

– alec_djinn
Jan 21 at 13:27













its documented here : developer.amazon.com/docs/custom-skills/…

– cobrachedz
Jan 21 at 15:38







its documented here : developer.amazon.com/docs/custom-skills/…

– cobrachedz
Jan 21 at 15:38














2 Answers
2






active

oldest

votes


















1














You can use regex, here a simple example working on that particular case:



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)





share|improve this answer
























  • thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

    – cobrachedz
    Jan 21 at 15:41



















0














So, after a bit of digging, for future reference for others, here's what happened.



I found that the very clever man that wrote flask-ask has already done all the hard work.



If you pass in a slot type of duration from Alexa, you can use the 'timedelta' option to convert it: like this:



@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 is my own function so i can track any errors and fine tune the intents and utterances to be more flexible)



"Alexa ask convert - how many minutes is 4 days and 7 hours and 22 minutes"



she replies......



"That is 6202 minutes"



It doesn't seem to work 100% of the time with a combination of years and weeks and hours, but its more than good enough for me to boost some home automation zones....so now i'll replace my slot types of int with "duration" and its all far more flexible...thanks mr flask-ask



cheers






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270009%2fconvert-alexa-duration-slot-to-number-of-mins%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can use regex, here a simple example working on that particular case:



    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)





    share|improve this answer
























    • thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

      – cobrachedz
      Jan 21 at 15:41
















    1














    You can use regex, here a simple example working on that particular case:



    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)





    share|improve this answer
























    • thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

      – cobrachedz
      Jan 21 at 15:41














    1












    1








    1







    You can use regex, here a simple example working on that particular case:



    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)





    share|improve this answer













    You can use regex, here a simple example working on that particular case:



    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)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 21 at 13:39









    alec_djinnalec_djinn

    2,47222036




    2,47222036













    • thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

      – cobrachedz
      Jan 21 at 15:41



















    • thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

      – cobrachedz
      Jan 21 at 15:41

















    thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

    – cobrachedz
    Jan 21 at 15:41





    thats very helpful thanks, i found a way using the flask-ask functions as above, but will remember that technique for another day.

    – cobrachedz
    Jan 21 at 15:41













    0














    So, after a bit of digging, for future reference for others, here's what happened.



    I found that the very clever man that wrote flask-ask has already done all the hard work.



    If you pass in a slot type of duration from Alexa, you can use the 'timedelta' option to convert it: like this:



    @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 is my own function so i can track any errors and fine tune the intents and utterances to be more flexible)



    "Alexa ask convert - how many minutes is 4 days and 7 hours and 22 minutes"



    she replies......



    "That is 6202 minutes"



    It doesn't seem to work 100% of the time with a combination of years and weeks and hours, but its more than good enough for me to boost some home automation zones....so now i'll replace my slot types of int with "duration" and its all far more flexible...thanks mr flask-ask



    cheers






    share|improve this answer






























      0














      So, after a bit of digging, for future reference for others, here's what happened.



      I found that the very clever man that wrote flask-ask has already done all the hard work.



      If you pass in a slot type of duration from Alexa, you can use the 'timedelta' option to convert it: like this:



      @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 is my own function so i can track any errors and fine tune the intents and utterances to be more flexible)



      "Alexa ask convert - how many minutes is 4 days and 7 hours and 22 minutes"



      she replies......



      "That is 6202 minutes"



      It doesn't seem to work 100% of the time with a combination of years and weeks and hours, but its more than good enough for me to boost some home automation zones....so now i'll replace my slot types of int with "duration" and its all far more flexible...thanks mr flask-ask



      cheers






      share|improve this answer




























        0












        0








        0







        So, after a bit of digging, for future reference for others, here's what happened.



        I found that the very clever man that wrote flask-ask has already done all the hard work.



        If you pass in a slot type of duration from Alexa, you can use the 'timedelta' option to convert it: like this:



        @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 is my own function so i can track any errors and fine tune the intents and utterances to be more flexible)



        "Alexa ask convert - how many minutes is 4 days and 7 hours and 22 minutes"



        she replies......



        "That is 6202 minutes"



        It doesn't seem to work 100% of the time with a combination of years and weeks and hours, but its more than good enough for me to boost some home automation zones....so now i'll replace my slot types of int with "duration" and its all far more flexible...thanks mr flask-ask



        cheers






        share|improve this answer















        So, after a bit of digging, for future reference for others, here's what happened.



        I found that the very clever man that wrote flask-ask has already done all the hard work.



        If you pass in a slot type of duration from Alexa, you can use the 'timedelta' option to convert it: like this:



        @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 is my own function so i can track any errors and fine tune the intents and utterances to be more flexible)



        "Alexa ask convert - how many minutes is 4 days and 7 hours and 22 minutes"



        she replies......



        "That is 6202 minutes"



        It doesn't seem to work 100% of the time with a combination of years and weeks and hours, but its more than good enough for me to boost some home automation zones....so now i'll replace my slot types of int with "duration" and its all far more flexible...thanks mr flask-ask



        cheers







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 21 at 13:29

























        answered Jan 21 at 13:13









        cobrachedzcobrachedz

        122




        122






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270009%2fconvert-alexa-duration-slot-to-number-of-mins%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre