Skip to content

Commit

Permalink
Fix #68
Browse files Browse the repository at this point in the history
New JLFields for mining (raw frequencies, primary spelling/readings/alternative spellings with orthography info)

Fix font combo box items not being visible with a light theme

Refactor mining
  • Loading branch information
rampaa committed Oct 7, 2023
1 parent ed6d0ec commit 458b52a
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 457 deletions.
6 changes: 6 additions & 0 deletions JL.Core/Anki/JLField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public enum JLField
[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,

// Word dictionaries
[Description("Deconjugated Matched Text")] DeconjugatedMatchedText,
Expand Down
5 changes: 5 additions & 0 deletions JL.Core/Anki/JLFieldUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ public static class JLFieldUtils
public static readonly JLField[] JLFieldsForWordDicts = {
JLField.Nothing,
JLField.PrimarySpelling,
JLField.PrimarySpellingWithOrthographyInfo,
JLField.AlternativeSpellings,
JLField.AlternativeSpellingsWithOrthographyInfo,
JLField.Readings,
JLField.ReadingsWithOrthographyInfo,
JLField.Definitions,
JLField.DictionaryName,
JLField.Audio,
Expand All @@ -17,6 +20,7 @@ public static class JLFieldUtils
JLField.DeconjugatedMatchedText,
JLField.DeconjugationProcess,
JLField.Frequencies,
JLField.RawFrequencies,
JLField.EdictId,
JLField.LocalTime
};
Expand All @@ -38,6 +42,7 @@ public static class JLFieldUtils
JLField.SourceText,
JLField.Sentence,
JLField.Frequencies,
JLField.RawFrequencies,
JLField.EdictId,
JLField.LocalTime
};
Expand Down
65 changes: 23 additions & 42 deletions JL.Core/Anki/Mining.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
using System.Globalization;
using JL.Core.Audio;
using JL.Core.Dicts;
using JL.Core.Lookup;
using JL.Core.Network;
using JL.Core.Utilities;

namespace JL.Core.Anki;

public static class Mining
{
public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
public static async Task<bool> Mine(Dictionary<JLField, string> miningParams, LookupResult lookupResult)
{
string primarySpelling = miningParams[JLField.PrimarySpelling];

Dictionary<MineType, AnkiConfig>? ankiConfigDict = await AnkiConfig.ReadAnkiConfig().ConfigureAwait(false);

if (ankiConfigDict is null)
if (!CoreConfig.AnkiIntegration)
{
Utils.Frontend.Alert(AlertLevel.Error, "Please setup mining first in the preferences");
return false;
}

AnkiConfig? ankiConfig;

DictType? dictType = null;

if (miningParams.TryGetValue(JLField.DictionaryName, out string? dictionaryName))
{
if (DictUtils.Dicts.TryGetValue(dictionaryName, out Dict? dict))
{
dictType = dict.Type;
}
}
Dictionary<MineType, AnkiConfig>? ankiConfigDict = await AnkiConfig.ReadAnkiConfig().ConfigureAwait(false);

if (dictType is null)
if (ankiConfigDict is null)
{
Utils.Frontend.Alert(AlertLevel.Error, string.Create(CultureInfo.InvariantCulture, $"Mining failed for {primarySpelling}. Cannot find the type of {JLField.DictionaryName.GetDescription()}"));
Utils.Logger.Error("Mining failed for {FoundSpelling}. Cannot find the type of {DictionaryName}", primarySpelling, JLField.DictionaryName.GetDescription());
Utils.Frontend.Alert(AlertLevel.Error, "Please setup mining first in the preferences");
return false;
}


if (DictUtils.s_wordDictTypes.Contains(dictType.Value))
AnkiConfig? ankiConfig;
if (DictUtils.s_wordDictTypes.Contains(lookupResult.Dict.Type))
{
_ = ankiConfigDict.TryGetValue(MineType.Word, out ankiConfig);
}

else if (DictUtils.s_kanjiDictTypes.Contains(dictType.Value))
else if (DictUtils.s_kanjiDictTypes.Contains(lookupResult.Dict.Type))
{
_ = ankiConfigDict.TryGetValue(MineType.Kanji, out ankiConfig);
}

else if (DictUtils.s_nameDictTypes.Contains(dictType.Value))
else if (DictUtils.s_nameDictTypes.Contains(lookupResult.Dict.Type))
{
_ = ankiConfigDict.TryGetValue(MineType.Name, out ankiConfig);
}
Expand All @@ -66,20 +52,13 @@ public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
return false;
}

// idk if this gets the right audio for every word
string? reading = miningParams.GetValueOrDefault(JLField.Readings)?.Split(',')[0];
if (string.IsNullOrEmpty(reading))
{
reading = primarySpelling;
}

Dictionary<string, JLField> userFields = ankiConfig.Fields;
Dictionary<string, object> fields = ConvertFields(userFields, miningParams);

List<string> audioFields = FindFields(JLField.Audio, userFields);
bool needsAudio = audioFields.Count > 0;
string reading = lookupResult.Readings?[0] ?? lookupResult.PrimarySpelling;

AudioResponse? audioResponse = needsAudio
? await AudioUtils.GetPrioritizedAudio(primarySpelling, reading).ConfigureAwait(false)
? await AudioUtils.GetPrioritizedAudio(lookupResult.PrimarySpelling, reading).ConfigureAwait(false)
: null;

byte[]? audioData = audioResponse?.AudioData;
Expand All @@ -98,7 +77,7 @@ public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
: new Dictionary<string, object>(4)
{
{ "data", audioData },
{ "filename", string.Create(CultureInfo.InvariantCulture, $"JL_audio_{reading}_{primarySpelling}.{audioResponse!.AudioFormat}") },
{ "filename", string.Create(CultureInfo.InvariantCulture, $"JL_audio_{reading}_{lookupResult.PrimarySpelling}.{audioResponse!.AudioFormat}") },
{ "skipHash", Networking.Jpod101NoAudioMd5Hash },
{ "fields", audioFields }
};
Expand All @@ -114,7 +93,7 @@ public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
: new Dictionary<string, object>(3)
{
{ "data", imageBytes },
{ "filename", string.Create(CultureInfo.InvariantCulture, $"JL_image_{reading}_{primarySpelling}.png") },
{ "filename", string.Create(CultureInfo.InvariantCulture, $"JL_image_{reading}_{lookupResult.PrimarySpelling}.png") },
{ "fields", imageFields }
};

Expand All @@ -123,26 +102,28 @@ public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
{ "allowDuplicate", CoreConfig.AllowDuplicateCards }
};

