Skip to content

Commit

Permalink
Merge pull request #8 from ookii-tsuki/master
Browse files Browse the repository at this point in the history
Add pronunciation support
  • Loading branch information
Cutano authored Apr 20, 2021
2 parents a4ceaca + 8758535 commit ad753a8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Kawazu-Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using System.Threading.Tasks;

namespace Kawazu
Expand Down Expand Up @@ -66,7 +67,13 @@ private static async Task Main(string[] args)
};
}
var result = await converter.Convert(str, to, mode, system, "(", ")");
var pronunciation = new StringBuilder();
foreach (var div in await converter.GetDivisions(str, to, mode, system, "(", ")"))
{
pronunciation.Append(div.RomaPronunciation);
}
Console.WriteLine(result);
Console.WriteLine($"Pronunciation: {pronunciation}");
Console.WriteLine();
}
}
Expand Down
49 changes: 36 additions & 13 deletions Kawazu/Division.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ public string HiraReading
return builder.ToString();
}
}
public string HiraPronunciation
{
get
{
var builder = new StringBuilder();
foreach (var element in this)
{
builder.Append(element.HiraPronunciation);
}

public string KataReading => Utilities.ToRawKatakana(HiraReading);
return builder.ToString();
}
}

public string KataReading => Utilities.ToRawKatakana(HiraReading);
public string KataPronunciation => Utilities.ToRawKatakana(HiraPronunciation);
public string RomaReading => Utilities.ToRawRomaji(HiraReading);
public string RomaPronunciation => Utilities.ToRawRomaji(HiraPronunciation);

public readonly bool IsEndsInTsu;

Expand All @@ -52,30 +66,33 @@ public Division(MeCabIpaDicNode node, TextType type, RomajiSystem system = Romaj
switch (type)
{
case TextType.PureKana:
foreach (var ch in node.Surface)
{
this.Add(new JapaneseElement(ch.ToString(), Utilities.ToRawKatakana(ch.ToString()), TextType.PureKana, system));
}
for(var i = 0; i < node.Surface.Length; i++)
Add(new JapaneseElement(node.Surface[i].ToString(), Utilities.ToRawKatakana(node.Surface[i].ToString()), node.Pronounciation[i].ToString(), TextType.PureKana, system));
break;

case TextType.PureKanji:
this.Add(new JapaneseElement(node.Surface, node.Reading, TextType.PureKanji, system));
Add(new JapaneseElement(node.Surface, node.Reading, node.Pronounciation, TextType.PureKanji, system));
break;

case TextType.KanjiKanaMixed:
var surfaceBuilder = new StringBuilder(node.Surface);
var readingBuilder = new StringBuilder(node.Reading);
var pronunciationBuilder = new StringBuilder(node.Pronounciation);
var kanasInTheEnd = new StringBuilder();
while (Utilities.IsKana(surfaceBuilder[0])) // Pop the kanas in the front.
{
this.Add(new JapaneseElement(surfaceBuilder[0].ToString(), Utilities.ToRawKatakana(surfaceBuilder[0].ToString()), TextType.PureKana, system));
Add(new JapaneseElement(surfaceBuilder[0].ToString(), Utilities.ToRawKatakana(surfaceBuilder[0].ToString()), pronunciationBuilder[0].ToString(), TextType.PureKana, system));
surfaceBuilder.Remove(0, 1);
readingBuilder.Remove(0, 1);
pronunciationBuilder.Remove(0, 1);
}

while (Utilities.IsKana(surfaceBuilder[surfaceBuilder.Length - 1])) // Pop the kanas in the end.
{
kanasInTheEnd.Append(surfaceBuilder[surfaceBuilder.Length - 1].ToString());
surfaceBuilder.Remove(surfaceBuilder.Length - 1, 1);
readingBuilder.Remove(readingBuilder.Length - 1, 1);
pronunciationBuilder.Remove(pronunciationBuilder.Length - 1, 1);
}

if (Utilities.HasKana(surfaceBuilder.ToString())) // For the middle part:
Expand All @@ -95,41 +112,47 @@ where Utilities.IsKana(ele)
{
if (kanaIndex >= kanaList.Count)
{
this.Add(new JapaneseElement(ch.ToString(), readingBuilder.ToString(previousIndex + 1, readingBuilder.Length - previousIndex - 1), TextType.PureKanji, system));
Add(new JapaneseElement(ch.ToString(), readingBuilder.ToString(previousIndex + 1, readingBuilder.Length - previousIndex - 1), pronunciationBuilder.ToString(previousIndex + 1, readingBuilder.Length - previousIndex - 1), TextType.PureKanji, system));
continue;
}

var index = readingBuilder.ToString()
.IndexOf(Utilities.ToRawKatakana(kanaList[kanaIndex].ToString()), StringComparison.Ordinal);

this.Add(new JapaneseElement(ch.ToString(), readingBuilder.ToString(previousIndex + 1, index - previousIndex - 1), TextType.PureKanji, system));
Add(new JapaneseElement(ch.ToString(), readingBuilder.ToString(previousIndex + 1, index - previousIndex - 1), pronunciationBuilder.ToString(previousIndex + 1, index - previousIndex - 1), TextType.PureKanji, system));
previousIndex = index;
kanaIndex++;
}

if (Utilities.IsKana(ch))
{
this.Add(new JapaneseElement(ch.ToString(), Utilities.ToRawHiragana(ch.ToString()), TextType.PureKana, system));
var kana = Utilities.ToRawKatakana(ch.ToString());
Add(new JapaneseElement(ch.ToString(), kana, kana, TextType.PureKana, system));
}
}
}

