Author Topic: Rounding Numbers with Decimals  (Read 2977 times)

0 Members and 3 Guests are viewing this topic.

Dave.ca

  • Jr. Member
  • **
  • Posts: 10
  • Karma: 0
    • View Profile
Rounding Numbers with Decimals
« on: April 14, 2014, 03:49:04 PM »
The Wunder plugin returns temperatures in a form that is not condusive to "natural" speech. Example:

Me: "Computer, please tell me the current temperature."

Computer: "The current temperature is 20.3 degrees celsius."

I would much rather hear:

Computer: "The current temperature is 20 degress celsius."

Is there a RND or INT command that would convert {c.temp_c} to an integer number? Is there a way to do this in the Wunder plugin itself?

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Rounding Numbers with Decimals
« Reply #1 on: April 14, 2014, 05:41:26 PM »
Interesting.  In our neck of the woods (and I do mean woods), Wunderground only gives us whole numbers.

If you asked lieutenant commander Data I believe that he would answer you with the decimal precision.

There are at least two ways you could correct this.  If you have only the temperature then you could use the python plugin, which does have an int function.

example:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 1.9.2.1-->
<command id="218" name="wunder vague temps" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <params>
      <param>{C.temp_c}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=int({LastResult})</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Or if you wanted to grab multiple variables in one shot from the weather underground and format it with text, then you would have trouble using this method.  In this case you should be able to use result.regex replace to detect a pattern of "digit period digit" and replace it with the first digit only.  Of course if you had some other variable which included an extra level of precision that you wanted to keep then this would not work.

example:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 1.9.2.1-->
<command id="222" name="wunder vague temps 2" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <params>
      <param>The current temperature is {C.temp_c} degrees, and it feels like {C.feelslike_c}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegExReplace</cmdType>
    <params>
      <param>(\d)\.\d</param>
      <param>$1</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Dave.ca

  • Jr. Member
  • **
  • Posts: 10
  • Karma: 0
    • View Profile
Re: Rounding Numbers with Decimals
« Reply #2 on: April 14, 2014, 06:36:35 PM »
Yes, after posting I continued to dig and found that the python module gives us the ability to perform math (and other) functions. As it is only the temperature that I am concerned with it turned out to be a rather simple process.

Code: [Select]

