Skip to content

Commit

Permalink
Add radical names to kanjis that are also radicals (KANJIDIC2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Oct 8, 2023
1 parent 44bbf18 commit 4230fe7
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 14 deletions.
20 changes: 12 additions & 8 deletions JL.Core/Anki/JLField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@ public enum JLField
[Description("Primary Spelling")] PrimarySpelling,
Readings,
Definitions,
[Description("Alternative Spellings")] AlternativeSpellings,
[Description("Dictionary Name")] DictionaryName,
Audio,
Image,
Sentence,
[Description("Source Text")] SourceText,
[Description("Matched Text")] MatchedText,
[Description("EDICT ID")] EdictId,
[Description("Local Time")] LocalTime,
Frequencies,
[Description("Raw Frequencies")] RawFrequencies,

// JMdict
[Description("Primary Spelling with Orthography Info")] PrimarySpellingWithOrthographyInfo,
[Description("Readings with Orthography Info")] ReadingsWithOrthographyInfo,
[Description("Alternative Spellings with Orthography Info")] AlternativeSpellingsWithOrthographyInfo,
// JMdict, JMnedict, KANJIDIC2
[Description("EDICT ID")] EdictId,

// Word dictionaries
[Description("Deconjugated Matched Text")] DeconjugatedMatchedText,
[Description("Deconjugation Process")] DeconjugationProcess,
// JMdict, Nazeka EPWING
[Description("Alternative Spellings")] AlternativeSpellings,
// JMdict
[Description("Primary Spelling with Orthography Info")] PrimarySpellingWithOrthographyInfo,
[Description("Readings with Orthography Info")] ReadingsWithOrthographyInfo,
[Description("Alternative Spellings with Orthography Info")] AlternativeSpellingsWithOrthographyInfo,

//Kanji dictionaries
[Description("Kun Readings")] KunReadings,
[Description("On Readings")] OnReadings,
[Description("Kanji Composition")] KanjiComposition,
[Description("Kanji Statistics")] KanjiStats,
//KANJIDIC2
[Description("Nanori Readings")] NanoriReadings,
[Description("Stroke Count")] StrokeCount,
[Description("Kanji Grade")] KanjiGrade,
[Description("Kanji Composition")] KanjiComposition,
[Description("Kanji Statistics")] KanjiStats
[Description("Radical Names")] RadicalNames
}
1 change: 1 addition & 0 deletions JL.Core/Anki/JLFieldUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static class JLFieldUtils
JLField.KunReadings,
JLField.OnReadings,
JLField.NanoriReadings,
JLField.RadicalNames,
JLField.StrokeCount,
JLField.KanjiGrade,
JLField.KanjiComposition,
Expand Down
13 changes: 12 additions & 1 deletion JL.Core/Dicts/EDICT/KANJIDIC/KanjidicLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private static async Task ReadCharacter(XmlReader xmlReader, Dictionary<string,
List<string> nanoriReadingList = new();
List<string> onReadingList = new();
List<string> kunReadingList = new();
List<string> radicalNameList = new();

while (!xmlReader.EOF)
{
Expand Down Expand Up @@ -118,6 +119,15 @@ private static async Task ReadCharacter(XmlReader xmlReader, Dictionary<string,

break;

case "rad_name":
radicalNameList.Add((await xmlReader.ReadElementContentAsStringAsync().ConfigureAwait(false)).GetPooledString());
break;

// Old JLPT, has 4 levels instead of 5
//case "jlpt":
// jlpt = xmlReader.ReadElementContentAsInt();
// break;

default:
_ = await xmlReader.ReadAsync().ConfigureAwait(false);
break;
Expand All @@ -134,8 +144,9 @@ private static async Task ReadCharacter(XmlReader xmlReader, Dictionary<string,
string[]? onReadings = onReadingList.TrimStringListToStringArray();
string[]? kunReadings = kunReadingList.TrimStringListToStringArray();
string[]? nanoriReadings = nanoriReadingList.TrimStringListToStringArray();
string[]? radicalNames = radicalNameList.TrimStringListToStringArray();

KanjidicRecord entry = new(definitions, onReadings, kunReadings, nanoriReadings, strokeCount, grade, frequency);
KanjidicRecord entry = new(definitions, onReadings, kunReadings, nanoriReadings, radicalNames, strokeCount, grade, frequency);

kanjidicDictionary[key] = new IDictRecord[] { entry };
}
Expand Down
3 changes: 3 additions & 0 deletions JL.Core/Dicts/EDICT/KANJIDIC/KanjidicRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal sealed class KanjidicRecord : IDictRecord
public string[]? OnReadings { get; }
public string[]? KunReadings { get; }
public string[]? NanoriReadings { get; }
public string[]? RadicalNames { get; }
public int StrokeCount { get; }
public int Grade { get; }
public int Frequency { get; }
Expand All @@ -14,6 +15,7 @@ public KanjidicRecord(string[]? definitions,
string[]? onReadings,
string[]? kunReadings,
string[]? nanoriReadings,
string[]? radicalNames,
int strokeCount,
int grade,
int frequency)
Expand All @@ -25,6 +27,7 @@ public KanjidicRecord(string[]? definitions,
StrokeCount = strokeCount;
Grade = grade;
Frequency = frequency;
RadicalNames = radicalNames;
}

public string? BuildFormattedDefinition()
Expand Down
3 changes: 3 additions & 0 deletions JL.Core/Lookup/LookupResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class LookupResult
public string[]? OnReadings { get; init; }
public string[]? KunReadings { get; init; }
public string[]? NanoriReadings { get; init; }
public string[]? RadicalNames { get; }
public int StrokeCount { get; init; }
public string? KanjiComposition { get; init; }
public int KanjiGrade { get; init; }
Expand All @@ -43,6 +44,7 @@ internal LookupResult(
string[]? onReadings = null,
string[]? kunReadings = null,
string[]? nanoriReadings = null,
string[]? radicalNames = null,
string? formattedDefinitions = null,
string? process = null,
string? kanjiComposition = null,
Expand Down Expand Up @@ -72,5 +74,6 @@ internal LookupResult(
KanjiComposition = kanjiComposition;
KanjiStats = kanjiStats;
KanjiGrade = kanjiGrade;
RadicalNames = radicalNames;
}
}
1 change: 1 addition & 0 deletions JL.Core/Lookup/LookupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ private static List<LookupResult> BuildKanjidicResult(Dictionary<string, Interme
onReadings: kanjiRecord.OnReadings,
kunReadings: kanjiRecord.KunReadings,
nanoriReadings: kanjiRecord.NanoriReadings,
radicalNames: kanjiRecord.RadicalNames,
strokeCount: kanjiRecord.StrokeCount,
kanjiGrade: kanjiRecord.Grade,
kanjiComposition: DictUtils.s_kanjiCompositionDict.GetValueOrDefault(dictResult.Key),
Expand Down
56 changes: 54 additions & 2 deletions JL.Windows/GUI/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,13 +1000,65 @@ public StackPanel MakeResultStackPanel(LookupResult result, int index, int resul
}
}

if (result.RadicalNames is not null)
{
if (MiningMode)
{
TouchScreenTextBox radicalNameTextBox = new()
{
Name = nameof(result.RadicalNames),
Text = string.Create(CultureInfo.InvariantCulture, $"Radical names: {string.Join(", ", result.RadicalNames)}"),
Foreground = ConfigManager.ReadingsColor,
FontSize = ConfigManager.ReadingsFontSize,
Margin = new Thickness(2, 0, 0, 0),
TextWrapping = TextWrapping.Wrap,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Background = Brushes.Transparent,
BorderThickness = new Thickness(0, 0, 0, 0),
Padding = new Thickness(0),
IsReadOnly = true,
IsUndoEnabled = false,
UndoLimit = 0,
Cursor = Cursors.Arrow,
SelectionBrush = ConfigManager.HighlightColor,
IsInactiveSelectionHighlightEnabled = true,
ContextMenu = PopupContextMenu
};

radicalNameTextBox.PreviewMouseUp += TextBox_PreviewMouseUp;
radicalNameTextBox.MouseMove += TextBox_MouseMove;
radicalNameTextBox.LostFocus += Unselect;
radicalNameTextBox.PreviewMouseRightButtonUp += TextBox_PreviewMouseRightButtonUp;
radicalNameTextBox.MouseLeave += OnMouseLeave;
radicalNameTextBox.PreviewMouseLeftButtonDown += TextBox_PreviewMouseLeftButtonDown;
_ = bottom.Children.Add(radicalNameTextBox);
}

else
{
TextBlock radicalNameTextBlock = new()
{
Name = nameof(result.NanoriReadings),
Text = string.Create(CultureInfo.InvariantCulture, $"Radical names: {string.Join(", ", result.RadicalNames)}"),
Foreground = ConfigManager.ReadingsColor,
FontSize = ConfigManager.ReadingsFontSize,
Margin = new Thickness(2, 0, 0, 0),
TextWrapping = TextWrapping.Wrap,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
};
_ = bottom.Children.Add(radicalNameTextBlock);
}
}

if (result.KanjiGrade > -1)
{
string gradeText = PopupWindowUtils.GradeToText(result.KanjiGrade);
TextBlock gradeTextBlock = new()
{
Name = nameof(result.KanjiGrade),
Text = string.Create(CultureInfo.InvariantCulture, $"{nameof(result.KanjiGrade)}: {gradeText}"),
Text = string.Create(CultureInfo.InvariantCulture, $"Grade: {gradeText}"),
Foreground = ConfigManager.DefinitionsColor,
FontSize = ConfigManager.DefinitionsFontSize,
Margin = new Thickness(2, 2, 2, 2),
Expand All @@ -1022,7 +1074,7 @@ public StackPanel MakeResultStackPanel(LookupResult result, int index, int resul
TextBlock strokeCountTextBlock = new()
{
Name = nameof(result.StrokeCount),
Text = string.Create(CultureInfo.InvariantCulture, $"Strokes: {result.StrokeCount}"),
Text = string.Create(CultureInfo.InvariantCulture, $"Stroke count: {result.StrokeCount}"),
Foreground = ConfigManager.DefinitionsColor,
FontSize = ConfigManager.DefinitionsFontSize,
Margin = new Thickness(2, 2, 2, 2),
Expand Down
1 change: 1 addition & 0 deletions JL.Windows/GUI/PreferencesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static bool IsItVisible()
• Kun Readings: Kun readings of the mined kanji.
• On Readings: On readings of the mined kanji.
• Nanori Readings: Nanori readings of the mined kanji.
• Radical Names: Radical names of the kanji.
• Stroke Count: Stroke count of the kanji.
• Kanji Grade: The kanji grade level.
• Kanji Composition: Kanji composition info, e.g., "⿰女未" for "妹".
Expand Down
10 changes: 7 additions & 3 deletions JL.Windows/Utilities/PopupWindowUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ public static string GradeToText(int grade)
{
string gradeText = grade switch
{
0 => "Hyougai",
<= 6 => string.Create(CultureInfo.InvariantCulture, $"{grade} (Kyouiku)"),
>= 1 and <= 6 => string.Create(CultureInfo.InvariantCulture, $"{grade} (Kyouiku)"),
8 => string.Create(CultureInfo.InvariantCulture, $"{grade} (Jouyou)"),
<= 10 => string.Create(CultureInfo.InvariantCulture, $"{grade} (Jinmeiyou)"),
_ => ""
_ => "Hyougai"
};

return gradeText;
Expand Down Expand Up @@ -381,6 +380,11 @@ public static async Task Mine(LookupResult lookupResult, string currentText, int
miningParams[JLField.NanoriReadings] = string.Join(", ", lookupResult.NanoriReadings);
}

if (lookupResult.RadicalNames is not null)
{
miningParams[JLField.RadicalNames] = string.Join(", ", lookupResult.RadicalNames);
}

bool mined = await Mining.Mine(miningParams, lookupResult).ConfigureAwait(false);

if (mined)
Expand Down

0 comments on commit 4230fe7

Please sign in to comment.