else
{
this.Add(new JapaneseElement(surfaceBuilder.ToString(), readingBuilder.ToString(), TextType.PureKanji, system));
Add(new JapaneseElement(surfaceBuilder.ToString(), readingBuilder.ToString(), pronunciationBuilder.ToString(), TextType.PureKanji, system));
}

if (kanasInTheEnd.Length != 0)
{
for (var i = kanasInTheEnd.Length - 1; i >= 0; i--)
{
this.Add(new JapaneseElement(kanasInTheEnd.ToString()[i].ToString(), Utilities.ToRawKatakana(kanasInTheEnd.ToString()[i].ToString()), TextType.PureKana, system));
var kana = Utilities.ToRawKatakana(kanasInTheEnd.ToString()[i].ToString());
Add(new JapaneseElement(kanasInTheEnd.ToString()[i].ToString(), kana, kana, TextType.PureKana, system));
}
}
break;

case TextType.Others:
this.Add(new JapaneseElement(node.Surface, node.Surface, TextType.Others, system));
Add(new JapaneseElement(node.Surface, node.Surface, node.Pronounciation, TextType.Others, system));
break;

default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
}
Expand Down
19 changes: 15 additions & 4 deletions Kawazu/JapaneseElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,40 @@ public readonly struct JapaneseElement
public string Element { get; }

public string HiraNotation { get; }

public string HiraPronunciation { get; }
public string KataNotation { get; }

public string KataPronunciation { get; }
public string RomaNotation { get; }

public string RomaPronunciation { get; }

public TextType Type { get; }

public JapaneseElement(string element, string kataNotation, TextType type, RomajiSystem system = RomajiSystem.Hepburn)
public JapaneseElement(string element, string kataNotation, string kataPronunciation, TextType type, RomajiSystem system = RomajiSystem.Hepburn)
{
Element = element;
Type = type;

if (type == TextType.Others)
{
KataNotation = kataNotation;
KataPronunciation = kataPronunciation;

HiraNotation = kataNotation;
HiraPronunciation = kataPronunciation;

RomaNotation = kataNotation;
RomaPronunciation = kataPronunciation;
return;
}

KataNotation = kataNotation;
KataPronunciation = kataPronunciation;

HiraNotation = Utilities.ToRawHiragana(kataNotation);
HiraPronunciation = Utilities.ToRawHiragana(kataPronunciation);

RomaNotation = Utilities.ToRawRomaji(kataNotation, system);
RomaPronunciation = Utilities.ToRawRomaji(kataPronunciation, system);
}
}
}
4 changes: 2 additions & 2 deletions Kawazu/Kawazu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<Title>Kawazu</Title>
<Description>Kawazu is a C# library for converting Japanese sentence to Hiragana, Katakana or Romaji with furigana and okurigana modes supported. Inspired by project Kuroshiro.</Description>
<PackageTags>Japanese;Kana;Kanji;Mecab;Hiragana;Katakana;Furigana;Okurigana</PackageTags>
<Version>1.0.1</Version>
<Version>1.1.0</Version>
<Authors>Cutano</Authors>
<Company>Cutano</Company>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.0.1</AssemblyVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<Copyright>© Cutano 2020</Copyright>
<PackageProjectUrl>https://github.com/Cutano/Kawazu</PackageProjectUrl>
<RepositoryUrl>https://github.com/Cutano/Kawazu</RepositoryUrl>
Expand Down

0 comments on commit ad753a8

Please sign in to comment.