-3

Does anyone know how to convert something like quatre-vingt mille quatre cent quatre-vingt-dix-sept to 80497 in Java?

Mansour.M
  • 500
  • 4
  • 18
  • You posted this same question a bit ago... I'm guessing it was closed because questions asking about libraries are considered off-topic: https://stackoverflow.com/help/on-topic – sleepToken Feb 04 '20 at 15:15
  • 1
    I'm not asking for a library. I'm asking for methodology. Read the question. – Mansour.M Feb 04 '20 at 15:16
  • In that case, probably the same way you would convert English to an `int`. You'd have to map keywords to values. – sleepToken Feb 04 '20 at 15:18
  • That is where you are wrong. I thought the same at first, but French won't work like that. – Mansour.M Feb 04 '20 at 15:20
  • Check out https://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java, you might be able to understand and reverse that. – lugiorgi Feb 04 '20 at 15:23
  • 1
    Sounds like you need a French expert then - not *stackOverflow*. Or just translate french to English first and work with that. – sleepToken Feb 04 '20 at 15:23
  • @lugiorgi Thanks for the link. The problem with that is that parsing the string. In French numbers are expressed in chunks. When you get the length of the number it is easy which chunk to use to describe. For example when you get a number with 4 digits you know you have to process the left most with thousand describing chunks and the second with hundred describing one and so on. However, when it comes to converting words to digits, you'll have to find away to know which chunk refers to where. That does not obey any pattern. Or I have not found any yet. – Mansour.M Feb 04 '20 at 15:29
  • 1
    @mnsr I mean, it must obey *some* pattern, right? Otherwise how else would French-speaking people know what number it was? I'm not trying to be difficult - I'm saying that that this is a tricky problem but probably focuses more around the nuances of the French language, rather than the difficulties of translating that logic into code. – sleepToken Feb 04 '20 at 15:31
  • You can write a parser for that. Read about parsing and parsers, there’s a whole lot written. – Ole V.V. Feb 04 '20 at 15:34
  • @sleepToken It should yes. But if it was that easy it wouldn't be an open question. Someone has solved it in Python but I was able to find cases which even that fails. So, something is missing. – Mansour.M Feb 04 '20 at 15:40
  • @OleV.V. Of course but no matter what a parser, or a gazetteer as another option, cover there always seems to be irregular cases. Anyway, the question is closed now. – Mansour.M Feb 04 '20 at 15:43
  • It sounds like you first need to establish exactly what the rules are. As someone who doesn’t know French, I only know a few rules from looking at your example. – VGR Feb 04 '20 at 18:44
  • @VGR You are correct. I was actually able to solve it with few simple rules. Not knowing French had its toll but I managed to solve it. It works better than the Python library and it is simpler too. – Mansour.M Feb 05 '20 at 14:03

1 Answers1

1

I know this is considered off-topic, but as he explains; he is asking for a methodology. I have suggestions:

One of two possible options would be putting all translation in an array equal to the index of those values in their array: {un, deux, troix, quatre} and so on.

Another option is using a switch statement and a stringbuilder. You can use case titles to capture certain parts of the number to have the case add them to a stringbuilder that in the end contains the result of the intepretation trough the switch statement.

I hope this sends you in the right direction.

Quadrivics
  • 181
  • 2
  • 10
  • Thanks for the response. Honestly, I don't get how this is off topic. I am asking a question about an actual problem that has no solution in Java. Anyway, your first method works but I don't have a max limit so it is not possible to provide all translations. Your second solution is for when I want to convert int to string. But I want to convert string to int. It is not possible to use the same procedure for string to int. Because a number like 80 is described as two forties in French. Now when a case like that is part of a large number it can break any pattern. – Mansour.M Feb 04 '20 at 15:38