Check if string is a number

I know that you can check if a string is an integer by simply doing Integer.parseInt("1234");
But I want this to associate a text number with an integer.
those.

  • "One" == 1
  • "Two" == 2
  • "Three" == 3
  • "Twenty" == 20

Is there any library that I can use for this, or do I need to program it all manually?

The reason I want to do this is because I have an Android application with speech recognition.
Then the user can count. This number is displayed on the screen.

EDIT
After some experimentation, I realized that the SpeechRecognizer class that I use automatically parses numbers to actual numbers ...

+7
source share
2 answers

I'm not sure if there is a library for this, but here is a good example.

+5
source

As far as I know, there is no library for this, but you can think like this:

create three types of tokens

a. one, two, three .................... nine

b. eleven - nineteen, twenty, thirty, ............ ninety

c. one hundred, one thousand, ........ and larger values

Now analyze your input line and match it with your tokens. Just for a basic idea, you can think like this:

Step 1. Create Tokens from the Token Token

step 2. corresponds to the rightmost line and matches tokens

Step 3. Match one line to the right and match with tokens and calculate

e.g. ONE THOUSAND FIVE HUNDRED FIFTY THREE

let sum = 0

first line = Thus, the sum + = 3

second line = FiFTY, therefore sum + = 50;

third line = One Hundred, so you need to multiply the fourth line by 100 and add so sum + = f * 100 .. etc.

This is just a basic idea. That way you can implement it perfectly after planning it right

+2
source

All Articles