Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to preserve decimal point and zeros when decimal values are all 0? #2

Closed
oshihirii opened this issue May 23, 2017 · 6 comments
Closed

Comments

@oshihirii
Copy link

Hello,

a)  200.001 // returns 200.001
b)  200.0000 // returns 200
c)  200.0001 //  returns 200.0001

How can I preserve the decimal point and zeros in case b?

jsFiddle for testing here.

@oshihirii
Copy link
Author

I'm not sure if this is a good/safe solution, but I ended up modifying the output with the following:

var output = "200.0000"
output.toFixed(4);
200.0000

@howlowck
Copy link
Owner

howlowck commented May 24, 2017

Can you provide the phrase, and the expected numeral output? Otherwise, if you're talking about formatting the output, it's outside the scope of this project.

@oshihirii
Copy link
Author

oshihirii commented May 26, 2017

I am working with speech recognition api and the phrases passed to WtoN can be things like:

a)  200.001 // returns 200.001 by default in WtoN
b)  200.0000 // returns 200 by default in WtoN
c)  200.0001 //  returns 200.0001 by default in WtoN

I think my solution posted above works, ie:

var result = WtoN.convert("200.0000");
result.toFixed(4) // returns 200.0000; 

There are other phrases, however, that I am trying to make successful in WtoN (jsFiddle test here):

Example 01 - Resolved

// BEGIN replace any instances of 'for' and 'to'
// scenarios captured:
// "123 for/to 23456" - middle, space on both sides
// "for/to 23456" - beginning, space on right
// "123 for/to" - end, space on left
// "for/to" - on it's own
var my_word = "for 91089373 to 8";  // by default WtoN outputs  91089387
my_word = my_word.replace(/ for /g, "4").replace(/for /g, "4").replace(/ for/g, "4").replace(/for/g, "4").replace(/ to /g, "2").replace(/to /g, "2").replace(/ to/g, "2").replace(/to/g, "2");

var result = WtoN.convert(my_word) // now outputs correct value ie 49108937328
// END replace any instances of 'for' and 'to'

Example 02 - Unresolved

The following phrases do not currently convert as desired:

"four 91089373 two 8" // returns 91089387
"four 91089373 two" // returns 91089379
"four nine one zero eight nine three seven three two"  // returns 46

So I think the issues can be described as:

"WtoN can not currently handle spaces between words and numbers"

"WtoN can not currently handle spaces between numbers as words and numbers as words"

My first instinct is to remove spaces with:

var my_word = my_word.replace(/ /g, "");

But this will cause errors in phrases like:

"one hundred"

Hmmm...

@oshihirii
Copy link
Author

oshihirii commented May 26, 2017

Here is another jsFiddle that resolves the scenarios above (it's not pretty, but it works i think!).

My approach was to modify the original phrase before passing it to WtoN.

This is done in the function originalPhraseModification().

Will need to test with more phrases to make sure it works as intended.

@howlowck
Copy link
Owner

hm.. I'm not sure WtoN is meant to handle words spoken out digit by digit. If you look under public/js/tests/specs.js you can see that the tests are for texts are are spoken out as whole numbers during natural speech.

I might have some time next few weeks to add some non-whole numbers like "three hundred point one two" to "300.12"

@oshihirii
Copy link
Author

Thank you for your reply, I think my modified version of WtoN is doing all that is required (its just not as nicely coded as yours hehe). I realised the "three hundred point one two" scenario is not really important as I can enforce the convention of saying "three hundred dot one two" which Google will translate to something that my modified version of WtoN can convert. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants