-
Notifications
You must be signed in to change notification settings - Fork 67
/
Language.cs
executable file
·255 lines (222 loc) · 9.46 KB
/
Language.cs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
namespace TelegramClient.Utils
{
public enum Gender
{
Female,
Male
}
public static class Language
{
public static string CapitalizeFirstLetter(string data)
{
var chars = data.ToCharArray();
// Find the Index of the first letter
var charac = data.FirstOrDefault(char.IsLetter);
if (charac == default(char)) return data;
var i = data.IndexOf(charac);
// capitalize that letter
chars[i] = char.ToUpper(chars[i]);
return new string(chars);
}
private static readonly Dictionary<char, string> _ruEnTable = new Dictionary<char, string>
{
{'а', "a"}, {'б', "b"},
{'в', "v"}, {'г', "g"},
{'д', "d"}, {'е', "e"},
{'ё', "e"}, {'ж', "zh"},
{'з', "z"}, {'и', "i"},
{'й', "j"}, {'к', "k"},
{'л', "l"}, {'м', "m"},
{'н', "n"}, {'о', "o"},
{'п', "p"}, {'р', "r"},
{'с', "s"}, {'т', "t"},
{'у', "u"}, {'ф', "f"},
{'х', "kh"}, {'ц', "tc"},
{'ч', "ch"}, {'ш', "sh"},
{'щ', "shch"}, {'ъ', ""},
{'ы', "y"}, {'ь', ""},
{'э', "e"}, {'ю', "iu"},
{'я', "ia"}
};
private static readonly Dictionary<char, string> _enRuTable = new Dictionary<char, string>
{
{'a', "а"}, {'b', "б"},
{'c', "ц"}, {'d', "д"},
{'e', "е"}, {'f', "ф"},
{'g', "г"}, {'h', "х"},
{'i', "и"}, {'j', "й"},
{'k', "к"}, {'l', "л"},
{'m', "м"}, {'n', "н"},
{'o', "о"}, {'p', "п"},
{'q', "к"}, {'r', "р"},
{'s', "с"}, {'t', "т"},
{'u', "ю"}, {'v', "в"},
{'w', "в"}, {'x', "х"},
{'y', "й"}, {'z', "з"}
};
public static string Transliterate(string str)
{
var enCount = 0;
var ruCount = 0;
var count = 0;
const int maxCount = 7;
foreach (var alpha in str)
{
if (count > maxCount) break;
if (_enRuTable.ContainsKey(alpha))
{
enCount++;
}
else if (_ruEnTable.ContainsKey(alpha))
{
ruCount++;
}
count++;
}
if (enCount > ruCount)
{
return TransliterateToRussian(str);
}
return TransliterateToEnglish(str);
}
public static string TransliterateToRussian(string str)
{
var enStr = new StringBuilder();
foreach (var alpha in str)
{
if (_enRuTable.ContainsKey(alpha))
{
enStr.Append(_enRuTable[alpha]);
}
}
return enStr.ToString();
}
public static string TransliterateToEnglish(string str)
{
var enStr = new StringBuilder();
foreach (var alpha in str)
{
if (_ruEnTable.ContainsKey(alpha))
{
enStr.Append(_ruEnTable[alpha]);
}
}
return enStr.ToString();
}
/// <summary>
/// Does a word declension after a number.
/// </summary>
/// <param name="number"> Number </param>
/// <param name="nominative"> Nominative (собеседник, участник) </param>
/// <param name="genitiveSingular"> Genitive singular (собеседника, участника) </param>
/// <param name="genitivePlural"> Genitive plural (собеседников, участников) </param>
/// <returns></returns>
public static string RuDeclension(int number, string nominative, string genitiveSingular, string genitivePlural, string format)
{
var lastDigit = number%10;
var lastTwoDigits = number%100;
if (lastDigit == 1 && lastTwoDigits != 11)
{
return string.Format("{0} {1}", number, nominative);
}
if (lastDigit == 2 && lastTwoDigits != 12 || lastDigit == 3 && lastTwoDigits != 13 ||
lastDigit == 4 && lastTwoDigits != 14)
{
return string.Format("{2}{0}{2} {1}", number, genitiveSingular, format);
}
return string.Format("{2}{0}{2} {1}", number, genitivePlural, format);
}
/// <summary>
/// Does a word declension after a number.
/// </summary>
/// <param name="number"> Number </param>
/// <param name="singular"> Singular (company) </param>
/// <param name="plural"> Plural (companies) </param>
/// <returns></returns>
public static string EnDeclension(int number, string singular, string plural, string format)
{
return string.Format("{2}{0}{2} {1}", number, number == 1 || number == 0 ? singular : plural, format);
}
public static string Declension(int number, string nominativeSingular, string nominativePlural, string genitiveSingular, string genitivePlural, string enException = null, string format = null)
{
var culture = CultureInfo.CurrentUICulture;
var languageName = culture.IsNeutralCulture ? culture.EnglishName : culture.Parent.EnglishName;
if (string.Equals(languageName, "Russian", StringComparison.OrdinalIgnoreCase))
{
return RuDeclension(number, nominativeSingular, genitiveSingular, genitivePlural, format);
}
if (!string.IsNullOrEmpty(enException)) return enException;
return EnDeclension(number, nominativeSingular, nominativePlural, format);
}
/// <summary>
/// Does a word declension after a number.
/// </summary>
/// <param name="number"> Number </param>
/// <param name="nominative"> Nominative (собеседник, участник) </param>
/// <param name="genitiveSingular"> Genitive singular (собеседника, участника) </param>
/// <param name="genitivePlural"> Genitive plural (собеседников, участников) </param>
/// <returns></returns>
public static string RuDeclension2(int number, string nominative, string genitiveSingular, string genitivePlural)
{
var lastDigit = number % 10;
var lastTwoDigits = number % 100;
if (lastDigit == 1 && lastTwoDigits != 11)
{
return string.Format("{0}", nominative);
}
if (lastDigit == 2 && lastTwoDigits != 12 || lastDigit == 3 && lastTwoDigits != 13 ||
lastDigit == 4 && lastTwoDigits != 14)
{
return string.Format("{0}", genitiveSingular);
}
return string.Format("{0}", genitivePlural);
}
/// <summary>
/// Does a word declension after a number.
/// </summary>
/// <param name="number"> Number </param>
/// <param name="singular"> Singular (company) </param>
/// <param name="plural"> Plural (companies) </param>
/// <returns></returns>
public static string EnDeclension2(int number, string singular, string plural)
{
return string.Format("{0}", number == 1 || number == 0 ? singular : plural);
}
public static string Declension2(int number, string nominativeSingular, string nominativePlural, string genitiveSingular, string genitivePlural, string enException = null)
{
var culture = CultureInfo.CurrentUICulture;
var languageName = culture.IsNeutralCulture ? culture.EnglishName : culture.Parent.EnglishName;
if (string.Equals(languageName, "Russian", StringComparison.OrdinalIgnoreCase))
{
return RuDeclension2(number, nominativeSingular, genitiveSingular, genitivePlural);
}
if (!string.IsNullOrEmpty(enException)) return enException;
return EnDeclension2(number, nominativeSingular, nominativePlural);
}
public static string GenderString(Gender gender, string maleString, string femaleString)
{
return gender == Gender.Male ? maleString : femaleString;
}
public static void Test()
{
for (var i = 0; i < 105; i++)
{
Debug.WriteLine(RuDeclension(i, "собеседник", "собеседника", "собеседников", null));
}
Console.ReadLine();
}
}
}