Skip to content

Commit

Permalink
Apply changes requested in PR #1172
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Serrano committed Jan 29, 2022
1 parent a3ba88b commit 8471f6c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ namespace Humanizer.Localisation.NumberToWords
{
internal class SpanishNumberToWordsConverter : GenderedNumberToWordsConverter
{
private static readonly string[] HundredsRootMap = {
"cero", "ciento", "doscient", "trescient", "cuatrocient", "quinient", "seiscient", "setecient",
"ochocient", "novecient" };

private static readonly string[] HundredthsRootMap = {
"", "centésim", "ducentésim", "tricentésim", "cuadringentésim", "quingentésim", "sexcentésim",
"septingentésim", "octingentésim", "noningentésim" };

private static readonly string[] OrdinalsRootMap = { "" , "primer", "segund", "tercer", "cuart", "quint", "sext",
"séptim", "octav", "noven"};

private static readonly string[] TensMap = {
"cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };

private static readonly string[] TenthsRootMap = {
"", "décim", "vigésim", "trigésim", "cuadragésim", "quincuagésim", "sexagésim", "septuagésim",
"octogésim", "nonagésim" };

private static readonly string[] ThousandthsRootMap = {
"", "milésim", "dosmilésim", "tresmilésim", "cuatromilésim", "cincomilésim", "seismilésim",
"sietemilésim", "ochomilésim", "nuevemilésim" };

private static readonly string[] TupleMap = {
"cero veces", "una vez", "doble", "triple", "cuádruple", "quíntuple", "séxtuple", "séptuple", "óctuple",
"nonuplo", "décuplo", "undécuplo", "duodécuplo", "terciodécuplo" };

private static readonly string[] UnitsMap = {
"cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce",
"trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve", "veinte", "veintiuno",
"veintidós", "veintitrés", "veinticuatro", "veinticinco", "veintiséis", "veintisiete", "veintiocho", "veintinueve"};

public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
return Convert(input, WordForm.Normal, gender, addAnd);
Expand Down Expand Up @@ -80,14 +111,10 @@ public override string ConvertToOrdinal(int number, GrammaticalGender gender, Wo

public override string ConvertToTuple(int number)
{
string[] map = {
"cero veces", "una vez", "doble", "triple", "cuádruple", "quíntuble", "séxtuple", "séptuple", "óctuple",
"nonupla", "décuplo", "undécuplo", "duodécuplo", "terciodécuplpo" };

number = Math.Abs(number);

if (number < map.Length)
return map[number];
if (number < TupleMap.Length)
return TupleMap[number];

return Convert(number) + " veces";
}
Expand Down Expand Up @@ -118,11 +145,7 @@ private static string ConvertHundreds(in long inputNumber, out long remainder, G

private static string ConvertHundredths(in int number, out int remainder, GrammaticalGender gender)
{
string[] hundredthsRootMap = {
"", "centésim", "ducentésim", "tricentésim", "cuadringentésim", "quingentésim", "sexcentésim",
"septingentésim", "octingentésim", "noningentésim" };

return ConvertMappedOrdinalNumber(number, 100, hundredthsRootMap, out remainder, gender);
return ConvertMappedOrdinalNumber(number, 100, HundredthsRootMap, out remainder, gender);
}

private static string ConvertMappedOrdinalNumber(
Expand All @@ -149,9 +172,6 @@ private static string ConvertOrdinalUnits(in int number, GrammaticalGender gende
{
if (number is > 0 and < 10)
{
string[] ordinalsRootMap = { "" , "primer", "segund", "tercer", "cuart", "quint", "sext", "séptim",
"octav", "noven"};

Dictionary<GrammaticalGender, string> genderedEndingDict = new()
{
{ GrammaticalGender.Feminine, "a" },
Expand All @@ -160,70 +180,41 @@ private static string ConvertOrdinalUnits(in int number, GrammaticalGender gende

genderedEndingDict.Add(GrammaticalGender.Neuter, genderedEndingDict[GrammaticalGender.Masculine]);

return ordinalsRootMap[number] + genderedEndingDict[gender];
return OrdinalsRootMap[number] + genderedEndingDict[gender];
}

return string.Empty;
}

private static string ConvertTenths(in int number, out int remainder, GrammaticalGender gender)
{
string[] tenthsRootMap = {
"", "décim", "vigésim", "trigésim", "cuadragésim", "quincuagésim", "sexagésim", "septuagésim",
"octogésim", "nonagésim" };

return ConvertMappedOrdinalNumber(number, 10, tenthsRootMap, out remainder, gender);
return ConvertMappedOrdinalNumber(number, 10, TenthsRootMap, out remainder, gender);
}

private static string ConvertThousandths(in int number, out int remainder, GrammaticalGender gender)
{
string[] thousandthsRootMap = {
"", "milésim", "dosmilésim", "tresmilésim", "cuatromilésim", "cincomilésim", "seismilésim",
"sietemilésim", "ochomilésim", "nuevemilésim" };

return ConvertMappedOrdinalNumber(number, 1000, thousandthsRootMap, out remainder, gender);
return ConvertMappedOrdinalNumber(number, 1000, ThousandthsRootMap, out remainder, gender);
}

private static string ConvertUnits(long inputNumber, GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var genderedOne = new Dictionary<GrammaticalGender, string>()
{
{ GrammaticalGender.Feminine, "una" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "un" : "uno" }
};

genderedOne.Add(GrammaticalGender.Neuter, genderedOne[GrammaticalGender.Masculine]);

var genderedtwentyOne = new Dictionary<GrammaticalGender, string>()
{
{ GrammaticalGender.Feminine, "veintiuna" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "veintiún" : "veintiuno" }
};

genderedtwentyOne.Add(GrammaticalGender.Neuter, genderedtwentyOne[GrammaticalGender.Masculine]);

string[] unitsMap = {
"cero", genderedOne[gender], "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce",
"trece", "catorce", "quince", "dieciséis", "diecisiete", "dieciocho", "diecinueve", "veinte", genderedtwentyOne[gender],
"veintidós", "veintitrés", "veinticuatro", "veinticinco", "veintiséis", "veintisiete", "veintiocho", "veintinueve"};

string[] tensMap = {
"cero", "diez", "veinte", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };

var wordPart = string.Empty;

if (inputNumber > 0)
{
UnitsMap[1] = GetGenderedOne(gender, wordForm);
UnitsMap[21] = GetGenderedTwentyOne(gender, wordForm);

if (inputNumber < 30)
{
wordPart = unitsMap[inputNumber];
wordPart = UnitsMap[inputNumber];
}
else
{
wordPart = tensMap[inputNumber / 10];
wordPart = TensMap[inputNumber / 10];
if (inputNumber % 10 > 0)
{
wordPart += $" y {unitsMap[inputNumber % 10]}";
wordPart += $" y {UnitsMap[inputNumber % 10]}";
}
}
}
Expand All @@ -234,22 +225,41 @@ private static string ConvertUnits(long inputNumber, GrammaticalGender gender, W
private static IReadOnlyList<string> GetGenderedHundredsMap(GrammaticalGender gender)
{
var genderedEnding = gender == GrammaticalGender.Feminine ? "as" : "os";

string[] hundredsRootMap = {
"cero", "ciento", "doscient", "trescient", "cuatrocient", "quinient", "seiscient", "setecient",
"ochocient", "novecient" };

var map = new List<string>();
map.AddRange(hundredsRootMap.Take(2));
map.AddRange(HundredsRootMap.Take(2));

for (var i = 2; i < hundredsRootMap.Length; i++)
for (var i = 2; i < HundredsRootMap.Length; i++)
{
map.Add(hundredsRootMap[i] + genderedEnding);
map.Add(HundredsRootMap[i] + genderedEnding);
}

return map;
}

private static string GetGenderedOne(GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var genderedOne = new Dictionary<GrammaticalGender, string>()
{
{ GrammaticalGender.Feminine, "una" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "un" : "uno" }
};

genderedOne.Add(GrammaticalGender.Neuter, genderedOne[GrammaticalGender.Masculine]);
return genderedOne[gender];
}

private static string GetGenderedTwentyOne(GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var genderedtwentyOne = new Dictionary<GrammaticalGender, string>()
{
{ GrammaticalGender.Feminine, "veintiuna" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "veintiún" : "veintiuno" }
};

genderedtwentyOne.Add(GrammaticalGender.Neuter, genderedtwentyOne[GrammaticalGender.Masculine]);
return genderedtwentyOne[gender];
}

private static bool HasOrdinalAbbreviation(int number, WordForm wordForm)
{
return (number == 1 || number == 3) && wordForm == WordForm.Abbreviation;
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ public static string ToWords(this long number, GrammaticalGender gender, Culture
/// </example>
/// <param name="number">Number to be turned to words</param>
/// <param name="wordForm">Form of the word, i.e. abbreviation</param>
/// <param name="gender">The grammatical gender to use for output words</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns>The number converted to words</returns>
public static string ToWords(this long number, WordForm wordForm, GrammaticalGender gender, CultureInfo culture = null)
Expand Down

0 comments on commit 8471f6c

Please sign in to comment.