Dictionary<string, object> fields = ConvertFields(userFields, miningParams);

Note note = new(ankiConfig.DeckName, ankiConfig.ModelName, fields, options, ankiConfig.Tags, audio, null, image);
Response? response = await AnkiConnect.AddNoteToDeck(note).ConfigureAwait(false);

if (response is null)
{
Utils.Frontend.Alert(AlertLevel.Error, string.Create(CultureInfo.InvariantCulture, $"Mining failed for {primarySpelling}"));
Utils.Logger.Error("Mining failed for {FoundSpelling}", primarySpelling);
Utils.Frontend.Alert(AlertLevel.Error, string.Create(CultureInfo.InvariantCulture, $"Mining failed for {lookupResult.PrimarySpelling}"));
Utils.Logger.Error("Mining failed for {FoundSpelling}", lookupResult.PrimarySpelling);
return false;
}

if (needsAudio && (audioData is null || Utils.GetMd5String(audioData) is Networking.Jpod101NoAudioMd5Hash))
{
Utils.Frontend.Alert(AlertLevel.Warning, string.Create(CultureInfo.InvariantCulture, $"Mined {primarySpelling} (no audio)"));
Utils.Logger.Information("Mined {FoundSpelling} (no audio)", primarySpelling);
Utils.Frontend.Alert(AlertLevel.Warning, string.Create(CultureInfo.InvariantCulture, $"Mined {lookupResult.PrimarySpelling} (no audio)"));
Utils.Logger.Information("Mined {FoundSpelling} (no audio)", lookupResult.PrimarySpelling);
}

else
{
Utils.Frontend.Alert(AlertLevel.Success, string.Create(CultureInfo.InvariantCulture, $"Mined {primarySpelling}"));
Utils.Logger.Information("Mined {FoundSpelling}", primarySpelling);
Utils.Frontend.Alert(AlertLevel.Success, string.Create(CultureInfo.InvariantCulture, $"Mined {lookupResult.PrimarySpelling}"));
Utils.Logger.Information("Mined {FoundSpelling}", lookupResult.PrimarySpelling);
}

