-4

I want to take input in the name form like two-third or one-fifth and I want my system to convert it into numerical form and give the answer.

Que: two-third of thirty is?

The system should output 20

How can I program it?

Sandeep
  • 13
  • 1
  • 2
  • 10

2 Answers2

0

you could try an mapping from

one ->1 
two ->2
three ->3
four ->4
and so on

and on the other hand:

half ->2
third ->3
fourth ->4

then create an double to divide first value with 2nd.. at least multiply this value with the third (you can use the first mapping for this value) and you got the result.

At least, it is not easy due to you have to build the mapping between string and int manually.

silentCode
  • 19
  • 3
  • thanks for the help but this way we have to define the numericals for each and every number which is not possible. Suppose we need to find out two third of two thousand, we cannot do that. – Sandeep Nov 19 '14 at 10:08
  • well, you could try to build up your own language parser. expecting a format "two thousand" would make it easy due to you only need to get the rules. like "teen means +10" so eight teen would be parsed as eight (8) and teen as +10 so you could get 18. This method is a lot of work, but you need to rebuild the rules of numbers. Or you find a library for it. I wouldn't have it posted as answer, but i haven't enough reputation to post as comment ;) – silentCode Nov 19 '14 at 10:13
  • PS: maybe this would help you, eventhough its in Javascript: http://stackoverflow.com/questions/19882831/javascript-parse-string-to-integer – silentCode Nov 19 '14 at 10:15
  • thnks a lot.. this will surely help me.. :-) – Sandeep Nov 19 '14 at 10:24
0

As a general problem natural language processing (NLP) - which is what you're talking about - is a difficult open-ended problem.

There are lots of libraries for this stuff. If you want background look here:

Is there a good natural language processing library

Or look up Natural Language Processing in Wikipedia.

However you said you want to do this and you're new to programming. The first thing you need to do is break the problem down. That's how we solve programming problems.

So first try writing a program that can read a string containing a single word and map it to a number. For example "One" outputs 1, "Two" outputs 2, "Thirty" outputs 30.

Next try and write a program that cuts a string into its constituent words. You probably want to use an array here. That's a process called tokenizing and Java has a built in StringTokenizer to do that. You might want to code that yourself, but you're learning and it might be the moment to start learning using library code.

When you've got those try combining them so your program can convert "Thirty Seven" into 37 (i.e. numbers under 100). That new program should combine the ideas of your program than can convert "Thirty" and "Seven" and the one that can split words up.

This is the other thing we do in programming - combining things. We break it down to smaller problems solve them and then build them back up to solve the bigger problems. (I apologize if I'm patronizing you but I have no idea of your experience).

After that you might add logic that handles "Five Hundred And Thirty Seven". Again, notice how spotting Five followed by Hundred is like converting Five and then finding a token that tells you to multiply what you just saw by 100. You could go on to handle Thousands, Hundred Thousand etc.

Or you could branch off into the fractions. That's similar but you just have a different vocabulary. Seven Forty-Seconds = 7/42.

As a learning challenge I would suggest you'll have come a long way if your program handles things like "forty two ninety-thirds of eight hundred and eighty nine".

The easy solution outputs 0.000508 - the floating point answer to (42/93)*889. The extra credit solution outputs 2/3937 - (42/93)*889 can be simplified as a rational number to 2/3937.

To be honest, you'll be doing well if you can handle "nine-ninths of ninety nine". Notice that the first word is the numerator (n). The second is the denominator (d). The third is always 'of'. The forth word is either the tens (t) or the units (u). If the forth was the units you're done otherwise if there is a fifth word it's the units.
The answer in that case is n/d*(t*10+u). If the tens or units are missing they're zero - obviously. PS: You might need special handling for zero if you object to someone typing in ninety zero. It obviously means ninety but we don't say it in English!

Community
  • 1
  • 1
Persixty
  • 8,165
  • 2
  • 13
  • 35