-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (115 loc) · 4.55 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (115 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
public class TTSTools
{
private static readonly string[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
private static readonly string[] teens = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
private static readonly string[] tens = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
private static readonly string[] thousands = { "", "thousand", "million", "billion", "trillion" };
public static string convertNumbertoString(int number)
{
if (number == 0)
{
return "zero";
}
string text = "";
for (int i = 0; i < number; i++)
{
if (number % 1000 != 0)//if number is greater than a thousand
{
text = convertHundreds(number % 1000) + thousands[i] + " " + text;
}
number /= 1000; // remove thousand digit
}
return text.Trim();
}
public static string convertHundreds(int number)
{
string words = "";
if (number >= 100)
{
words += ones[number / 100] + " hundred ";//count hundreds
}
if (number >- 10 && number <19)//if bigger than 9 and less than 20
{
words += tens[number % 10] + " ";//count tens
}
else
{
words += tens[number / 10] + " ";//count normal tens
words += ones[number % 10] + " ";//count single digits
}
return words;
}
public static string replaceAcronyms(string text)
{
text.Replace("dr", "doctor");
text.Replace("mr", "mister");
text.Replace("mrs", "missus");
text.Replace("ms", "miss");
text.Replace("e.g.", "e g");
text.Replace("+", "plus ");
text.Replace("-", "minus ");
text.Replace("#", "hashtag ");
text.Replace("=", "equals ");
text.Replace("%", "mod ");
text.Replace("/", "slash ");
text.Replace("etc", "etcetera ");
return text;
}
private string NormaliseText(string text)
{
bool numberStarted = false;
bool decimalStarted = false;
string newText = "";
for (int i = 0; i < text.Length; i++)
{
if (int.TryParse(text[i].ToString(), out int result) == true)//if character is number
{
if (i! < 2 && !decimalStarted)//if not first 2 characters and a decimal hasn't started
{
if (text[i - 1].ToString() == "." && int.TryParse(text[i - 2].ToString(), out int result2) == true)//if previous character was decimal AND the character before that was a number
{
decimalStarted = true;//decimal number counting started
newText.Remove(i - 1);//delete the last character
newText += "point ";//decimal started
}
}
if (!decimalStarted)//if not in a decimal
{
numberStarted = true;//Number counting has started
int number = result;//Numberstarted
int indent = 1;//check next number
do //while not final character
{
if (int.TryParse(text[i + indent].ToString(), out result) == true)//if next number
{
number = number * 10 + result;//then add next number to back of "number"
indent++;//increment next number
}
else
{
numberStarted = false; //Numberended
}
} while (i != text.Length - 1 && numberStarted);//while not final character
i += indent;//skip rest of numbers
}
else//decimal
{
newText += ones[result % 10] + " ";//add single digit
}
}
else//if not number
{
if (decimalStarted)//disable decimal started
{
decimalStarted = false;
}
newText += text[i];
}
}
newText = newText.ToLower();
newText = replaceAcronyms(newText);
return newText;
}
}