Skip to content

Commit

Permalink
Merge pull request #4 from pablodz/feat/fr
Browse files Browse the repository at this point in the history
working partially fr
  • Loading branch information
pablodz authored May 19, 2024
2 parents fc1c55f + a3f43ba commit f65ed39
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Check [folder](/examples/)
## Supported languages

- ✅ español
- ✅ inglés
- ✅ portugués
- 🌀 francés
- 🌀 italiano
- 🌀 portugués
- 🌀 alemán
- 🌀 ruso
- 🌀 Catalan
15 changes: 15 additions & 0 deletions examples/alpha/fr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/pablodz/itn/itn"
)

func main() {
itn.SetDebug(true)

processor, _ := itn.NewLanguage(itn.French)
new_string := processor.Alpha2Digit("un millième", false, true, 3)
println(new_string)
println("-----------------------------------------------------")
println("un 1000ème")
}
54 changes: 52 additions & 2 deletions itn/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Language struct {
RadMap map[string]string // Optional
Composites map[string]int // Optional
PtOrdinals map[string]string // Only for Portuguese
IrrOrd map[string]RelaxTuple
}

type RelaxTuple struct {
Expand All @@ -37,6 +38,41 @@ type RelaxTuple struct {

func (lg *Language) Ord2Card(word string) string {
switch lg.LangCode {
case French:
logPrintf(">>>> Ord2Card.0 [word] %s", word)
if containsKey(lg.IrrOrd, word) {
logPrintf(">>>> Ord2Card.1 %s", word)
return lg.IrrOrd[word].Zero
}

plurSuff := strings.HasSuffix(word, "ièmes")
singSuff := strings.HasSuffix(word, "ième")
if !(plurSuff || singSuff) {
logPrintf(">>>> Ord2Card.2 %s", word)
return ""
}

source := ""
if plurSuff {
source = word[:len(word)-5]
} else {
source = word[:len(word)-4]
}

if source == "cinqu" {
source = "cinq"
} else if source == "neuv" {
source = "neuf"
} else if !containsKey(lg.Numbers, source) {
source = source + "e"
if !containsKey(lg.Numbers, source) {
logPrintf(">>>> Ord2Card.3 %s", source)
return ""
}
}
logPrintf(">>>> Ord2Card.4 %s", source)
return source

case Portuguese:
logPrintf(">>>> Ord2Card.0 [word] %s", word)
if len(word) < 1 {
Expand Down Expand Up @@ -98,8 +134,20 @@ func (lg *Language) Ord2Card(word string) string {

func (lg *Language) NumOrd(digits string, originalWord string) string {
switch lg.LangCode {
case English:
case French:
logPrintf(">>>> NumOrd.0 %s", originalWord)

if containsKey(lg.IrrOrd, originalWord) {
return lg.IrrOrd[originalWord].One
}

if strings.HasSuffix(originalWord, "e") {
return fmt.Sprintf("%s%s", digits, "ème")
}
return fmt.Sprintf("%s%s", digits, "èmes")

case English:
logPrintf(">>>> NumOrd.1 %s", originalWord)
sf := ""
if strings.HasSuffix(originalWord, "s") {
sf = originalWord[len(originalWord)-3:]
Expand All @@ -110,7 +158,7 @@ func (lg *Language) NumOrd(digits string, originalWord string) string {
return fmt.Sprintf("%s%s", digits, sf)

case Portuguese, Spanish:
logPrintf(">>>> NumOrd.1 %s", originalWord)
logPrintf(">>>> NumOrd.2 %s", originalWord)
if strings.HasSuffix(originalWord, "o") {
return fmt.Sprintf("%sº", digits)
}
Expand All @@ -124,6 +172,8 @@ func (lg *Language) NumOrd(digits string, originalWord string) string {

func (lg *Language) Normalize(word string) string {
switch lg.LangCode {
case French:
return strings.ReplaceAll(word, "vingts", "vingt")
default:
return word
}
Expand Down
134 changes: 132 additions & 2 deletions itn/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,143 @@ type LanguageCode int
const (
Spanish LanguageCode = iota
English
French
Portuguese
French
Deutsch
Russian
Catalan
)

func NewLanguage(LangCode LanguageCode) (*Language, error) {
switch LangCode {
case French:

l := &Language{
LangCode: LangCode,
Multipliers: map[string]int{
"mil": 1000,
"mille": 1000,
"milles": 1000,
"million": 1000000,
"millions": 1000000,
"milliard": 1000000000,
"milliards": 1000000000,
},
Units: map[string]int{
"un": 1,
"deux": 2,
"trois": 3,
"quatre": 4,
"cinq": 5,
"six": 6,
"sept": 7,
"huit": 8,
"neuf": 9,
"une": 1, // optional
},
STens: map[string]int{
"dix": 10,
"onze": 11,
"douze": 12,
"treize": 13,
"quatorze": 14,
"quinze": 15,
"seize": 16,
"dix-sept": 17,
"dix-huit": 18,
"dix-neuf": 19,
},
MTens: map[string]int{
"vingt": 20,
"trente": 30,
"quarante": 40,
"cinquante": 50,
"soixante": 60,
"septante": 70,
"huitante": 80,
"nonante": 90,
"quatre-vingt": 80, // with hyphen
"octante": 80, // with typo
},
MTensWSTens: []string{
"soixante",
"quatre-vingt",
},
Hundred: map[string]int{
"cent": 100,
"cents": 100,
},
Sign: map[string]string{
"plus": "+",
"moins": "-",
},
Zero: []string{
"zéro",
},
DecimalSep: "virgule",
DecimalSYM: ",",
AndNums: []string{
"un",
"une",
"unième",
"onze",
"onzième",
},

And: "et",
NeverIfAlone: []string{
"un",
"une",
},
Relaxed: map[string]RelaxTuple{
"quatre": {"vingt", "quatre-vingt"},
},
Composites: map[string]int{},
IrrOrd: map[string]RelaxTuple{
"premier": {"un", "1er"},
"première": {"un", "1ère"},
"second": {"deux", "2nd"},
"seconde": {"deux", "2nde"},
},
}

for k1, v1 := range l.MTens {
for k2, v2 := range l.Units {
if v2 != 1 {
l.Composites[fmt.Sprintf("%s-%s", k1, k2)] = v1 + v2
}
}
}

for k1, v1 := range l.MTens {
for k2, v2 := range map[string]int{"et-un": 1, "et-une": 1} {
if v1 > 10 && v2 <= 90 {
l.Composites[fmt.Sprintf("%s-%s", k1, k2)] = v1 + v2
}
}
}

l.Composites["quatre-vingt-un"] = 81

for k1, v1 := range map[string]int{"soixante": 60, "quatre-vingt": 80} {
for k2, v2 := range l.STens {
l.Composites[fmt.Sprintf("%s-%s", k1, k2)] = v1 + v2
}
}

l.Composites["soixante-et-onze"] = 71

// deep copy from l.multipliers
l.Numbers = maps.Clone(l.Multipliers)
maps.Copy(l.Numbers, l.Units)
maps.Copy(l.Numbers, l.STens)
maps.Copy(l.Numbers, l.MTens)
maps.Copy(l.Numbers, l.Hundred)
maps.Copy(l.Numbers, l.Composites)

l.Numbers["quatre-vingts"] = 80

return l, nil
case Spanish:
l := &Language{
LangCode: LangCode,
Expand Down Expand Up @@ -135,7 +266,6 @@ func NewLanguage(LangCode LanguageCode) (*Language, error) {
maps.Copy(l.Numbers, l.STens)
maps.Copy(l.Numbers, l.MTens)
maps.Copy(l.Numbers, l.Hundred)
maps.Copy(l.Numbers, l.MTens)

return l, nil

Expand Down
Loading

0 comments on commit f65ed39

Please sign in to comment.