phpmg / octopus

MIT License
4 stars 0 forks source link

number to word conception #1

Open joubertredrat opened 8 years ago

joubertredrat commented 8 years ago

Hi guys,

This is a first conception about translate numbers to words but I have more questions.

If I have more questions I will put here. Let's talk.

lucasmezencio commented 8 years ago

It will be for ANY language in the world. Keep it in mind: ANY. 😃

luizpedone commented 8 years ago

Hey guys! Looking forward to this project. It seems really promising.

Just a few thoughts about it:

If I can help somehow, please ping me on Slack or here. I'll be glad to help.

luizpedone commented 8 years ago

@joubertredrat Just saw your gist and have some suggestions:

luizpedone commented 8 years ago

One more thing, my suggestion about the package usage interface:

<?php

interface NumberToWord {
    public function __construct(Language $language);
    public function getValueInWords($valueInNumbers);
    public function getValueInNumbers($valueInWords);
}

// usage
$value = new NumberToWord(new PortugueseLanguage());
$value->getValueInWords(299); // will output duzentos e noventa e nove
$value->getValueInNumbers('duzentos e noventa e nove'); // will output 299
pedrochaves commented 8 years ago

The question actually is how are we going to structure this file to be generic for every language, since every language has their own specificities.

Example for the number 1700:

The problem here is:

See what I mean? Each language structures numbers in a totally different ways and some even have more than one way to do this. How to structure this file? Am I overcomplicating things?

luizpedone commented 8 years ago

@pedrochaves I agree with you that each language has it's own structure so it's hard to create a generic code to all. That's why I proposed a Language interface in which each language will have it's own implementation of the rules required to process it. It's also simplifies how to start the project: we can start with Portuguese or English, make it work and in the future focus on new languages. Doing that using the interface, we ensure that the NumberToWord class calls exact method for the required language.

luizpedone commented 8 years ago

Guys, just made a commit with the basic structure that I said previously. I think there's several flaws on it, but it's a start.

Do you guys have any idea on how to improve it? I'm quite skeptical about the NumberToWord class. It's a wrapper to the LanguageInterface, but I'm not sure if we need it.

Anyway, it's just a proposal. If you guys think it's good, we can move forward with it. If there's some better approaches to it, feel free to move on another direction.

pedrochaves commented 8 years ago

@luizpedone I thought of some alternatives (I also changed the method names, see what you guys think):

$translator = NumberToWordTranslatorFactory::createForLanguage(Language::PORTUGUESE);
$word = $translator->toWord(299);
$number $translator->toNumber('duzentos');
$translator = new NumberToWordTranslator();
// This would be the __call, setting a private attribute
$translator->loadPortuguese(); 
// Or another option (in this case, the parameter can be passed on constructor)
$translator->load(Language::PORTUGUESE);

$word = $translator->toWord(299);
$number = $translator->toNumber('duzentos');
$translator = new NumberToWordTranslator();

// This would have to be a singleton to avoid creating objects on every call
$word = $translator->portuguese->toWord(299);
$number = $translator->portuguese->toNumber('duzentos');

(enums would be awesome here :()

joubertredrat commented 8 years ago

Hi guys,

For consistent language selector, we need to use RFC 3066 to set language, as in here.