-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinnishSyllableTransformer.cs
52 lines (48 loc) · 1.98 KB
/
FinnishSyllableTransformer.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
public class FinnishSyllableTransformer : ISyllableTransformer
{
static public readonly string Alphabet = "abcdefghijklmnopqrstuywvxyzöä";
static public readonly string Vowels = "aeiouyäö";
static public readonly string Consonants = string.Join("", Alphabet.Except(Vowels));
static public readonly string[] Diftongs = new string[]
{
"ai", "ei", "oi", "äi", "öi", "ey", "äy", "öy", "au",
"eu", "ou", "ui", "yi", "iu", "iy", "ie", "uo", "yö"
};
static public bool IsConsonant(char c) => Consonants.Contains(c);
static public bool IsVowel(char c) => Vowels.Contains(c);
static public bool IsDiftong(string str) => Diftongs.Contains(str);
static public List<string> GetSyllables(string word)
{
List<string> syllables = new List<string>();
int start = 0;
for (int i = 1; i < word.Length - 1; i++)
{
char? prev = i > 0 ? (char?)word[i - 1] : null;
char a = word[i];
char b = word[i + 1];
char? next = i < word.Length - 2 ? (char?)word[i + 2] : null;
if (IsConsonant(a) && IsVowel(b))
{
syllables.Add(word.Substring(start, i - start));
start = i;
}
else if (IsVowel(a) && IsVowel(b))
{
bool DiftongA = prev.HasValue && IsDiftong(prev.Value.ToString() + a.ToString());
bool DiftongB = next.HasValue && IsDiftong(b.ToString() + next.Value.ToString());
if (!DiftongA && !DiftongB)
{
//todo: find next!!!!??? instead of start + 1
syllables.Add(word.Substring(start, i - start + 1).ToUpper());
start = ++i;
}
}
}
var last = syllables.LastOrDefault();
if (last == null || word != syllables.Join("").ToLower())
{
syllables.Add("|" + word.Substring(start));
}
return syllables;
}
}