I have some paragraphs that contained the Textual Amounts multiple times (e.g : Insurance with limits of not less than Five Hundred Thousand Dollars per person and One Million Dollars) per occurrence insuring against all liability)
I have the following code that is responsible for extracting the numbers from the textual amount. Like (one thousand five hundred = 1500). It works fine if amount occurrence is only one but difficult when there is more than one occurrence of the amount in textual format. The numbers returned are not correct.
private static Dictionary<string, long> numberTable = new Dictionary<string, long>
{ {"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},
{"trillion",1000000000000},{"quadrillion",1000000000000000},
{"quintillion",1000000000000000000}};
var numbers = Regex.Matches(numberString, @"\w+").Cast<Match>()
.Select(m => m.Value.ToLowerInvariant())
.Where(v => numberTable.ContainsKey(v)).Select(v => numberTable[v]);
long acc = 0, total = 0L;
foreach (var n in numbers)
{
if (n >= 1000)
{
total += (acc * n);
acc = 0;
}
else if (n >= 100)
{
acc *= n;
}
else acc += n;
}
string a = Convert.ToString((total + acc) * (numberString.StartsWith("minus", StringComparison.InvariantCultureIgnoreCase) ? -1 : 1));
return a;
Can anyone help or give suggestion to solve the issue.
If I input only the text ="Insurance with limits of not less than Five Hundred Thousand Dollars" then the output is correct that us 500,000
But If i input the text = "Insurance with limits of not less than Five Hundred Thousand Dollars per person and One Million Dollars) per occurrence insuring against all liability"
Then answer I get is 1500000 , But I need this separately like 50,000 and 1000,000
Note: I also have in my mind that if I can get all amounts to end up with dollars and convert them one by one. But I don't think that will be a good choice, I open for any kind of discussion. thanks