<?xml version="1.0" encoding="utf-16"?>
<command id="404" name="Tell Me The Temperature" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>{c.temp_c}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result= int({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.Speak</cmdType>
    <cmdString>The current temperature is {LastResult} degrees celsius.</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Please</phrase>
  <phrase>Tell me the temperature</phrase>
</command>


It is curious that I am receiving the temperature to decimal precision and you are not.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Rounding Numbers with Decimals
« Reply #3 on: April 14, 2014, 06:42:33 PM »
It depends on your location.  I set my zip code to 90210 and it gives it to me with the extra decimal.  I guess their data gathering from various sources is not completely standardized.

Dave.ca

  • Jr. Member
  • **
  • Posts: 10
  • Karma: 0
    • View Profile
Re: Rounding Numbers with Decimals
« Reply #4 on: April 14, 2014, 08:02:44 PM »
Are you rolling in VoxCommando big bucks and aspiring to that 90210 zip code?  ;D

Here's a question. Is there any way to call a subroutine in any of the modules? The code below basicly does the same thing four times to say "The current temperature is 14 degrees celsius or 58 degrees farenheit, and feels like 18 or 64."

Code: [Select]

<?xml version="1.0" encoding="utf-16"?>
<command id="404" name="Tell Me The Temperature" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>{c.temp_c}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result= int({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <cmdString>degreesC&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>{c.temp_f}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result= int({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <cmdString>degreesF&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>{c.feelslike_c}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result= int({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <cmdString>feelsC&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>{c.feelslike_f}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <cmdString>result= int({LastResult})</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <cmdString>feelsF&amp;&amp;{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>TTS.Speak</cmdType>
    <cmdString>The current temperature is {Var.degreesC} degrees celsius or {Var.degreesF} degrees farenheit, and feels like {Var.feelsC} or {Var.feelsF}.</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Please</phrase>
  <phrase>Tell me the temperature</phrase>
</command>


jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Rounding Numbers with Decimals
« Reply #5 on: April 14, 2014, 08:47:08 PM »
You can define and then call a python function but it won't change how you are doing what you are doing here, since all you are doing is calling a function anyway (int).  You can't just put a python function into any action parameter in the LCB.  It sounds like you want to call a bunch of different functions within an action parameter and you can't do that in VC.

My example above on the other hand, which uses results.regex will solve your problem, and allow you to do them all in one shot.

When you say "Is there any way to call a subroutine in any of the modules?"  I actually don't know what you mean.  I don't know what you mean by "the modules", and when you say "a subroutine" I am guessing that you are talking about a python function, but not sure.  My answer is mostly based on the example you posted.

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Rounding Numbers with Decimals
« Reply #6 on: April 14, 2014, 08:49:56 PM »
Hi Dave.ca,

I guess maybe you're just asking in general terms for future reference, but it seems to me that in this particular example, it would make more sense to implement James's second solution, which is three basic actions. As in:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="222" name="Tell me the temperature" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>The current temperature is {C.temp_c} degrees Celsius or {C.temp_f} Fahrenheit, and it feels like {C.feelslike_c} or {c.feelslike_f} Fahrenheit.</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegExReplace</cmdType>
    <cmdString>(\d)\.\d&amp;&amp;$1</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <cmdString>{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Please</phrase>
  <phrase>Tell me the temperature</phrase>
</command>
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Rounding Numbers with Decimals
« Reply #7 on: April 14, 2014, 08:58:32 PM »
Are you rolling in VoxCommando big bucks and aspiring to that 90210 zip code?  ;D

Yes.  Thanks to VC I now have more money than God can count, ;) but I don't aspire to living in that ZipCode.  It is just the only one I can remember, thanks to that terrible TV show, which I totally watched...

Dave.ca

  • Jr. Member
  • **
  • Posts: 10
  • Karma: 0
    • View Profile
Re: Rounding Numbers with Decimals
« Reply #8 on: April 15, 2014, 12:18:52 AM »
Hi Dave.ca,

I guess maybe you're just asking in general terms for future reference, but it seems to me that in this particular example, it would make more sense to implement James's second solution, which is three basic actions. As in:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<command id="222" name="Tell me the temperature" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>WUnder.GetCustom</cmdType>
    <cmdString>The current temperature is {C.temp_c} degrees Celsius or {C.temp_f} Fahrenheit, and it feels like {C.feelslike_c} or {c.feelslike_f} Fahrenheit.</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegExReplace</cmdType>
    <cmdString>(\d)\.\d&amp;&amp;$1</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <cmdString>{LastResult}</cmdString>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>Please</phrase>
  <phrase>Tell me the temperature</phrase>
</command>

This is a much more efficent piece of code. It took me a few minutes to grasp the synatx, as I could not find a reference to the "\d" (for digit)  in the wiki explanation for Results.RegExReplace. Are there other codes and if so is there a link to a list (as there is for the Custom DateTime Codes, )?

vulcanjedi

  • $upporter
  • Sr. Member
  • *****
  • Posts: 213
  • Karma: 8
    • View Profile
Re: Rounding Numbers with Decimals
« Reply #9 on: April 15, 2014, 01:30:00 AM »
http://overapi.com/regex/?_nospa=true
http://www.regexr.com/
Not sure if these are applicable or what you are referring to but I've used for some regex hell in the past.
« Last Edit: April 15, 2014, 01:36:21 AM by vulcanjedi »

Dave.ca

  • Jr. Member
  • **
  • Posts: 10
  • Karma: 0
    • View Profile
Re: Rounding Numbers with Decimals
« Reply #10 on: April 15, 2014, 07:30:22 AM »
http://overapi.com/regex/?_nospa=true
http://www.regexr.com/
Not sure if these are applicable or what you are referring to but I've used for some regex hell in the past.

Wow... Be carefull what you ask for...  ;D

Thanks.

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Rounding Numbers with Decimals
« Reply #11 on: May 04, 2014, 04:41:50 PM »
This is a much more efficent piece of code. It took me a few minutes to grasp the synatx, as I could not find a reference to the "\d" (for digit)  in the wiki explanation for Results.RegExReplace. Are there other codes and if so is there a link to a list (as there is for the Custom DateTime Codes, )?

Good point. A link to a regex reference "cheat sheet" is provided in the standard RegEx action (http://voxcommando.com/mediawiki/index.php?title=Actions#RegEx), but it should probably be repeated in the description of all the RegEx actions.

(thanks also @vulcanjedi -- I just noticed this thread now)
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)