Hi,
When trying to transcript "chuu" into hiragana, I get the result "ใกใ
" instead of the expected "ใกใ
ใ".
This seems to happen when adding the "ya" parts after consonants by calling this method inTreeBuilder.cs
during the call line 70:
private static void AddNode(Node node, string romaji, string kana)
{
if (romaji.Length == 1 && !node.Children.ContainsKey(romaji))
node.Children.Add(romaji, new Node(kana));
else if (romaji.Length == 1 && node.Children.ContainsKey(romaji))
node.Children[romaji].Data = kana;
else
{
foreach (char c in romaji)
{
if (!node.Children.ContainsKey(c.ToString()))
node.Children.Add(c.ToString(), new Node(string.Empty));
node = node.Children[c.ToString()];
AddNode(node, romaji[1..], kana);
}
}
}
When trying to add ya
in the k
node, we get to foreach loop:
Current node is `k`, romaji = "ya", kana = "ใใ", current character is "y"
`k -> y` does not exist
we create `k -> y`
node = `k -> y`
-> Add node recursively (node: `k -> y`, romaji: "a", kana: "ใใ")
romaji.Length is 1 and `k -> y` does not have any child yet
`k -> y -> a` is created with value "ใใ"
<-
// next loop, node `k -> y`, romaji = "ya", kana = "ใใ", current character is "a"
`k -> y -> a` exists
node = `k -> y -> a`
-> Add node recursively (node: `k -> y -> a`, romaji: "a", kana: "ใใ")
romaji.Length is 1 and `k -> y -> a` does not have any child yet
`k -> y -> a -> a` is created with value "ใใ"
It seems that there is a mix of recursion and iteration here which mixes things up.
I can see two solutions to that problem.
By fixing the iterations:
private static void AddNode(Node node, string romaji, string kana)
{
if (romaji.Length == 1 && !node.Children.ContainsKey(romaji))
node.Children.Add(romaji, new Node(kana));
else if (romaji.Length == 1 && node.Children.ContainsKey(romaji))
node.Children[romaji].Data = kana;
else
{
var remaining = romaji;
foreach (char c in romaji)
{
if (!node.Children.ContainsKey(c.ToString()))
node.Children.Add(c.ToString(), new Node(string.Empty));
node = node.Children[c.ToString()];
AddNode(node, remaining[1..], kana);
remaining = remaining[1..];
}
}
}
By fixing the recursion: (this seems more elegant to me)
private static void AddNode(Node node, string romaji, string kana)
{
if (romaji.Length == 0)
return;
else if (romaji.Length == 1 && !node.Children.ContainsKey(romaji))
node.Children.Add(romaji, new Node(kana));
else if (romaji.Length == 1 && node.Children.ContainsKey(romaji))
node.Children[romaji].Data = kana;
else
{
var firstChar = romaji.First().ToString();
if (!node.Children.ContainsKey(firstChar))
node.Children.Add(firstChar.First().ToString(), new Node(string.Empty));
node = node.Children[firstChar.ToString()];
AddNode(node, romaji[1..], kana);
}
}