Author Topic: Unit Converter  (Read 5637 times)

0 Members and 1 Guest are viewing this topic.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Unit Converter
« on: April 01, 2015, 05:17:41 AM »
this unit converter relies on Google, so it need internet connection. the advantage that it can convert from any unit to any unit... just add what you use to xml payload

check https://support.google.com/websearch/answer/3284611?hl=en-CA#unitconverter for full list of supported units ...

here is a new command, that is memory & SP version friendly .... this version requires PY plug-in and attached PY script to be loaded at start-up ( based on http://voxcommando.com/forum/index.php?topic=1769.msg15399#msg15399 which list james PY script)
range is limited between - 999999999.xxx to 999999999.xxx (can be increased !!!!)

I would build multiple commands based on conversion type, ie. one for weight, one for volume ...etc. to increase recognition accuracy (avoid converting 3 cubic meters to square centimeters)

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.2-->
<command id="226" name="Unit Converter" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="&lt;H2 class=r style=&quot;FONT-SIZE: 138%; DISPLAY: inline&quot;&gt;20 kilograms = 44.0924524 pounds&lt;/H2&gt;">
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>Number</param>
      <param><![CDATA[ ]]></param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetLastResult</cmdType>
    <params />
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>{LastSpoken}&amp;&amp;point</ifParams>
    <then>
      <action>
        <cmdType>Results.RegEx</cmdType>
        <params>
          <param>(.*?)\spoint\s(.*?){PF.1}</param>
          <param />
          <param>{LastSpoken}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>PY.ExecString</cmdType>
        <params>
          <param>result = txt2int ("{Match.1.2}")</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Number</param>
          <param>.{LastResult}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Results.SetLastResult</cmdType>
        <params>
          <param>{Match.1.1} {PF.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetLastResult</cmdType>
        <params>
          <param>{LastSpoken}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>Results.Replace</cmdType>
    <params>
      <param>minus</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegEx</cmdType>
    <params>
      <param>Convert\s(.*?){PF.1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result = txt2int ("{Match.1}")</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)Contains(B)</ifType>
    <ifParams>{LastSpoken}&amp;&amp;minus</ifParams>
    <then>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Number</param>
          <param>-{LastResult}{var.Number}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetVar</cmdType>
        <params>
          <param>Number</param>
          <param>{LastResult}{var.Number}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>Results.SetLastResult</cmdType>
    <params>
      <param>{var.Number}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastSpoken}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.AddText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Scrape</cmdType>
    <params>
      <param>https://www.google.ca/search?biw=1920&amp;bih=939&amp;noj=1&amp;site=webhp&amp;q=convert+{LastResult}+{1}+to+{2}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegExSingle</cmdType>
    <params>
      <param>&lt;h2\sclass="r".*?&gt;(.*?)&lt;/h2&gt;</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>LastActionSuccess</ifType>
    <ifParams>&amp;&amp;</ifParams>
    <then>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>{Match.1.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>I think that can not be done !</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <phrase>Convert</phrase>
  <phrase optional="true">minus</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine</phrase>
  <phrase optional="true">hundred</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen</phrase>
  <phrase optional="true">million</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine</phrase>
  <phrase optional="true">hundred</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen</phrase>
  <phrase optional="true">thousand</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine</phrase>
  <phrase optional="true">hundred</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety</phrase>
  <phrase optional="true">zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen</phrase>
  <phrase optional="true">point</phrase>
  <phrase optional="true">one, two, three, four, five, six, seven, eight, nine</phrase>
  <phrase optional="true">hundred</phrase>
  <phrase optional="true">and</phrase>
  <phrase optional="true">twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety</phrase>
  <phrase optional="true">zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen</phrase>
  <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\MeasurementsUnits.xml</payloadFromXML>
  <phrase>to</phrase>
  <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\MeasurementsUnits.xml</payloadFromXML>
</command>
    old command ...

    •    be careful, do not put all units in one xml, as the matrix will be too big and VC will eat-up too much memory for nothing. instead create multiple commands and attach different xml. ex. one command for area, other for volume, third for currency ...etc. and keep it to minimum
    •      limited to the 2 ranges ... keep the ranges small (saves memory)
    •     won't work with SP
    [/li]

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.2-->
<command id="191" name="Convert" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="&lt;H2 class=r style=&quot;FONT-SIZE: 138%; DISPLAY: inline&quot;&gt;20 kilograms = 44.0924524 pounds&lt;/H2&gt;&#xD;&#xA;">
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>(A)==(B)</ifType>
    <ifParams>{#P}&amp;&amp;4</ifParams>
    <then>
      <action>
        <cmdType>Results.SetLastResult</cmdType>
        <params>
          <param>{1}.{2}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Scrape</cmdType>
        <params>
          <param>https://www.google.ca/search?biw=1920&amp;bih=939&amp;noj=1&amp;site=webhp&amp;q=convert+{LastResult}+{3}+to+{4}&amp;oq=convert+20+kg+to+lbs</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>Results.SetLastResult</cmdType>
        <params>
          <param>{1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>Scrape</cmdType>
        <params>
          <param>https://www.google.ca/search?biw=1920&amp;bih=939&amp;noj=1&amp;site=webhp&amp;q=convert+{LastResult}+{2}+to+{3}&amp;oq=convert+20+kg+to+lbs</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <action>
    <cmdType>Results.RegExSingle</cmdType>
    <params>
      <param>&lt;h2\sclass="r".*?&gt;(.*?)&lt;/h2&gt;</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>LastActionSuccess</ifType>
    <ifParams>&amp;&amp;</ifParams>
    <then>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>{Match.1.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>I think that can not be done !</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <phrase>Convert</phrase>
  <payloadRange>1,10000</payloadRange>
  <phrase optional="true">point</phrase>
  <payloadRange optional="true">1,999</payloadRange>
  <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\MeasurementsUnits.xml</payloadFromXML>
  <phrase>to</phrase>
  <payloadFromXML phraseOnly="False" use2partPhrase="False" phraseConnector="by" Phrase2wildcard="anyone" optional="False">payloads\MeasurementsUnits.xml</payloadFromXML>
</command>
« Last Edit: April 02, 2015, 03:36:02 PM by Haddood »
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Unit Converter
« Reply #1 on: April 01, 2015, 09:23:55 AM »
A payload range of 1 to 10000 is not exactly small!

Since you are using Google anyway I wonder if could work using dictation. It would be much lighter and more flexible.

When you create a payload range it actually has to loop through every value and create a voice command for each and every one.

OK if this is the only command you have in your library but if this is part of a full set of commands then these sorts of things add up fast.
« Last Edit: April 01, 2015, 09:42:43 AM by nime5ter »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Unit Converter
« Reply #2 on: April 01, 2015, 09:55:19 AM »
be careful, do not put all units in one xml, as the matrix will be too big and VC will eat-up too much memory for nothing. instead create multiple commands and attach different xml. ex. one command for area, other for volume, third for currency ...etc.

I'm not sure if I understand what you are saying in this part but creating multiple commands each of which uses massive payload ranges will use much more memory than doing it all in one command.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #3 on: April 01, 2015, 06:34:06 PM »
I'm not sure if I understand what you are saying in this part but creating multiple commands each of which uses massive payload ranges will use much more memory than doing it all in one command.

I assume VC (actually MS VR engine) will parse all possiblities of payloads just like with ranges ...
So if we have this command with a range of 10 and the xml file has 2 units we will end up with 40 possible phrases ? No?

Accordingly putting area units with tempreture units will create useless phrases as one can not convert m2 to Celsius ... With a range of only 10 we will 20 useless phrases

However I might be wrong....

Dictation will be great... However I do not have results with it do I avoid it ...
I am thinking of a way, where big ranges needed, to build the number from various parts payload then Python to added up various parts
« Last Edit: April 01, 2015, 06:36:59 PM by Haddood »
When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2009
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Unit Converter
« Reply #4 on: April 02, 2015, 12:56:07 PM »
Dictation will be great... However I do not have results with it do I avoid it ...

It's really unfortunate (and bizarre!) that MS's speech engines handle numbers so poorly in general -- and especially frustrating for non-native speakers trying to use their SR. As you say, regular dictation is not an option unless you have a "standard" accent.

Native speakers, however, should try regular dictation rather than payload ranges in these situations.

@Haddood: have you experimented at all with using dictation:spelling instead?

This requires that you say your numbers as a series of digits, but I wonder if MS can understand accented pronunciation of digits if you use this method?

e.g.
"Convert three two degrees Fahrenheit to Celsius" (Instead of "thirty-two")
"Convert five four six point two five metres to feet" (Instead of "five hundred and forty six point two five")

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.2-->
<command id="191" name="Convert" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="&#xD;&#xA;">
  <action>
    <cmdType>Results.SetLastResult</cmdType>
    <params>
      <param>{1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegExReplace</cmdType>
    <params>
      <param>\s</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Scrape</cmdType>
    <params>
      <param>https://www.google.ca/search?sourceid=chrome-psyapi2&amp;ion=1&amp;espv=2&amp;ie=UTF-8&amp;q=convert+{LastResult} +{2}+to+{3}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.RegEx</cmdType>
    <params>
      <param>&lt;h2\sclass="r".*?&gt;(.*?{3}).*?/h2&gt;</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <if ifBlockDisabled="False" ifNot="False">
    <ifType>LastActionSuccess</ifType>
    <ifParams>&amp;&amp;</ifParams>
    <then>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>{Match.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
      <action>
        <cmdType>OSD.ShowText</cmdType>
        <params>
          <param>{Match.1}</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </then>
    <else>
      <action>
        <cmdType>TTS.Speak</cmdType>
        <params>
          <param>Google seems to think this is not possible.</param>
        </params>
        <cmdRepeat>1</cmdRepeat>
      </action>
    </else>
  </if>
  <phrase>Convert</phrase>
  <payloadDictation>payloadDictation: Spelling</payloadDictation>
  <phrase optional="true">degrees</phrase>
  <payloadList>miles, metres, kilometres, yards, feet, foot, inches, centimetres, millimetres, Celsius, Fahrenheit</payloadList>
  <phrase>to</phrase>
  <payloadList>miles, metres, kilometres, yards, feet, foot, inches, centimetres, millimetres, Celsius, Fahrenheit</payloadList>
</command>

Maybe you could test this and let us know? Actually, it would be great to have multiple people test this out and report back. (By this I mean, anyone here using a Microsoft recognition language that is not their mother tongue.)
« Last Edit: April 02, 2015, 01:13:05 PM by nime5ter »
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)

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #5 on: April 02, 2015, 02:55:16 PM »
@nime5ter ... I do not think the problem is the accent... the problem is in the dictation and open-air mic ... when I specify ranges MS SR have no difficulty in understanding me very well ...

@james, I posted a new command with a new approach to numbers (based on http://voxcommando.com/forum/index.php?topic=1769.msg15399#msg15399 which uses your script)... in fact I was wondering if you think it worth (I am sure you can come with more advanced version of the concept) to be built inside VC to interpret ranges, rather than looping or maybe introducing new phrase type for numbers... the best part of it, it will be SP friendly ... and it is extremely memory friendly ...  the number of optional phrases can be easily found based on the number of digits (i.e if the max is hundred thousand, all the phrases on top of hundred of thousand can be removed) ....

it successfully interpreted "Convert fifteen million nine hundred thirty six thousand and four hundred sixty seven point six hundred thirty five Celsius to Fahrenheit"
« Last Edit: April 02, 2015, 03:17:28 PM by Haddood »
When Voice command gets tough, use hand gestures

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2009
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Unit Converter
« Reply #6 on: April 02, 2015, 03:02:27 PM »
@nime5ter ... I do not think the problem is the accent... the problem is in the dictation and open-air mic ... when I specify ranges MS VR have no difficulty in understanding me very well ...

This doesn't really make sense. The whole point of VoxCommando's controlled grammar -- including number ranges -- is that it limits what the MS VR needs to listen for. That is precisely why it "understands you very well".

The problem with dictation is that you could be saying anything at all, and that is when your accent becomes a problem.

And yes, certainly the open air mic complicates the problem.
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)

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #7 on: April 02, 2015, 03:16:40 PM »
you might  be right nime5ter, I have no idea how the internals of SR work... however, when I dictate to word (using Logitech CAM mic) I do not have real issues with dictation and specifically not with number ... so probably it is the training and the open-air mic
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Unit Converter
« Reply #8 on: April 02, 2015, 04:37:53 PM »
...
So if we have this command with a range of 10 and the xml file has 2 units we will end up with 40 possible phrases ? No?
No.  :D

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #9 on: April 02, 2015, 04:41:14 PM »
No.  :D
forgive my ignorance ... what will end up then? 10 phrases with 2  options? ???
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Unit Converter
« Reply #10 on: April 02, 2015, 04:56:57 PM »
20?

But anyway total possible number of phrases is not the same as memory consumption, nor is it directly proportional to load time.

If you use two payloads one with 5 values and one with 8 values, that means 5x8=40 phrases, but it uses something like 5+8=13 memory units.  The actual units are unknown but the ratios are valid.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #11 on: April 03, 2015, 11:52:52 PM »
Well the lesson is to avoid ranges beyond 10 :) ...

When I first tried the old converter, I used a range of one million and the memory needed by VC jumped from 90MB to almost one GB ...
Maybe that is why MS removed numbers from SP ... To encourage programmers to limit numbers to few words ...
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Unit Converter
« Reply #12 on: April 04, 2015, 09:58:37 AM »
For anyone else following this thread, here are the lessons I think you should keep in mind.

We should always avoid any payloads that are larger than needed.  We should avoid creating more commands than we need especially if those commands use large payloads.

@Haddod: There is a lot of room for reasonable compromise between the values of 10 and 1 million.

I have a few commands that use a range of 1 to 100 for percentage.  No problem there although I find it is usually better to use a list with values like 5, 10, 25, 50 etc..  If you absolutely must use a range from 1 to 1000 for some reason, it will work fine, it's just going to take a bit longer to load and use more memory.

I must point out that in fact, number ranges do work in SP!  Microsoft did not "remove" them.  They just don't work very well for some reason, at least in the English SP engines.  This is particularly baffling since the speech platform was originally designed for machines answering phone calls, where you would expect numbers to be important.  Also they are up to version 11 so it's not like they just didn't get around to it yet.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Unit Converter
« Reply #13 on: April 05, 2015, 02:21:36 PM »
@james ... Excellent comment ...
As well I would like to add, if I may, that all these issues do not come from VC, but rather from MS engines. In fact I think it is the best piece of software that is built on top of MS SR engines.

I have a few commands that use a range of 1 to 100 for percentage.  No problem there although I find it is usually better to use a list with values like 5, 10, 25, 50 etc..

That brings to my mind a suggestion, to introduce a third parameter in range phrase that is step. Say:
1-100, 5
That would be translated into: 1,5,10,15....100 ...
If step parameter is absent it defaults to 1 ... That will maintain compatibility with existing commands
« Last Edit: April 05, 2015, 02:23:49 PM by Haddood »
When Voice command gets tough, use hand gestures