if (CoreConfig.ForceSyncAnki)
Expand Down
1 change: 1 addition & 0 deletions JL.Core/CoreConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public static class CoreConfig
{
public static Uri AnkiConnectUri { get; set; } = new("http://127.0.0.1:8765");
public static bool KanjiMode { get; set; } = false;
public static bool AnkiIntegration { get; set; } = false;
public static bool ForceSyncAnki { get; set; } = false;
public static bool AllowDuplicateCards { get; set; } = false;
public static int LookupRate { get; set; } = 0;
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/EDICT/JMdict/JmdictLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private static Sense ReadSense(XmlTextReader xmlReader)
{
lang = CultureInfo.GetCultureInfo(lang).EnglishName;
}
catch (Exception ex)
catch (CultureNotFoundException ex)
{
Utils.Logger.Error(ex, "Underlying OS cannot process the culture info");
s_canHandleCulture = false;
Expand Down
14 changes: 4 additions & 10 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static class ConfigManager
#region General

private static readonly ComboBoxItem[] s_japaneseFonts =
WindowsUtils.FindJapaneseFonts().OrderByDescending(static f => f.Foreground.ToString(CultureInfo.InvariantCulture)).ThenBy(static font => font.Content)
WindowsUtils.FindJapaneseFonts().OrderBy(static f => f.Foreground == Brushes.DimGray).ThenBy(static font => font.Content)
.ToArray();

private static readonly ComboBoxItem[] s_popupJapaneseFonts =
Expand Down Expand Up @@ -125,12 +125,6 @@ internal static class ConfigManager

#endregion

#region Anki

public static bool AnkiIntegration { get; set; } = false;

#endregion

#region Hotkeys

public static KeyGesture DisableHotkeysKeyGesture { get; private set; } = new(Key.Pause, ModifierKeys.Alt);
Expand Down Expand Up @@ -281,7 +275,7 @@ public static void ApplyPreferences()
WinApi.PreventActivation(mainWindow.WindowHandle);
}

AnkiIntegration = GetValueFromConfig(config, AnkiIntegration, nameof(AnkiIntegration), bool.TryParse);
CoreConfig.AnkiIntegration = GetValueFromConfig(config, CoreConfig.AnkiIntegration, nameof(CoreConfig.AnkiIntegration), bool.TryParse);
CoreConfig.KanjiMode = GetValueFromConfig(config, CoreConfig.KanjiMode, nameof(CoreConfig.KanjiMode), bool.TryParse);
CoreConfig.ForceSyncAnki = GetValueFromConfig(config, CoreConfig.ForceSyncAnki, nameof(CoreConfig.ForceSyncAnki), bool.TryParse);
CoreConfig.AllowDuplicateCards = GetValueFromConfig(config, CoreConfig.AllowDuplicateCards, nameof(CoreConfig.AllowDuplicateCards), bool.TryParse);
Expand Down Expand Up @@ -833,7 +827,7 @@ public static void LoadPreferences(PreferencesWindow preferenceWindow)
preferenceWindow.DisableHotkeysCheckBox.IsChecked = DisableHotkeys;
preferenceWindow.FocusableCheckBox.IsChecked = Focusable;
preferenceWindow.TextOnlyVisibleOnHoverCheckBox.IsChecked = TextOnlyVisibleOnHover;
preferenceWindow.AnkiIntegrationCheckBox.IsChecked = AnkiIntegration;
preferenceWindow.AnkiIntegrationCheckBox.IsChecked = CoreConfig.AnkiIntegration;
preferenceWindow.LookupRateNumericUpDown.Value = CoreConfig.LookupRate;

preferenceWindow.MainWindowDynamicWidthCheckBox.IsChecked = MainWindowDynamicWidth;
Expand Down Expand Up @@ -1140,7 +1134,7 @@ public static async Task SavePreferences(PreferencesWindow preferenceWindow)
settings[nameof(TextOnlyVisibleOnHover)].Value =
preferenceWindow.TextOnlyVisibleOnHoverCheckBox.IsChecked.ToString();

settings[nameof(AnkiIntegration)].Value =
settings[nameof(CoreConfig.AnkiIntegration)].Value =
preferenceWindow.AnkiIntegrationCheckBox.IsChecked.ToString();
settings[nameof(HighlightColor)].Value =
preferenceWindow.HighlightColorButton.Tag.ToString();
Expand Down
10 changes: 7 additions & 3 deletions JL.Windows/GUI/AddNameWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)
string? extraInfo = ExtraInfoTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
if (extraInfo.Length is 0)
{
reading = null;
extraInfo = null;
}

DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global"
Expand All @@ -80,6 +80,7 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)
Utils.Frontend.InvalidateDisplayCache();
}

PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
Close();

string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath);
Expand All @@ -90,7 +91,6 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
WindowsUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
WindowsUtils.UpdateMainWindowVisibility();
_ = MainWindow.Instance.Focus();
s_instance = null;
Expand All @@ -103,10 +103,14 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
{
_ = SpellingTextBox.Focus();
}
else // if (string.IsNullOrEmpty(ReadingTextBox.Text))
else if (string.IsNullOrEmpty(ReadingTextBox.Text))
{
_ = ReadingTextBox.Focus();
}
else // if (string.IsNullOrEmpty(ExtraInfoTextBox.Text))
{
_ = ExtraInfoTextBox.Focus();
}
}

private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/GUI/AddWordWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)
Utils.Frontend.InvalidateDisplayCache();
}

PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
Close();

string line = string.IsNullOrWhiteSpace(rawWordClasses)
Expand All @@ -108,7 +109,6 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
WindowsUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
WindowsUtils.UpdateMainWindowVisibility();
_ = MainWindow.Instance.Focus();
s_instance = null;
Expand Down
Loading

0 comments on commit 458b52a

Please sign in to comment.