From 640faef4e1d6125dbb5a634d8a066332cfb1a603 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 30 May 2022 15:56:19 -0300 Subject: [PATCH 01/14] added pt_BR --- .gitignore | 3 +- SpelledOut.jl.Rproj | 13 +++ docs/src/supported_languages.md | 1 + src/SpelledOut.jl | 12 ++- src/pt_BR.jl | 140 ++++++++++++++++++++++++++++++++ src/pt_BR/standard_pt_BR.jl | 82 +++++++++++++++++++ test/runtests.jl | 7 +- 7 files changed, 252 insertions(+), 6 deletions(-) create mode 100644 SpelledOut.jl.Rproj create mode 100644 src/pt_BR.jl create mode 100644 src/pt_BR/standard_pt_BR.jl diff --git a/.gitignore b/.gitignore index 88cdeb53..e2316af4 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ Manifest.toml .DS_Store # VS Code folder -.vscode \ No newline at end of file +.vscode +.Rproj.user diff --git a/SpelledOut.jl.Rproj b/SpelledOut.jl.Rproj new file mode 100644 index 00000000..8e3c2ebc --- /dev/null +++ b/SpelledOut.jl.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/docs/src/supported_languages.md b/docs/src/supported_languages.md index d7a7bf4a..f931c0f5 100644 --- a/docs/src/supported_languages.md +++ b/docs/src/supported_languages.md @@ -7,3 +7,4 @@ Current languages to choose from are: - `:en_UK` (aliases: `:en_GB`; equivalencies: `:en_NZ`, `:en_AU`) - Dictionaries supported include `:modern`, `:british`, and `:european`, and default to the former. - Spanish (`:es`) + - Portuguese (`:pt_BR`) diff --git a/src/SpelledOut.jl b/src/SpelledOut.jl index 8fce9543..2f7fdc14 100644 --- a/src/SpelledOut.jl +++ b/src/SpelledOut.jl @@ -1,12 +1,13 @@ module SpelledOut -using DecFP: Dec64 +using DecFP: Dec64, Dec128 using Formatting: format export spelled_out, Spelled_out, Spelled_Out, SPELLED_OUT include("en.jl") include("es.jl") +include("pt_BR.jl") """ ```julia @@ -38,6 +39,9 @@ julia> spelled_out(112, lang = :en, dict = :european) julia> spelled_out(112, lang = :es) "ciento doce" + +julia> spelled_out(112, lang = :pt_BR) +"cento e doze" ``` """ function spelled_out( @@ -51,7 +55,9 @@ function spelled_out( elseif lang ∈ (:en_UK, :en_GB, :en_NZ, :en_AU) return spelled_out_en(number, british = true, dict = dict) elseif lang ∈ (:es,) - return spelled_out_es(number,dict) + return spelled_out_es(number, dict) + elseif lang ∈ (:pt_BR,) + return spelled_out_pt_BR(number, dict) end throw(error("We do not support $lang yet. Please make an issue and someone might be able to help you, or feel free to contribute.")) @@ -59,4 +65,4 @@ function spelled_out( return nothing end -end # end module +end # end module \ No newline at end of file diff --git a/src/pt_BR.jl b/src/pt_BR.jl new file mode 100644 index 00000000..d743f560 --- /dev/null +++ b/src/pt_BR.jl @@ -0,0 +1,140 @@ +include( joinpath( @__DIR__, "pt_BR", "standard_pt_BR.jl" ) ) + +#retuns a vector of ints, each of that +function split_numbers_10³( num::Integer ) + digits( num, base = 10^3) +end + +function split_numbers_10⁶( num::Integer ) + digits( num, base = 10^6) +end + +#function +function pt_BR_spell_1e3( number, short_one = false ) + if number <= 99 #0 - 99 + if number < 20 #1 - 20 + iszero(number) && return "" + if isone(number) && short_one + return "um" + else + return pt_BR_1_a_19[ number ] + end + else #20-99 + unit, dec = digits( number ) + if isone( unit ) && short_one + unit_text = " e um" + elseif iszero( unit ) + unit_text = "" + else + unit_text = " e " * pt_BR_1_a_19[ unit ] + end + return pt_BR_dezenas[ dec ] * unit_text + end + elseif 100 <= number <= 999 + number == 100 && return "cem" + unit, cent = digits( number, base = 100 ) + unit_text = pt_BR_spell_1e3( unit, short_one ) + return pt_BR_centenas[ cent ] * " e " * unit_text + end +end + +function pt_BR_spell_1e6( number, short_one = false ) + number = Int( number ) + number == 0 && return "" + number == 1000 && return "mil" + number <= 999 && return pt_BR_spell_1e3( number, short_one ) + low,high = digits( number, base = 1000 ) + low_txt = pt_BR_spell_1e3( low, short_one ) + if high == 1 + high_text = "mil e " + else + high_text = pt_BR_spell_1e3( high, false ) * " mil" + end + return high_text * low_txt +end + +#spells only decimals, including 0 +function pt_BR_spell_decimal( number, partitive = true ) + whole, decimal = split( string( number ), "." ) + decnum, wholenum = abs.( modf( number ) ) + decimal_length = length( decimal ) + decint = decnum*10^decimal_length + if partitive + if decimal_length > 6 + throw(error("""partitive pronunciation of Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . + If you want english-like decimals (zero ponto dois), try using partitive = false. + If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. + """)) + end + + dectext = pt_BR_spell_1e6( Dec128( decint ), false ) + res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] + if decint != 1 + res = res * "s" + end + return res + else + res = digits( decint ) .|> z-> pt_BR_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) + res = "ponto " * res + end +end + +function pt_BR_spell_large_map( number, i ) + if isone( i ) + return pt_BR_spell_1e6( number, true ) + elseif isone( number ) + return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] + else + return pt_BR_spell_1e6( number, false ) * " " * pt_BR_multiplos_1e6_plural[ i - 1 ] + end +end + +function pt_BR_spell_large( _number ) + number = Int( abs( _number ) ) + list = digits( number, base = 1_000_000 ) + res = pt_BR_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) + ( _number < 0 ) && ( res = "menos " * res ) + return res +end + +function spelled_out_pt_BR( number, dict = :standard ) + if iszero( number ) + return "zero" + end + if isinteger( number ) + if dict in ( :standard, :large, :modern ) + res = pt_BR_spell_large( number ) + elseif dict in (:short,) + res = pt_BR_spell_short( number ) + else + throw( error( "unrecognized dict value: $dict" ) ) + end + return res + else #with decimals + partitive = true + _dec, _int = modf( number ) + _int = Dec128( _int ) + if ( _int == 0 ) & !partitive + intres = "zero" + + elseif ( _int == 0 ) & partitive + intres = "" + elseif dict in ( :standard, :large, :modern ) + intres = pt_BR_spell_large( _int ) * " con " + elseif dict in ( :short, ) + intres = pt_BR_spell_short( _int ) * " con " + else + throw( error( "unrecognized dict value: $dict" ) ) + end + + decres = pt_BR_spell_decimal( abs( _dec ) ) + return intres * decres + end +end + + + + + + + diff --git a/src/pt_BR/standard_pt_BR.jl b/src/pt_BR/standard_pt_BR.jl new file mode 100644 index 00000000..80aeb799 --- /dev/null +++ b/src/pt_BR/standard_pt_BR.jl @@ -0,0 +1,82 @@ +const pt_BR_1_a_19 =[ + "um" + "dois" + "três" + "quatro" + "cinco" + "seis" + "sete" + "oito" + "nove" + "dez" + "onze" + "doze" + "treze" + "catorze" + "quinze" + "dezesseis" + "dezessete" + "dezoito" + "dezenove" +] + +const pt_BR_dezenas = [ + "" + "vinte" + "trinta" + "quarenta" + "cinquenta" + "sessenta" + "setenta" + "oitenta" + "noventa" +] + +const pt_BR_centenas = [ + "cento" + "duzentos" + "trezentos" + "quatrocentos" + "quinhentos" + "seiscentos" + "setecentos" + "oitocentos" + "novecentos" +] + +const pt_BR_multiplos_1e6_singular = [ + "milhão" + "bilhão" + "trilhão" + "quatrilhão" + "quintilhão" + "sextilhão" + "septilhão" + "oitolhão" + "nonilhão" + "decilhão" + "undecilhão" + "duodecilhão" +] + +const pt_BR_multiplos_1e6_plural = [ + "milhões" + "bilhões" + "trilhões" + "quatrilhões" + "quintilhões" + "sextilhões" + "septilhões" + "oitolhões" + "nonilhões" + "decilhões" + "undecilhões" + "duodecilhões" +] + +const pt_BR_decimais = [ + "décimo" + "centésimo" + "milésimo" + "milionésimo" +] \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 666282f8..05a1a1bc 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -34,11 +34,14 @@ using SpelledOut @test spelled_out(40//6, lang = :en_UK) == "twenty thirds" end - - @testset "Spanish" begin @test spelled_out(585000, lang = :es) == "quinientos ochenta y cinco mil" end +@testset "Portuguese" begin + @test spelled_out(585000, lang = :pt_BR) == "quinhentos e oitenta e cinco mil" + +end + nothing From 7c113f143e56699e2c08d187fa8ae9d455110239 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 30 May 2022 16:01:01 -0300 Subject: [PATCH 02/14] "deleting useless file" --- SpelledOut.jl.Rproj | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 SpelledOut.jl.Rproj diff --git a/SpelledOut.jl.Rproj b/SpelledOut.jl.Rproj deleted file mode 100644 index 8e3c2ebc..00000000 --- a/SpelledOut.jl.Rproj +++ /dev/null @@ -1,13 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX From 6cf1a1ffa3d06bd83fe96461bdde072caeebbe1d Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 30 May 2022 16:02:18 -0300 Subject: [PATCH 03/14] .gitignore --- .Rproj.user/49806DE7/sources/s-A9A11D40/lock_file | 0 .Rproj.user/shared/notebooks/patch-chunk-names | 0 .gitignore | 3 +-- 3 files changed, 1 insertion(+), 2 deletions(-) create mode 100644 .Rproj.user/49806DE7/sources/s-A9A11D40/lock_file create mode 100644 .Rproj.user/shared/notebooks/patch-chunk-names diff --git a/.Rproj.user/49806DE7/sources/s-A9A11D40/lock_file b/.Rproj.user/49806DE7/sources/s-A9A11D40/lock_file new file mode 100644 index 00000000..e69de29b diff --git a/.Rproj.user/shared/notebooks/patch-chunk-names b/.Rproj.user/shared/notebooks/patch-chunk-names new file mode 100644 index 00000000..e69de29b diff --git a/.gitignore b/.gitignore index e2316af4..88cdeb53 100644 --- a/.gitignore +++ b/.gitignore @@ -27,5 +27,4 @@ Manifest.toml .DS_Store # VS Code folder -.vscode -.Rproj.user +.vscode \ No newline at end of file From fd2886d4bf08192094e7ff36abb3935a60d6978c Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Wed, 1 Jun 2022 15:34:31 -0300 Subject: [PATCH 04/14] removed files from Rstudio IDE --- .Rproj.user/49806DE7/sources/s-A9A11D40/lock_file | 0 .Rproj.user/shared/notebooks/patch-chunk-names | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .Rproj.user/49806DE7/sources/s-A9A11D40/lock_file delete mode 100644 .Rproj.user/shared/notebooks/patch-chunk-names diff --git a/.Rproj.user/49806DE7/sources/s-A9A11D40/lock_file b/.Rproj.user/49806DE7/sources/s-A9A11D40/lock_file deleted file mode 100644 index e69de29b..00000000 diff --git a/.Rproj.user/shared/notebooks/patch-chunk-names b/.Rproj.user/shared/notebooks/patch-chunk-names deleted file mode 100644 index e69de29b..00000000 From 4ad30eb8fe71056798e6f01e066fdcc90a56239a Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Thu, 2 Jun 2022 22:33:09 +1200 Subject: [PATCH 05/14] Added variants subsection to support future addition of Portugal Pt --- docs/src/supported_languages.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/src/supported_languages.md b/docs/src/supported_languages.md index f931c0f5..04210599 100644 --- a/docs/src/supported_languages.md +++ b/docs/src/supported_languages.md @@ -7,4 +7,6 @@ Current languages to choose from are: - `:en_UK` (aliases: `:en_GB`; equivalencies: `:en_NZ`, `:en_AU`) - Dictionaries supported include `:modern`, `:british`, and `:european`, and default to the former. - Spanish (`:es`) - - Portuguese (`:pt_BR`) + - Portuguese + - Variants + - (`:pt_BR`) From 29af6ecaf4067b399926b30bee1a9d203417f657 Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Thu, 2 Jun 2022 22:45:10 +1200 Subject: [PATCH 06/14] Refactor pt_BR code structure to contain all Portuguese variants within one file --- src/SpelledOut.jl | 6 +-- src/{pt_BR.jl => pt.jl} | 40 ++++++++++--------- .../standard_pt_br.jl} | 0 test/runtests.jl | 2 - 4 files changed, 24 insertions(+), 24 deletions(-) rename src/{pt_BR.jl => pt.jl} (75%) rename src/{pt_BR/standard_pt_BR.jl => pt/standard_pt_br.jl} (100%) diff --git a/src/SpelledOut.jl b/src/SpelledOut.jl index 2f7fdc14..df815f14 100644 --- a/src/SpelledOut.jl +++ b/src/SpelledOut.jl @@ -7,7 +7,7 @@ export spelled_out, Spelled_out, Spelled_Out, SPELLED_OUT include("en.jl") include("es.jl") -include("pt_BR.jl") +include("pt.jl") """ ```julia @@ -57,7 +57,7 @@ function spelled_out( elseif lang ∈ (:es,) return spelled_out_es(number, dict) elseif lang ∈ (:pt_BR,) - return spelled_out_pt_BR(number, dict) + return spelled_out_pt_br(number, dict) end throw(error("We do not support $lang yet. Please make an issue and someone might be able to help you, or feel free to contribute.")) @@ -65,4 +65,4 @@ function spelled_out( return nothing end -end # end module \ No newline at end of file +end # end module diff --git a/src/pt_BR.jl b/src/pt.jl similarity index 75% rename from src/pt_BR.jl rename to src/pt.jl index d743f560..5aa9a6ce 100644 --- a/src/pt_BR.jl +++ b/src/pt.jl @@ -1,4 +1,6 @@ -include( joinpath( @__DIR__, "pt_BR", "standard_pt_BR.jl" ) ) +include(joinpath(@__DIR__, "pt", "standard_pt_br.jl")) + +## Implement Spelled Out for Brazilian Portuguese #retuns a vector of ints, each of that function split_numbers_10³( num::Integer ) @@ -10,7 +12,7 @@ function split_numbers_10⁶( num::Integer ) end #function -function pt_BR_spell_1e3( number, short_one = false ) +function pt_br_spell_1e3( number, short_one = false ) if number <= 99 #0 - 99 if number < 20 #1 - 20 iszero(number) && return "" @@ -33,28 +35,28 @@ function pt_BR_spell_1e3( number, short_one = false ) elseif 100 <= number <= 999 number == 100 && return "cem" unit, cent = digits( number, base = 100 ) - unit_text = pt_BR_spell_1e3( unit, short_one ) + unit_text = pt_br_spell_1e3( unit, short_one ) return pt_BR_centenas[ cent ] * " e " * unit_text end end -function pt_BR_spell_1e6( number, short_one = false ) +function pt_br_spell_1e6( number, short_one = false ) number = Int( number ) number == 0 && return "" number == 1000 && return "mil" - number <= 999 && return pt_BR_spell_1e3( number, short_one ) + number <= 999 && return pt_br_spell_1e3( number, short_one ) low,high = digits( number, base = 1000 ) - low_txt = pt_BR_spell_1e3( low, short_one ) + low_txt = pt_br_spell_1e3( low, short_one ) if high == 1 high_text = "mil e " else - high_text = pt_BR_spell_1e3( high, false ) * " mil" + high_text = pt_br_spell_1e3( high, false ) * " mil" end return high_text * low_txt end #spells only decimals, including 0 -function pt_BR_spell_decimal( number, partitive = true ) +function pt_br_spell_decimal( number, partitive = true ) whole, decimal = split( string( number ), "." ) decnum, wholenum = abs.( modf( number ) ) decimal_length = length( decimal ) @@ -67,43 +69,43 @@ function pt_BR_spell_decimal( number, partitive = true ) """)) end - dectext = pt_BR_spell_1e6( Dec128( decint ), false ) + dectext = pt_br_spell_1e6( Dec128( decint ), false ) res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] if decint != 1 res = res * "s" end return res else - res = digits( decint ) .|> z-> pt_BR_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) + res = digits( decint ) .|> z-> pt_br_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) res = "ponto " * res end end -function pt_BR_spell_large_map( number, i ) +function pt_br_spell_large_map( number, i ) if isone( i ) - return pt_BR_spell_1e6( number, true ) + return pt_br_spell_1e6( number, true ) elseif isone( number ) return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] else - return pt_BR_spell_1e6( number, false ) * " " * pt_BR_multiplos_1e6_plural[ i - 1 ] + return pt_br_spell_1e6( number, false ) * " " * pt_BR_multiplos_1e6_plural[ i - 1 ] end end -function pt_BR_spell_large( _number ) +function pt_br_spell_large( _number ) number = Int( abs( _number ) ) list = digits( number, base = 1_000_000 ) - res = pt_BR_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) + res = pt_br_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) ( _number < 0 ) && ( res = "menos " * res ) return res end -function spelled_out_pt_BR( number, dict = :standard ) +function spelled_out_pt_br( number, dict = :standard ) if iszero( number ) return "zero" end if isinteger( number ) if dict in ( :standard, :large, :modern ) - res = pt_BR_spell_large( number ) + res = pt_br_spell_large( number ) elseif dict in (:short,) res = pt_BR_spell_short( number ) else @@ -120,14 +122,14 @@ function spelled_out_pt_BR( number, dict = :standard ) elseif ( _int == 0 ) & partitive intres = "" elseif dict in ( :standard, :large, :modern ) - intres = pt_BR_spell_large( _int ) * " con " + intres = pt_br_spell_large( _int ) * " con " elseif dict in ( :short, ) intres = pt_BR_spell_short( _int ) * " con " else throw( error( "unrecognized dict value: $dict" ) ) end - decres = pt_BR_spell_decimal( abs( _dec ) ) + decres = pt_br_spell_decimal( abs( _dec ) ) return intres * decres end end diff --git a/src/pt_BR/standard_pt_BR.jl b/src/pt/standard_pt_br.jl similarity index 100% rename from src/pt_BR/standard_pt_BR.jl rename to src/pt/standard_pt_br.jl diff --git a/test/runtests.jl b/test/runtests.jl index 05a1a1bc..8940d4e4 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -36,12 +36,10 @@ end @testset "Spanish" begin @test spelled_out(585000, lang = :es) == "quinientos ochenta y cinco mil" - end @testset "Portuguese" begin @test spelled_out(585000, lang = :pt_BR) == "quinhentos e oitenta e cinco mil" - end nothing From 2f442cf72dbfa50f7d13faeacd45eeb55d65e748 Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Thu, 2 Jun 2022 22:46:08 +1200 Subject: [PATCH 07/14] Removed unused functions --- src/es.jl | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/es.jl b/src/es.jl index bad01488..e0af3b5c 100644 --- a/src/es.jl +++ b/src/es.jl @@ -1,15 +1,5 @@ include(joinpath(@__DIR__, "es", "standard_es.jl")) -#retuns a vector of ints, each of that -function split_numbers_10³(num::Integer) - digits(num,base=10^3) -end - -function split_numbers_10⁶(num::Integer) - digits(num,base=10^6) -end - -#function function es_spell_1e3(number,short_one=false) if number <= 99 #0 - 99 if number<30 #1 - 30 From 1e48e1aeeff60bcdcfbae7ae717eb5c3381626d3 Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Thu, 2 Jun 2022 22:49:30 +1200 Subject: [PATCH 08/14] Changed dict to keyword argument for consistency in interface --- src/SpelledOut.jl | 4 ++-- src/es.jl | 2 +- src/pt.jl | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SpelledOut.jl b/src/SpelledOut.jl index df815f14..9b8454aa 100644 --- a/src/SpelledOut.jl +++ b/src/SpelledOut.jl @@ -55,9 +55,9 @@ function spelled_out( elseif lang ∈ (:en_UK, :en_GB, :en_NZ, :en_AU) return spelled_out_en(number, british = true, dict = dict) elseif lang ∈ (:es,) - return spelled_out_es(number, dict) + return spelled_out_es(number; dict = dict) elseif lang ∈ (:pt_BR,) - return spelled_out_pt_br(number, dict) + return spelled_out_pt_br(number; dict = dict) end throw(error("We do not support $lang yet. Please make an issue and someone might be able to help you, or feel free to contribute.")) diff --git a/src/es.jl b/src/es.jl index e0af3b5c..d87bc358 100644 --- a/src/es.jl +++ b/src/es.jl @@ -92,7 +92,7 @@ function es_spell_large(_number) end -function spelled_out_es(number,dict=:standard) +function spelled_out_es(number; dict=:standard) if iszero(number) return "cero" end diff --git a/src/pt.jl b/src/pt.jl index 5aa9a6ce..225627cb 100644 --- a/src/pt.jl +++ b/src/pt.jl @@ -99,7 +99,7 @@ function pt_br_spell_large( _number ) return res end -function spelled_out_pt_br( number, dict = :standard ) +function spelled_out_pt_br( number; dict = :standard ) if iszero( number ) return "zero" end From 987c4f38a649044269f3e18788f3eac7bdfc95e0 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Thu, 2 Jun 2022 13:58:58 -0300 Subject: [PATCH 09/14] pt_BR tests --- test/runtests.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 8940d4e4..4d31816e 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -40,6 +40,15 @@ end @testset "Portuguese" begin @test spelled_out(585000, lang = :pt_BR) == "quinhentos e oitenta e cinco mil" + @test spelled_out(0, lang = :pt_BR) == "zero" + @test spelled_out(0.9, lang = :pt_BR) == "nove décimos" + @test spelled_out(0.09, lang = :pt_BR) == "nove centésimos" + @test spelled_out(0.009, lang = :pt_BR) == "nove milésimos" + @test spelled_out(0.0009, lang = :pt_BR) == "nove milionésimos" + @test spelled_out(-10, lang = :pt_BR) == "menos dez" + @test spelled_out(-109, lang = :pt_BR) == "menos cento e nove" + @test spelled_out(100, lang = :pt_BR) == "cem" + @test spelled_out(105, lang = :pt_BR) == "cento e cinco" end nothing From d977fee414feba54dc48cb1dcd24c8a66997aa28 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Thu, 2 Jun 2022 19:03:26 -0300 Subject: [PATCH 10/14] fixing a bug and implementing pt-PT separately --- src/SpelledOut.jl | 10 ++- src/pt.jl | 71 ++++++++++---------- src/pt/pt_pt.jl | 82 +++++++++++++++++++++++ src/pt/standard_pt_br.jl | 6 +- src/pt_BR.jl | 139 +++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 10 +++ 6 files changed, 280 insertions(+), 38 deletions(-) create mode 100644 src/pt/pt_pt.jl create mode 100644 src/pt_BR.jl diff --git a/src/SpelledOut.jl b/src/SpelledOut.jl index 9b8454aa..b2227a31 100644 --- a/src/SpelledOut.jl +++ b/src/SpelledOut.jl @@ -8,6 +8,7 @@ export spelled_out, Spelled_out, Spelled_Out, SPELLED_OUT include("en.jl") include("es.jl") include("pt.jl") +include("pt_BR.jl") """ ```julia @@ -40,8 +41,11 @@ julia> spelled_out(112, lang = :en, dict = :european) julia> spelled_out(112, lang = :es) "ciento doce" -julia> spelled_out(112, lang = :pt_BR) -"cento e doze" +julia> spelled_out(19, lang = :pt_BR) +"dezenove" + +julia> spelled_out(19, lang = :pt) +"dezanove" ``` """ function spelled_out( @@ -58,6 +62,8 @@ function spelled_out( return spelled_out_es(number; dict = dict) elseif lang ∈ (:pt_BR,) return spelled_out_pt_br(number; dict = dict) + elseif lang ∈ (:pt,) + return spelled_out_pt_pt(number; dict = dict) end throw(error("We do not support $lang yet. Please make an issue and someone might be able to help you, or feel free to contribute.")) diff --git a/src/pt.jl b/src/pt.jl index 225627cb..02654f38 100644 --- a/src/pt.jl +++ b/src/pt.jl @@ -1,6 +1,6 @@ -include(joinpath(@__DIR__, "pt", "standard_pt_br.jl")) +include(joinpath(@__DIR__, "pt", "pt_pt.jl")) -## Implement Spelled Out for Brazilian Portuguese +## Implement Spelled Out for Portugal Portuguese #retuns a vector of ints, each of that function split_numbers_10³( num::Integer ) @@ -12,14 +12,14 @@ function split_numbers_10⁶( num::Integer ) end #function -function pt_br_spell_1e3( number, short_one = false ) +function pt_pt_spell_1e3( number, short_one = false ) if number <= 99 #0 - 99 if number < 20 #1 - 20 iszero(number) && return "" if isone(number) && short_one return "um" else - return pt_BR_1_a_19[ number ] + return pt_PT_1_a_19[ number ] end else #20-99 unit, dec = digits( number ) @@ -28,86 +28,90 @@ function pt_br_spell_1e3( number, short_one = false ) elseif iszero( unit ) unit_text = "" else - unit_text = " e " * pt_BR_1_a_19[ unit ] + unit_text = " e " * pt_PT_1_a_19[ unit ] end - return pt_BR_dezenas[ dec ] * unit_text + return pt_PT_dezenas[ dec ] * unit_text end - elseif 100 <= number <= 999 - number == 100 && return "cem" + elseif number ∈ centenas_dicionario unit, cent = digits( number, base = 100 ) - unit_text = pt_br_spell_1e3( unit, short_one ) - return pt_BR_centenas[ cent ] * " e " * unit_text + unit_text = pt_pt_spell_1e3( unit, short_one ) + return pt_PT_centenas[ cent ] + elseif 100 <= number <= 999 + number == 100 && return "cem" + unit, cent = digits( number, base = 100 ) + unit_text = pt_pt_spell_1e3( unit, short_one ) + return pt_PT_centenas[ cent ] * " e " * unit_text end end -function pt_br_spell_1e6( number, short_one = false ) +function pt_pt_spell_1e6( number, short_one = false ) number = Int( number ) number == 0 && return "" number == 1000 && return "mil" - number <= 999 && return pt_br_spell_1e3( number, short_one ) + number <= 999 && return pt_pt_spell_1e3( number, short_one ) low,high = digits( number, base = 1000 ) - low_txt = pt_br_spell_1e3( low, short_one ) + low_txt = pt_pt_spell_1e3( low, short_one ) if high == 1 high_text = "mil e " else - high_text = pt_br_spell_1e3( high, false ) * " mil" + high_text = pt_pt_spell_1e3( high, false ) * " mil" end return high_text * low_txt end #spells only decimals, including 0 -function pt_br_spell_decimal( number, partitive = true ) +function pt_pt_spell_decimal( number, partitive = true ) whole, decimal = split( string( number ), "." ) decnum, wholenum = abs.( modf( number ) ) decimal_length = length( decimal ) decint = decnum*10^decimal_length if partitive if decimal_length > 6 - throw(error("""partitive pronunciation of Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . + throw(error("""partitive pronunciation of Portugal Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . If you want english-like decimals (zero ponto dois), try using partitive = false. If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. """)) end - dectext = pt_br_spell_1e6( Dec128( decint ), false ) - res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] + dectext = pt_pt_spell_1e6( Dec128( decint ), false ) + res = strip( dectext ) * " " * pt_PT_decimais[ decimal_length ] if decint != 1 res = res * "s" end return res else - res = digits( decint ) .|> z-> pt_br_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) + res = digits( decint ) .|> z-> pt_pt_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) res = "ponto " * res end end -function pt_br_spell_large_map( number, i ) +function pt_pt_spell_large_map( number, i ) if isone( i ) - return pt_br_spell_1e6( number, true ) + return pt_pt_spell_1e6( number, true ) elseif isone( number ) - return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] + return "um " * pt_PT_multiplos_1e6_singular[ i - 1 ] else - return pt_br_spell_1e6( number, false ) * " " * pt_BR_multiplos_1e6_plural[ i - 1 ] + return pt_pt_spell_1e6( number, false ) end end -function pt_br_spell_large( _number ) - number = Int( abs( _number ) ) +function pt_pt_spell_large( _number ) + number = abs( _number ) list = digits( number, base = 1_000_000 ) - res = pt_br_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) + res = pt_pt_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) ( _number < 0 ) && ( res = "menos " * res ) return res end -function spelled_out_pt_br( number; dict = :standard ) +function spelled_out_pt_pt( number; dict = :standard ) if iszero( number ) return "zero" end if isinteger( number ) if dict in ( :standard, :large, :modern ) - res = pt_br_spell_large( number ) + res = pt_pt_spell_large( number ) elseif dict in (:short,) - res = pt_BR_spell_short( number ) + res = pt_pt_spell_short( number ) else throw( error( "unrecognized dict value: $dict" ) ) end @@ -122,14 +126,14 @@ function spelled_out_pt_br( number; dict = :standard ) elseif ( _int == 0 ) & partitive intres = "" elseif dict in ( :standard, :large, :modern ) - intres = pt_br_spell_large( _int ) * " con " + intres = pt_pt_spell_large( _int ) * " con " elseif dict in ( :short, ) - intres = pt_BR_spell_short( _int ) * " con " + intres = pt_PT_spell_short( _int ) * " con " else throw( error( "unrecognized dict value: $dict" ) ) end - decres = pt_br_spell_decimal( abs( _dec ) ) + decres = pt_pt_spell_decimal( abs( _dec ) ) return intres * decres end end @@ -137,6 +141,3 @@ end - - - diff --git a/src/pt/pt_pt.jl b/src/pt/pt_pt.jl new file mode 100644 index 00000000..816514b3 --- /dev/null +++ b/src/pt/pt_pt.jl @@ -0,0 +1,82 @@ +const pt_PT_1_a_19 =[ + "um" + "dois" + "três" + "quatro" + "cinco" + "seis" + "sete" + "oito" + "nove" + "dez" + "onze" + "doze" + "treze" + "catorze" + "quinze" + "dezasseis" + "dezassete" + "dezoito" + "dezanove" +] + +const pt_PT_dezenas = [ + "" + "vinte" + "trinta" + "quarenta" + "cinquenta" + "sessenta" + "setenta" + "oitenta" + "noventa" +] + +const pt_PT_centenas = [ + "cento" + "duzentos" + "trezentos" + "quatrocentos" + "quinhentos" + "seiscentos" + "setecentos" + "oitocentos" + "novecentos" +] + +const pt_PT_multiplos_1e6_singular = [ + "milhão" + "bilião" + "trilião" + "quatrilião" + "quintilião" + "sextilião" + "septilião" + "oitolião" + "nonilião" + "decilião" + "undecilião" + "duodecilião" +] + +const pt_PT_multiplos_1e6_plural = [ + "milhões" + "biliões" + "triliões" + "quatriliões" + "quintiliões" + "sextiliões" + "septiliões" + "oitoliões" + "noniliões" + "deciliões" + "undeciliões" + "duodeciliões" +] + +const pt_PT_decimais = [ + "décimo" + "centésimo" + "milésimo" + "milionésimo" +] \ No newline at end of file diff --git a/src/pt/standard_pt_br.jl b/src/pt/standard_pt_br.jl index 80aeb799..22f6441e 100644 --- a/src/pt/standard_pt_br.jl +++ b/src/pt/standard_pt_br.jl @@ -12,7 +12,7 @@ const pt_BR_1_a_19 =[ "onze" "doze" "treze" - "catorze" + "quatorze" "quinze" "dezesseis" "dezessete" @@ -44,6 +44,10 @@ const pt_BR_centenas = [ "novecentos" ] +const centenas_dicionario = Number[ + 200, 300, 400, 500, 600, 700, 800, 900 +] + const pt_BR_multiplos_1e6_singular = [ "milhão" "bilhão" diff --git a/src/pt_BR.jl b/src/pt_BR.jl new file mode 100644 index 00000000..c4e7dedb --- /dev/null +++ b/src/pt_BR.jl @@ -0,0 +1,139 @@ +include(joinpath(@__DIR__, "pt", "standard_pt_br.jl")) + +## Implement Spelled Out for Brazilian Portuguese + +#retuns a vector of ints, each of that +function split_numbers_10³( num::Integer ) + digits( num, base = 10^3) +end + +function split_numbers_10⁶( num::Integer ) + digits( num, base = 10^6) +end + +#function +function pt_br_spell_1e3( number, short_one = false ) + if number <= 99 #0 - 99 + if number < 20 #1 - 20 + iszero(number) && return "" + if isone(number) && short_one + return "um" + else + return pt_BR_1_a_19[ number ] + end + else #20-99 + unit, dec = digits( number ) + if isone( unit ) && short_one + unit_text = " e um" + elseif iszero( unit ) + unit_text = "" + else + unit_text = " e " * pt_BR_1_a_19[ unit ] + end + return pt_BR_dezenas[ dec ] * unit_text + end + elseif number ∈ centenas_dicionario + unit, cent = digits( number, base = 100 ) + unit_text = pt_br_spell_1e3( unit, short_one ) + return pt_BR_centenas[ cent ] + elseif 100 <= number <= 999 + number == 100 && return "cem" + unit, cent = digits( number, base = 100 ) + unit_text = pt_br_spell_1e3( unit, short_one ) + return pt_BR_centenas[ cent ] * " e " * unit_text + end +end + +function pt_br_spell_1e6( number, short_one = false ) + number = Int( number ) + number == 0 && return "" + number == 1000 && return "mil" + number <= 999 && return pt_br_spell_1e3( number, short_one ) + low,high = digits( number, base = 1000 ) + low_txt = pt_br_spell_1e3( low, short_one ) + if high == 1 + high_text = "mil e " + else + high_text = pt_br_spell_1e3( high, false ) * " mil" + end + return high_text * low_txt +end + +#spells only decimals, including 0 +function pt_br_spell_decimal( number, partitive = true ) + whole, decimal = split( string( number ), "." ) + decnum, wholenum = abs.( modf( number ) ) + decimal_length = length( decimal ) + decint = decnum*10^decimal_length + if partitive + if decimal_length > 6 + throw(error("""partitive pronunciation of Brazilian Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . + If you want english-like decimals (zero ponto dois), try using partitive = false. + If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. + """)) + end + + dectext = pt_br_spell_1e6( Dec128( decint ), false ) + res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] + if decint != 1 + res = res * "s" + end + return res + else + res = digits( decint ) .|> z-> pt_br_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) + res = "ponto " * res + end +end + +function pt_br_spell_large_map( number, i ) + if isone( i ) + return pt_br_spell_1e6( number, true ) + elseif isone( number ) + return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] + else + return pt_br_spell_1e6( number, false ) + end +end + +function pt_br_spell_large( _number ) + number = abs( _number ) + list = digits( number, base = 1_000_000 ) + res = pt_br_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) + ( _number < 0 ) && ( res = "menos " * res ) + return res +end + +function spelled_out_pt_br( number; dict = :standard ) + if iszero( number ) + return "zero" + end + if isinteger( number ) + if dict in ( :standard, :large, :modern ) + res = pt_br_spell_large( number ) + elseif dict in (:short,) + res = pt_BR_spell_short( number ) + else + throw( error( "unrecognized dict value: $dict" ) ) + end + return res + else #with decimals + partitive = true + _dec, _int = modf( number ) + _int = Dec128( _int ) + if ( _int == 0 ) & !partitive + intres = "zero" + + elseif ( _int == 0 ) & partitive + intres = "" + elseif dict in ( :standard, :large, :modern ) + intres = pt_br_spell_large( _int ) * " con " + elseif dict in ( :short, ) + intres = pt_BR_spell_short( _int ) * " con " + else + throw( error( "unrecognized dict value: $dict" ) ) + end + + decres = pt_br_spell_decimal( abs( _dec ) ) + return intres * decres + end +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 4d31816e..329e9bc9 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -49,6 +49,16 @@ end @test spelled_out(-109, lang = :pt_BR) == "menos cento e nove" @test spelled_out(100, lang = :pt_BR) == "cem" @test spelled_out(105, lang = :pt_BR) == "cento e cinco" + @test spelled_out(19, lang = :pt_BR) == "dezenove" + @test spelled_out(14, lang = :pt_BR) == "quatorze" + @test spelled_out(585000, lang = :pt) == "quinhentos e oitenta e cinco mil" + @test spelled_out(200, lang = :pt) == "duzentos" + @test spelled_out(19, lang = :pt) == "dezanove" + @test spelled_out(0.9, lang = :pt) == "nove décimos" + @test spelled_out(14, lang = :pt) == "catorze" + @test spelled_out(-10, lang = :pt) == "menos dez" + @test spelled_out(-109, lang = :pt) == "menos cento e nove" + @test spelled_out(105, lang = :pt) == "cento e cinco" end nothing From 5f77f9c06c51b6dd6f021321abd636413ddca51a Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 6 Jun 2022 20:33:07 -0300 Subject: [PATCH 11/14] implementing pt-PT and pt-BR together --- src/SpelledOut.jl | 5 +- src/pt.jl | 91 +++++++++++++++++------------- src/pt_BR.jl | 139 ---------------------------------------------- 3 files changed, 55 insertions(+), 180 deletions(-) delete mode 100644 src/pt_BR.jl diff --git a/src/SpelledOut.jl b/src/SpelledOut.jl index b2227a31..1244e0f7 100644 --- a/src/SpelledOut.jl +++ b/src/SpelledOut.jl @@ -8,7 +8,6 @@ export spelled_out, Spelled_out, Spelled_Out, SPELLED_OUT include("en.jl") include("es.jl") include("pt.jl") -include("pt_BR.jl") """ ```julia @@ -61,9 +60,9 @@ function spelled_out( elseif lang ∈ (:es,) return spelled_out_es(number; dict = dict) elseif lang ∈ (:pt_BR,) - return spelled_out_pt_br(number; dict = dict) + return spelled_out_pt(number; portugal = false, dict = dict) elseif lang ∈ (:pt,) - return spelled_out_pt_pt(number; dict = dict) + return spelled_out_pt(number; portugal = true, dict = dict) end throw(error("We do not support $lang yet. Please make an issue and someone might be able to help you, or feel free to contribute.")) diff --git a/src/pt.jl b/src/pt.jl index 02654f38..6d775582 100644 --- a/src/pt.jl +++ b/src/pt.jl @@ -1,6 +1,7 @@ +include(joinpath(@__DIR__, "pt", "standard_pt_br.jl")) include(joinpath(@__DIR__, "pt", "pt_pt.jl")) -## Implement Spelled Out for Portugal Portuguese +## Implement Spelled Out for Brazilian Portuguese and Portugal Portuguese #retuns a vector of ints, each of that function split_numbers_10³( num::Integer ) @@ -11,107 +12,123 @@ function split_numbers_10⁶( num::Integer ) digits( num, base = 10^6) end -#function -function pt_pt_spell_1e3( number, short_one = false ) +#function +function pt_spell_1e3( number; short_one = false, portugal::Bool = false ) if number <= 99 #0 - 99 - if number < 20 #1 - 20 - iszero(number) && return "" - if isone(number) && short_one + if number < 20 #1 - 19 + iszero( number ) && return "" + if isone( number ) && short_one return "um" + elseif portugal + return pt_PT_1_a_19[ number ] else - return pt_PT_1_a_19[ number ] - end + return pt_BR_1_a_19[ number ] + end else #20-99 unit, dec = digits( number ) if isone( unit ) && short_one unit_text = " e um" elseif iszero( unit ) unit_text = "" + elseif portugal + unit_text = " e " * pt_PT_1_a_19[ unit ] else - unit_text = " e " * pt_PT_1_a_19[ unit ] + unit_text = " e " * pt_BR_1_a_19[ unit ] end - return pt_PT_dezenas[ dec ] * unit_text + println( pt_BR_dezenas[ dec ] * unit_text ) end elseif number ∈ centenas_dicionario unit, cent = digits( number, base = 100 ) - unit_text = pt_pt_spell_1e3( unit, short_one ) - return pt_PT_centenas[ cent ] - elseif 100 <= number <= 999 + unit_text = pt_spell_1e3( unit; short_one ) + return pt_BR_centenas[ cent ] + elseif portugal == false & 100 <= number <= 999 + number == 100 && return "cem" + unit, cent = digits( number, base = 100 ) + unit_text = pt_spell_1e3( unit; short_one ) + return pt_BR_centenas[ cent ] * " e " * unit_text + elseif portugal & 100 <= number <= 999 number == 100 && return "cem" unit, cent = digits( number, base = 100 ) - unit_text = pt_pt_spell_1e3( unit, short_one ) + unit_text = pt_spell_1e3( unit; short_one, portugal = portugal ) return pt_PT_centenas[ cent ] * " e " * unit_text end end -function pt_pt_spell_1e6( number, short_one = false ) +function pt_spell_1e6( number; short_one = false, portugal::Bool = false ) number = Int( number ) number == 0 && return "" number == 1000 && return "mil" - number <= 999 && return pt_pt_spell_1e3( number, short_one ) + number <= 999 && return pt_spell_1e3( number; short_one = short_one, portugal = portugal ) low,high = digits( number, base = 1000 ) - low_txt = pt_pt_spell_1e3( low, short_one ) + low_txt = pt_spell_1e3( low; short_one = short_one, portugal = portugal ) if high == 1 high_text = "mil e " else - high_text = pt_pt_spell_1e3( high, false ) * " mil" + high_text = pt_spell_1e3( high; short_one = false, portugal = portugal ) * " mil" end return high_text * low_txt end #spells only decimals, including 0 -function pt_pt_spell_decimal( number, partitive = true ) +function pt_spell_decimal( number, partitive = true; portugal::Bool = false ) whole, decimal = split( string( number ), "." ) decnum, wholenum = abs.( modf( number ) ) decimal_length = length( decimal ) decint = decnum*10^decimal_length if partitive if decimal_length > 6 - throw(error("""partitive pronunciation of Portugal Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . + throw(error("""partitive pronunciation of Brazilian Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . If you want english-like decimals (zero ponto dois), try using partitive = false. If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. """)) end - dectext = pt_pt_spell_1e6( Dec128( decint ), false ) - res = strip( dectext ) * " " * pt_PT_decimais[ decimal_length ] + dectext = pt_spell_1e6( Dec128( decint ); short_one = false, portugal = portugal ) + res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] if decint != 1 res = res * "s" end return res else - res = digits( decint ) .|> z-> pt_pt_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) + res = digits( decint ) .|> z-> pt_spell_1e3( z; short_one = true, portugal = portugal ) .|> strip |> reverse |> z-> join( z," " ) res = "ponto " * res end end -function pt_pt_spell_large_map( number, i ) +function pt_spell_large_map( number, i; portugal::Bool = false ) if isone( i ) - return pt_pt_spell_1e6( number, true ) - elseif isone( number ) + return pt_spell_1e6( number; short_one = true, portugal = portugal ) + elseif portugal == false & isone( number ) + return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] + elseif portugal & isone( number ) return "um " * pt_PT_multiplos_1e6_singular[ i - 1 ] else - return pt_pt_spell_1e6( number, false ) + return pt_spell_1e6( number; short_one = false, portugal = portugal ) * " " * pt_PT_multiplos_1e6_plural[ i - 1 ] end end -function pt_pt_spell_large( _number ) +function pt_spell_large( _number; portugal::Bool = false ) number = abs( _number ) list = digits( number, base = 1_000_000 ) - res = pt_pt_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) - ( _number < 0 ) && ( res = "menos " * res ) + if portugal + res = pt_spell_large_map.( list, 1:length( list ), portugal = portugal ) .|> strip |> reverse |> z-> join( z," " ) + ( _number < 0 ) && ( res = "menos " * res ) + else + res = pt_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) + ( _number < 0 ) && ( res = "menos " * res ) + end return res end -function spelled_out_pt_pt( number; dict = :standard ) +function spelled_out_pt( number; portugal::Bool = false, dict::Symbol = :standard ) if iszero( number ) return "zero" end if isinteger( number ) if dict in ( :standard, :large, :modern ) - res = pt_pt_spell_large( number ) + res = pt_spell_large( number; portugal = portugal ) elseif dict in (:short,) - res = pt_pt_spell_short( number ) + res = pt_spell_short( number; portugal = portugal ) else throw( error( "unrecognized dict value: $dict" ) ) end @@ -126,18 +143,16 @@ function spelled_out_pt_pt( number; dict = :standard ) elseif ( _int == 0 ) & partitive intres = "" elseif dict in ( :standard, :large, :modern ) - intres = pt_pt_spell_large( _int ) * " con " + intres = pt_spell_large( _int; portugal = portugal ) * " con " elseif dict in ( :short, ) - intres = pt_PT_spell_short( _int ) * " con " + intres = pt_spell_short( _int; portugal = portugal ) * " con " else throw( error( "unrecognized dict value: $dict" ) ) end - decres = pt_pt_spell_decimal( abs( _dec ) ) + decres = pt_spell_decimal( abs( _dec ); portugal = portugal ) return intres * decres end end - - diff --git a/src/pt_BR.jl b/src/pt_BR.jl deleted file mode 100644 index c4e7dedb..00000000 --- a/src/pt_BR.jl +++ /dev/null @@ -1,139 +0,0 @@ -include(joinpath(@__DIR__, "pt", "standard_pt_br.jl")) - -## Implement Spelled Out for Brazilian Portuguese - -#retuns a vector of ints, each of that -function split_numbers_10³( num::Integer ) - digits( num, base = 10^3) -end - -function split_numbers_10⁶( num::Integer ) - digits( num, base = 10^6) -end - -#function -function pt_br_spell_1e3( number, short_one = false ) - if number <= 99 #0 - 99 - if number < 20 #1 - 20 - iszero(number) && return "" - if isone(number) && short_one - return "um" - else - return pt_BR_1_a_19[ number ] - end - else #20-99 - unit, dec = digits( number ) - if isone( unit ) && short_one - unit_text = " e um" - elseif iszero( unit ) - unit_text = "" - else - unit_text = " e " * pt_BR_1_a_19[ unit ] - end - return pt_BR_dezenas[ dec ] * unit_text - end - elseif number ∈ centenas_dicionario - unit, cent = digits( number, base = 100 ) - unit_text = pt_br_spell_1e3( unit, short_one ) - return pt_BR_centenas[ cent ] - elseif 100 <= number <= 999 - number == 100 && return "cem" - unit, cent = digits( number, base = 100 ) - unit_text = pt_br_spell_1e3( unit, short_one ) - return pt_BR_centenas[ cent ] * " e " * unit_text - end -end - -function pt_br_spell_1e6( number, short_one = false ) - number = Int( number ) - number == 0 && return "" - number == 1000 && return "mil" - number <= 999 && return pt_br_spell_1e3( number, short_one ) - low,high = digits( number, base = 1000 ) - low_txt = pt_br_spell_1e3( low, short_one ) - if high == 1 - high_text = "mil e " - else - high_text = pt_br_spell_1e3( high, false ) * " mil" - end - return high_text * low_txt -end - -#spells only decimals, including 0 -function pt_br_spell_decimal( number, partitive = true ) - whole, decimal = split( string( number ), "." ) - decnum, wholenum = abs.( modf( number ) ) - decimal_length = length( decimal ) - decint = decnum*10^decimal_length - if partitive - if decimal_length > 6 - throw(error("""partitive pronunciation of Brazilian Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . - If you want english-like decimals (zero ponto dois), try using partitive = false. - If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. - """)) - end - - dectext = pt_br_spell_1e6( Dec128( decint ), false ) - res = strip( dectext ) * " " * pt_BR_decimais[ decimal_length ] - if decint != 1 - res = res * "s" - end - return res - else - res = digits( decint ) .|> z-> pt_br_spell_1e3( z, true ) .|> strip |> reverse |> z-> join( z," " ) - res = "ponto " * res - end -end - -function pt_br_spell_large_map( number, i ) - if isone( i ) - return pt_br_spell_1e6( number, true ) - elseif isone( number ) - return "um " * pt_BR_multiplos_1e6_singular[ i - 1 ] - else - return pt_br_spell_1e6( number, false ) - end -end - -function pt_br_spell_large( _number ) - number = abs( _number ) - list = digits( number, base = 1_000_000 ) - res = pt_br_spell_large_map.( list, 1:length( list ) ) .|> strip |> reverse |> z-> join( z," " ) - ( _number < 0 ) && ( res = "menos " * res ) - return res -end - -function spelled_out_pt_br( number; dict = :standard ) - if iszero( number ) - return "zero" - end - if isinteger( number ) - if dict in ( :standard, :large, :modern ) - res = pt_br_spell_large( number ) - elseif dict in (:short,) - res = pt_BR_spell_short( number ) - else - throw( error( "unrecognized dict value: $dict" ) ) - end - return res - else #with decimals - partitive = true - _dec, _int = modf( number ) - _int = Dec128( _int ) - if ( _int == 0 ) & !partitive - intres = "zero" - - elseif ( _int == 0 ) & partitive - intres = "" - elseif dict in ( :standard, :large, :modern ) - intres = pt_br_spell_large( _int ) * " con " - elseif dict in ( :short, ) - intres = pt_BR_spell_short( _int ) * " con " - else - throw( error( "unrecognized dict value: $dict" ) ) - end - - decres = pt_br_spell_decimal( abs( _dec ) ) - return intres * decres - end -end \ No newline at end of file From 1624012753783e2b076363b79ca5fbaccc49a199 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 6 Jun 2022 20:58:50 -0300 Subject: [PATCH 12/14] Included pt on the supported language file --- docs/src/supported_languages.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/supported_languages.md b/docs/src/supported_languages.md index 04210599..b540936f 100644 --- a/docs/src/supported_languages.md +++ b/docs/src/supported_languages.md @@ -10,3 +10,4 @@ Current languages to choose from are: - Portuguese - Variants - (`:pt_BR`) + - (`:pt`) From 4041d8bec15b9d644643ab1cd0b130860402cd98 Mon Sep 17 00:00:00 2001 From: Gabrielforest Date: Mon, 20 Jun 2022 23:12:26 -0300 Subject: [PATCH 13/14] fixing print error --- src/pt.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pt.jl b/src/pt.jl index 6d775582..caba8b0b 100644 --- a/src/pt.jl +++ b/src/pt.jl @@ -33,9 +33,9 @@ function pt_spell_1e3( number; short_one = false, portugal::Bool = false ) elseif portugal unit_text = " e " * pt_PT_1_a_19[ unit ] else - unit_text = " e " * pt_BR_1_a_19[ unit ] + unit_text = " e " * pt_BR_1_a_19[ unit ] end - println( pt_BR_dezenas[ dec ] * unit_text ) + return pt_BR_dezenas[ dec ] * unit_text end elseif number ∈ centenas_dicionario unit, cent = digits( number, base = 100 ) @@ -77,7 +77,7 @@ function pt_spell_decimal( number, partitive = true; portugal::Bool = false ) decint = decnum*10^decimal_length if partitive if decimal_length > 6 - throw(error("""partitive pronunciation of Brazilian Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . + throw(error("""partitive pronunciation of Portuguese decimals is only supported to up to six decimals, the decimal part is $decimal . If you want english-like decimals (zero ponto dois), try using partitive = false. If the decimal part seems to differ from the perceived input number, try using big numbers: `big"123.23"`. """)) From dc74774440767c37dbad6d02945e4f55359f1503 Mon Sep 17 00:00:00 2001 From: Jake Ireland Date: Tue, 21 Jun 2022 15:09:11 +1200 Subject: [PATCH 14/14] Update package version number --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a199ccaf..2c2603fa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SpelledOut" uuid = "4728c690-e668-4265-bc0d-51a8c0f93067" authors = ["Jake W. Ireland and contributors"] -version = "1.1.1" +version = "1.2.0" [deps] DecFP = "55939f99-70c6-5e9b-8bb0-5071ed7d61fd"