-
Notifications
You must be signed in to change notification settings - Fork 966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NumberToWord Turkish implementation #312
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Humanizer.Tests.Localisation.tr | ||
{ | ||
public class NumberToWordsTests : AmbientCulture | ||
{ | ||
public NumberToWordsTests() : base("tr") { } | ||
|
||
[Theory] | ||
[InlineData("sıfır", 0)] | ||
[InlineData("bir", 1)] | ||
[InlineData("iki", 2)] | ||
[InlineData("on", 10)] | ||
[InlineData("yüz on iki", 112)] | ||
[InlineData("bin dört yüz kırk", 1440)] | ||
[InlineData("yirmi iki", 22)] | ||
[InlineData("on bir", 11)] | ||
[InlineData("üç bin beş yüz bir", 3501)] | ||
[InlineData("bir milyon bir", 1000001)] | ||
[InlineData("eksi bir milyon üç yüz kırk altı bin yedi yüz on bir", -1346711)] | ||
public void ToWords(string expected, int number) | ||
{ | ||
Assert.Equal(expected, number.ToWords()); | ||
} | ||
|
||
[Theory] | ||
[InlineData(0, "sıfırıncı")] | ||
[InlineData(1, "birinci")] | ||
[InlineData(2, "ikinci")] | ||
[InlineData(3, "üçüncü")] | ||
[InlineData(4, "dördüncü")] | ||
[InlineData(5, "beşinci")] | ||
[InlineData(6, "altıncı")] | ||
[InlineData(7, "yedinci")] | ||
[InlineData(8, "sekizinci")] | ||
[InlineData(9, "dokuzuncu")] | ||
[InlineData(10, "onuncu")] | ||
[InlineData(11, "on birinci")] | ||
[InlineData(12, "on ikinci")] | ||
[InlineData(13, "on üçüncü")] | ||
[InlineData(14, "on dördüncü")] | ||
[InlineData(15, "on beşinci")] | ||
[InlineData(16, "on altıncı")] | ||
[InlineData(17, "on yedinci")] | ||
[InlineData(18, "on sekizinci")] | ||
[InlineData(19, "on dokuzuncu")] | ||
[InlineData(20, "yirminci")] | ||
[InlineData(21, "yirmi birinci")] | ||
[InlineData(30, "otuzuncu")] | ||
[InlineData(40, "kırkıncı")] | ||
[InlineData(50, "ellinci")] | ||
[InlineData(60, "altmışıncı")] | ||
[InlineData(70, "yetmişinci")] | ||
[InlineData(80, "sekseninci")] | ||
[InlineData(90, "doksanıncı")] | ||
[InlineData(100, "yüzüncü")] | ||
[InlineData(120, "yüz yirminci")] | ||
[InlineData(121, "yüz yirmi birinci")] | ||
[InlineData(200, "iki yüzüncü")] | ||
[InlineData(221, "iki yüz yirmi birinci")] | ||
[InlineData(300, "üç yüzüncü")] | ||
[InlineData(321, "üç yüz yirmi birinci")] | ||
[InlineData(1000, "bininci")] | ||
[InlineData(1001, "bin birinci")] | ||
[InlineData(10000, "on bininci")] | ||
[InlineData(100000, "yüz bininci")] | ||
[InlineData(1000000, "bir milyonuncu")] | ||
[InlineData(1022135, "bir milyon yirmi iki bin yüz otuz beşinci")] | ||
public void ToOrdinalWords(int number, string words) | ||
{ | ||
Assert.Equal(words, number.ToOrdinalWords()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Humanizer.Localisation.NumberToWords | ||
{ | ||
internal class TurkishNumberToWordConverter : GenderlessNumberToWordsConverter | ||
{ | ||
private static readonly string[] UnitsMap = { "sıfır", "bir", "iki", "üç", "dört", "beş", "altı", "yedi", "sekiz", "dokuz" }; | ||
private static readonly string[] TensMap = { "sıfır", "on", "yirmi", "otuz", "kırk", "elli", "altmış", "yetmiş", "seksen", "doksan" }; | ||
|
||
private static readonly Dictionary<char, string> OrdinalSuffix = new Dictionary<char, string> | ||
{ | ||
{'ı', "ıncı"}, | ||
{'i', "inci"}, | ||
{'u', "uncu"}, | ||
{'ü', "üncü"}, | ||
{'o', "uncu"}, | ||
{'ö', "üncü"}, | ||
{'e', "inci"}, | ||
{'a', "ıncı"}, | ||
}; | ||
|
||
public override string Convert(int number) | ||
{ | ||
if (number == 0) | ||
return UnitsMap[0]; | ||
|
||
if (number < 0) | ||
return string.Format("eksi {0}", Convert(-number)); | ||
|
||
var parts = new List<string>(); | ||
|
||
if ((number / 1000000000) > 0) | ||
{ | ||
parts.Add(string.Format("{0} milyar", Convert(number / 1000000000))); | ||
number %= 1000000000; | ||
} | ||
|
||
if ((number / 1000000) > 0) | ||
{ | ||
parts.Add(string.Format("{0} milyon", Convert(number / 1000000))); | ||
number %= 1000000; | ||
} | ||
|
||
var thousand = (number / 1000); | ||
if (thousand > 0) | ||
{ | ||
parts.Add(string.Format("{0} bin", thousand > 1 ? Convert(thousand) : "").Trim()); | ||
number %= 1000; | ||
} | ||
|
||
var hundred = (number / 100); | ||
if (hundred > 0) | ||
{ | ||
parts.Add(string.Format("{0} yüz", hundred > 1 ? Convert(number / 100) : "").Trim()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could reuse the already calculated |
||
number %= 100; | ||
} | ||
|
||
if ((number / 10) > 0) | ||
{ | ||
parts.Add(TensMap[number / 10]); | ||
number %= 10; | ||
} | ||
|
||
if (number > 0) | ||
{ | ||
parts.Add(UnitsMap[number]); | ||
} | ||
|
||
string toWords = string.Join(" ", parts.ToArray()); | ||
|
||
return toWords; | ||
} | ||
|
||
public override string ConvertToOrdinal(int number) | ||
{ | ||
var word = Convert(number); | ||
var wordSuffix = string.Empty; | ||
var wordChars = word.ToCharArray().Reverse().ToArray(); | ||
var suffixFoundOnLastVowel = false; | ||
|
||
for (int i = 0; i < wordChars.Count(); i++) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can iterate in reverse order over the word
and you can remove the copy |
||
{ | ||
wordSuffix = (from suffix in OrdinalSuffix where suffix.Key == wordChars[i] select suffix.Value).FirstOrDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may the |
||
if (!string.IsNullOrEmpty(wordSuffix)) | ||
{ | ||
suffixFoundOnLastVowel = i == 0; | ||
break; | ||
} | ||
} | ||
|
||
if (wordChars[0] == 't') | ||
word = word.Substring(0, word.Length - 1) + 'd'; | ||
|
||
if (suffixFoundOnLastVowel) | ||
word = word.Substring(0, word.Length - 1); | ||
|
||
return String.Format("{0}{1}", word, wordSuffix); | ||
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Humanizer.Localisation.Ordinalizers | ||
{ | ||
internal class TurkishOrdinalizer : DefaultOrdinalizer | ||
{ | ||
public override string Convert(int number, string numberString) | ||
{ | ||
return numberString + "."; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should indicate that Ordinalizer is added too.