forked from Humanizr/Humanizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
604ebcc
commit 82894c7
Showing
10 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests | ||
{ | ||
[UseCulture("en-US")] | ||
public class WordsToNumberTest | ||
{ | ||
[InlineData("one", 1)] | ||
[InlineData("minus five", -5)] | ||
[InlineData("eleven", 11)] | ||
[InlineData("ninety five", 95)] | ||
[InlineData("hundred five", 105)] | ||
[InlineData("one hundred ninety six", 196)] | ||
[Theory] | ||
public void ToNumber(string words, int expectedNumber) | ||
{ | ||
Assert.Equal(expectedNumber, words.ToNumber()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Humanizer/Configuration/DefaultWordsToNumberConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Globalization; | ||
using Humanizer.Localisation.WordsToNumber; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class DefaultWordsToNumberConverter : GenderlessWordsToNumberConverter | ||
{ | ||
private readonly CultureInfo _culture; | ||
|
||
public DefaultWordsToNumberConverter(CultureInfo culture) | ||
{ | ||
_culture = culture; | ||
} | ||
|
||
public override int Convert(string words) | ||
{ | ||
return words.ToNumber(_culture); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Humanizer.Configuration | ||
{ | ||
public interface IWordsToNumberConverter | ||
{ | ||
int Convert(string words); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Humanizer/Configuration/WordsToNumberConverterRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Humanizer.Localisation.WordsToNumber; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class WordsToNumberConverterRegistry : LocaliserRegistry<IWordsToNumberConverter> | ||
{ | ||
public WordsToNumberConverterRegistry() | ||
: base((culture) => new DefaultWordsToNumberConverter(culture)) | ||
{ | ||
Register("en", new EnglishWordsToNumberConverter()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/Humanizer/Localisation/WordsToNumber/EnglishWordsToNumberConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Humanizer.Configuration; | ||
|
||
namespace Humanizer.Localisation.WordsToNumber | ||
{ | ||
internal class EnglishWordsToNumberConverter : GenderlessWordsToNumberConverter | ||
{ | ||
private static readonly Dictionary<string, int> NumbersMap = new Dictionary<string, int> | ||
{ | ||
{"zero",0}, | ||
{"one",1}, | ||
{"two",2}, | ||
{"three",3}, | ||
{"four",4}, | ||
{"five",5}, | ||
{"six",6}, | ||
{"seven",7}, | ||
{"eight",8}, | ||
{"nine",9}, | ||
{"ten",10}, | ||
{"eleven",11}, | ||
{"twelve",12}, | ||
{"thirteen",13}, | ||
{"fourteen",14}, | ||
{"fifteen",15}, | ||
{"sixteen",16}, | ||
{"seventeen",17}, | ||
{"eighteen",18}, | ||
{"nineteen",19}, | ||
{"twenty", 20 }, | ||
{"thirty", 30 }, | ||
{"forty", 40 }, | ||
{"fifty", 50 }, | ||
{"sixty", 60 }, | ||
{"seventy", 70 }, | ||
{"eighty", 80 }, | ||
{"ninety", 90 }, | ||
{"hundred", 100 }, | ||
{"thousand", 1000 }, | ||
{"million", 1000000 }, | ||
{"billion", 1000000000 } | ||
}; | ||
|
||
public override int Convert(string words) | ||
{ | ||
bool isNegative = false; | ||
if (words.StartsWith("minus")) | ||
{ | ||
isNegative = true; | ||
words = words.Remove(0, 6); | ||
} | ||
|
||
string[] wordsArray = words.Split(' '); | ||
|
||
int response = NumbersMap[wordsArray[0]]; | ||
|
||
for (int i = 1; i < wordsArray.Length; i++) | ||
{ | ||
if(response < NumbersMap[wordsArray[i]]) | ||
{ | ||
response *= NumbersMap[wordsArray[i]]; | ||
} | ||
else | ||
{ | ||
response += NumbersMap[wordsArray[i]]; | ||
} | ||
|
||
} | ||
|
||
if(isNegative) | ||
{ | ||
return response*-1; | ||
} | ||
else | ||
{ | ||
return response; | ||
} | ||
|
||
|
||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Humanizer/Localisation/WordsToNumber/GenderlessWordsToNumberConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Humanizer.Configuration; | ||
|
||
namespace Humanizer.Localisation.WordsToNumber | ||
{ | ||
internal abstract class GenderlessWordsToNumberConverter : IWordsToNumberConverter | ||
{ | ||
public abstract int Convert(string words); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Globalization; | ||
using Humanizer.Configuration; | ||
|
||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Transform humanized string to number; e.g. one => 1 | ||
/// </summary> | ||
public static class WordsToNumberExtension | ||
{ | ||
public static int ToNumber(this string words, CultureInfo culture = null) | ||
{ | ||
return Configurator.GetWordsToNumberConverter(culture).Convert(words); | ||
} | ||
|
||
} | ||
} |