Skip to content
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

Merged
merged 2 commits into from
Jul 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [#306](https://github.com/MehdiK/Humanizer/pull/306): Added Singularize/Pluralize overload without using obsolete plurality enum
- [#303](https://github.com/MehdiK/Humanizer/pull/303): Added support for all integer types in ByteSize extensions
- [#307](https://github.com/MehdiK/Humanizer/pull/307): Added support to string.FormatWith for the explicit culture parameter.
- [#312](https://github.com/MehdiK/Humanizer/pull/312): Added Turkish NumberToWord implementation
Copy link
Member

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.


[Commits](https://github.com/MehdiK/Humanizer/compare/v1.27.0...master)

Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="Localisation\sv\TimeSpanHumanizeTests.cs" />
<Compile Include="DateTimeHumanizePrecisionStrategyTests.cs" />
<Compile Include="Localisation\tr\DateHumanizeTests.cs" />
<Compile Include="Localisation\tr\NumberToWordsTests.cs" />
<Compile Include="Localisation\tr\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\vi\DateHumanizeTests.cs" />
<Compile Include="Localisation\vi\TimeSpanHumanizeTests.cs" />
Expand Down
75 changes: 75 additions & 0 deletions src/Humanizer.Tests/Localisation/tr/NumberToWordsTests.cs
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
Expand Up @@ -19,6 +19,7 @@ internal class NumberToWordsConverterRegistry : LocaliserRegistry<INumberToWords
Register("sl", (culture) => new SlovenianNumberToWordsConverter(culture));
Register("de", new GermanNumberToWordsConverter());
Register("bn-BD", new BanglaNumberToWordsConverter());
Register("tr", new TurkishNumberToWordConverter());
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer/Configuration/OrdinalizerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public OrdinalizerRegistry() : base(new DefaultOrdinalizer())
Register("ru", new RussianOrdinalizer());
Register("pt-BR", new BrazilianPortugueseOrdinalizer());
Register("de", new GermanOrdinalizer());
Register("tr", new TurkishOrdinalizer());
}
}
}
2 changes: 2 additions & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@
<Compile Include="Localisation\NumberToWords\PolishNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\RussianNumberToWordsConverter.cs" />
<Compile Include="Localisation\Formatters\HebrewFormatter.cs" />
<Compile Include="Localisation\NumberToWords\TurkishNumberToWordConverter.cs" />
<Compile Include="Localisation\Ordinalizers\IOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\GermanOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\RussianOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\SpanishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\DefaultOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\EnglishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\BrazilianPortugueseOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizers\TurkishOrdinalizer.cs" />
<Compile Include="Localisation\Tense.cs" />
<Compile Include="Localisation\NumberToWords\SpanishNumberToWordsConverter.cs" />
<Compile Include="TimeSpanHumanizeExtensions.cs" />
Expand Down
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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could reuse the already calculated hundred value

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++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can iterate in reverse order over the word

for (var i = word.Length - 1; i >= 0; i--)

and you can remove the copy wordChars

{
wordSuffix = (from suffix in OrdinalSuffix where suffix.Key == wordChars[i] select suffix.Value).FirstOrDefault();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may the OrdinalSuffix.TryGetValue be an alternative to this linear lookup?

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);
}


}
}
15 changes: 15 additions & 0 deletions src/Humanizer/Localisation/Ordinalizers/TurkishOrdinalizer.cs
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 + ".";
}
}
}