Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"chuu" is translated into "ちゅ" instead of "ちゅう" #1

Closed
Gothor opened this issue Oct 21, 2023 · 1 comment · Fixed by #2
Closed

"chuu" is translated into "ちゅ" instead of "ちゅう" #1

Gothor opened this issue Oct 21, 2023 · 1 comment · Fixed by #2
Assignees

Comments

@Gothor
Copy link

Gothor commented Oct 21, 2023

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);
    }
}
@kmoroz kmoroz self-assigned this Oct 24, 2023
@kmoroz kmoroz linked a pull request Oct 24, 2023 that will close this issue
@kmoroz kmoroz closed this as completed in #2 Oct 24, 2023
@kmoroz
Copy link
Owner

kmoroz commented Oct 24, 2023

Thanks for such detailed feedback @Gothor ! I used your second solution above and this is now fixed in release 1.0.2. ありがとうございました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants