From beedf9ae05a72d26cb4298f5a1553a582d22c154 Mon Sep 17 00:00:00 2001 From: rampaa Date: Sun, 6 Oct 2024 01:25:26 +0300 Subject: [PATCH] Minor --- JL.Core/Dicts/JMdict/JmdictRecord.cs | 52 ++++++++++---------- JL.Core/Freqs/FreqDBManager.cs | 35 +++++++------- JL.Core/Network/Networking.cs | 2 - JL.Core/Utilities/JapaneseUtils.cs | 72 ++++++++++++++-------------- JL.Windows/GUI/AddNameWindow.xaml.cs | 60 ++++++++++++----------- JL.Windows/GUI/AddWordWindow.xaml.cs | 70 ++++++++++++++------------- JL.Windows/Utilities/BacklogUtils.cs | 20 ++++---- 7 files changed, 159 insertions(+), 152 deletions(-) diff --git a/JL.Core/Dicts/JMdict/JmdictRecord.cs b/JL.Core/Dicts/JMdict/JmdictRecord.cs index 596ae626..793dbfe6 100644 --- a/JL.Core/Dicts/JMdict/JmdictRecord.cs +++ b/JL.Core/Dicts/JMdict/JmdictRecord.cs @@ -256,23 +256,25 @@ public int GetFrequency(Freq freq) } } - if (frequency is int.MaxValue && AlternativeSpellings is not null) + if (frequency is not int.MaxValue || AlternativeSpellings is null) { - for (int i = 0; i < AlternativeSpellings.Length; i++) + return frequency; + } + + for (int i = 0; i < AlternativeSpellings.Length; i++) + { + if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), + out IList? alternativeSpellingFreqResults)) { - if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), - out IList? alternativeSpellingFreqResults)) + int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count; + for (int j = 0; j < alternativeSpellingFreqResultCount; j++) { - int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count; - for (int j = 0; j < alternativeSpellingFreqResultCount; j++) + FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j]; + if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false) { - FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j]; - if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false) + if (frequency > alternativeSpellingFreqResult.Frequency) { - if (frequency > alternativeSpellingFreqResult.Frequency) - { - frequency = alternativeSpellingFreqResult.Frequency; - } + frequency = alternativeSpellingFreqResult.Frequency; } } } @@ -285,7 +287,6 @@ public int GetFrequency(Freq freq) for (int i = 0; i < Readings.Length; i++) { string reading = Readings[i]; - if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(reading), out IList? readingFreqResults)) { @@ -318,7 +319,6 @@ public int GetFrequencyFromDB(Dictionary> freqDict for (int i = 0; i < freqResultCount; i++) { FrequencyRecord freqResult = freqResults[i]; - if (PrimarySpelling == freqResult.Spelling || (Readings?.Contains(freqResult.Spelling) ?? false)) { if (frequency > freqResult.Frequency) @@ -328,22 +328,24 @@ public int GetFrequencyFromDB(Dictionary> freqDict } } - if (frequency is int.MaxValue && AlternativeSpellings is not null) + if (frequency is not int.MaxValue || AlternativeSpellings is null) + { + return frequency; + } + + for (int i = 0; i < AlternativeSpellings.Length; i++) { - for (int i = 0; i < AlternativeSpellings.Length; i++) + if (freqDict.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), out List? alternativeSpellingFreqResults)) { - if (freqDict.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), out List? alternativeSpellingFreqResults)) + int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count; + for (int j = 0; j < alternativeSpellingFreqResultCount; j++) { - int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count; - for (int j = 0; j < alternativeSpellingFreqResultCount; j++) + FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j]; + if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false) { - FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j]; - if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false) + if (frequency > alternativeSpellingFreqResult.Frequency) { - if (frequency > alternativeSpellingFreqResult.Frequency) - { - frequency = alternativeSpellingFreqResult.Frequency; - } + frequency = alternativeSpellingFreqResult.Frequency; } } } diff --git a/JL.Core/Freqs/FreqDBManager.cs b/JL.Core/Freqs/FreqDBManager.cs index 601dd811..3e82afdf 100644 --- a/JL.Core/Freqs/FreqDBManager.cs +++ b/JL.Core/Freqs/FreqDBManager.cs @@ -140,28 +140,29 @@ WHERE rsk.search_key IN (@1 } using SqliteDataReader dataReader = command.ExecuteReader(); - if (dataReader.HasRows) + if (!dataReader.HasRows) { - Dictionary> results = new(StringComparer.Ordinal); - while (dataReader.Read()) - { - FrequencyRecord record = GetRecord(dataReader); + return null; + } - string searchKey = dataReader.GetString(nameof(searchKey)); - if (results.TryGetValue(searchKey, out List? result)) - { - result.Add(record); - } - else - { - results[searchKey] = [record]; - } - } + Dictionary> results = new(StringComparer.Ordinal); + while (dataReader.Read()) + { + FrequencyRecord record = GetRecord(dataReader); - return results; + string searchKey = dataReader.GetString(nameof(searchKey)); + if (results.TryGetValue(searchKey, out List? result)) + { + result.Add(record); + } + else + { + results[searchKey] = [record]; + } } - return null; + return results; + } public static List? GetRecordsFromDB(string dbName, string term) diff --git a/JL.Core/Network/Networking.cs b/JL.Core/Network/Networking.cs index 6739ffc8..c9b076cb 100644 --- a/JL.Core/Network/Networking.cs +++ b/JL.Core/Network/Networking.cs @@ -59,8 +59,6 @@ public static async Task CheckForJLUpdates(bool isAutoCheck) foreach (JsonElement asset in assets.EnumerateArray()) { string latestReleaseUrl = asset.GetProperty("browser_download_url").GetString()!; - - // Add OS check? if (latestReleaseUrl.Contains(architecture, StringComparison.Ordinal)) { foundRelease = true; diff --git a/JL.Core/Utilities/JapaneseUtils.cs b/JL.Core/Utilities/JapaneseUtils.cs index a9cb5704..8e230170 100644 --- a/JL.Core/Utilities/JapaneseUtils.cs +++ b/JL.Core/Utilities/JapaneseUtils.cs @@ -356,56 +356,58 @@ internal static string FindSentence(string text, int position) ? text[startPosition..(endPosition + 1)].Trim() : ""; - if (sentence.Length > 1) + if (sentence.Length <= 1) { - if (s_rightToLeftBracketDict.ContainsKey(sentence[0])) + return sentence; + } + + if (s_rightToLeftBracketDict.ContainsKey(sentence[0])) + { + sentence = sentence[1..]; + } + + if (s_leftToRightBracketDict.ContainsKey(sentence.LastOrDefault())) + { + sentence = sentence[..^1]; + } + + if (s_leftToRightBracketDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket)) + { + if (sentence[^1] == rightBracket) { - sentence = sentence[1..]; + sentence = sentence[1..^1]; } - - if (s_leftToRightBracketDict.ContainsKey(sentence.LastOrDefault())) + else if (!sentence.Contains(rightBracket, StringComparison.Ordinal)) { - sentence = sentence[..^1]; + sentence = sentence[1..]; } - - if (s_leftToRightBracketDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket)) + else { - if (sentence[^1] == rightBracket) - { - sentence = sentence[1..^1]; - } - else if (!sentence.Contains(rightBracket, StringComparison.Ordinal)) + int numberOfLeftBrackets = sentence.Count(p => p == sentence[0]); + int numberOfRightBrackets = sentence.Count(p => p == rightBracket); + + if (numberOfLeftBrackets == numberOfRightBrackets + 1) { sentence = sentence[1..]; } - else - { - int numberOfLeftBrackets = sentence.Count(p => p == sentence[0]); - int numberOfRightBrackets = sentence.Count(p => p == rightBracket); - - if (numberOfLeftBrackets == numberOfRightBrackets + 1) - { - sentence = sentence[1..]; - } - } } + } - else if (s_rightToLeftBracketDict.TryGetValue(sentence.LastOrDefault(), out char leftBracket)) + else if (s_rightToLeftBracketDict.TryGetValue(sentence.LastOrDefault(), out char leftBracket)) + { + if (!sentence.Contains(leftBracket, StringComparison.Ordinal)) + { + sentence = sentence[..^1]; + } + else { - if (!sentence.Contains(leftBracket, StringComparison.Ordinal)) + int numberOfLeftBrackets = sentence.Count(p => p == leftBracket); + int numberOfRightBrackets = sentence.Count(p => p == sentence[^1]); + + if (numberOfRightBrackets == numberOfLeftBrackets + 1) { sentence = sentence[..^1]; } - else - { - int numberOfLeftBrackets = sentence.Count(p => p == leftBracket); - int numberOfRightBrackets = sentence.Count(p => p == sentence[^1]); - - if (numberOfRightBrackets == numberOfLeftBrackets + 1) - { - sentence = sentence[..^1]; - } - } } } diff --git a/JL.Windows/GUI/AddNameWindow.xaml.cs b/JL.Windows/GUI/AddNameWindow.xaml.cs index 3f562667..3392ed93 100644 --- a/JL.Windows/GUI/AddNameWindow.xaml.cs +++ b/JL.Windows/GUI/AddNameWindow.xaml.cs @@ -48,45 +48,47 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e) SpellingTextBox.ClearValue(BorderBrushProperty); } - if (isValid) + if (!isValid) { + return; + } + #pragma warning disable CA1308 // Normalize strings to uppercase - string nameType = - NameTypeStackPanel.Children.OfType() - .FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!.ToLowerInvariant(); + string nameType = + NameTypeStackPanel.Children.OfType() + .FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!.ToLowerInvariant(); #pragma warning restore CA1308 // Normalize strings to uppercase - string spelling = SpellingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); + string spelling = SpellingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); - string? reading = ReadingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); - if (reading.Length is 0 || reading == spelling) - { - reading = null; - } + string? reading = ReadingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); + if (reading.Length is 0 || reading == spelling) + { + reading = null; + } - string? extraInfo = ExtraInfoTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); - if (extraInfo.Length is 0) - { - extraInfo = null; - } + string? extraInfo = ExtraInfoTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim(); + if (extraInfo.Length is 0) + { + extraInfo = null; + } - DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global" - ? DictType.CustomNameDictionary - : DictType.ProfileCustomNameDictionary; + DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global" + ? DictType.CustomNameDictionary + : DictType.ProfileCustomNameDictionary; - Dict dict = DictUtils.SingleDictTypeDicts[dictType]; - if (dict.Active) - { - CustomNameLoader.AddToDictionary(spelling, reading, nameType, extraInfo, dict.Contents); - } + Dict dict = DictUtils.SingleDictTypeDicts[dictType]; + if (dict.Active) + { + CustomNameLoader.AddToDictionary(spelling, reading, nameType, extraInfo, dict.Contents); + } - PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow); - Close(); + PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow); + Close(); - string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath); - string line = $"{spelling}\t{reading}\t{nameType}\t{extraInfo}\n"; - await File.AppendAllTextAsync(path, line).ConfigureAwait(false); - } + string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath); + string line = $"{spelling}\t{reading}\t{nameType}\t{extraInfo}\n"; + await File.AppendAllTextAsync(path, line).ConfigureAwait(false); } private void Window_Closed(object sender, EventArgs e) diff --git a/JL.Windows/GUI/AddWordWindow.xaml.cs b/JL.Windows/GUI/AddWordWindow.xaml.cs index 765f8827..df13763c 100644 --- a/JL.Windows/GUI/AddWordWindow.xaml.cs +++ b/JL.Windows/GUI/AddWordWindow.xaml.cs @@ -61,49 +61,51 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e) DefinitionsTextBox.ClearValue(BorderBrushProperty); } - if (isValid) + if (!isValid) { - string rawSpellings = SpellingsTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); - string rawReadings = ReadingsTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); - string rawPartOfSpeech = PartOfSpeechStackPanel.Children.OfType() - .FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!; - string rawWordClasses = WordClassTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); + return; + } - string[] spellings = rawSpellings.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + string rawSpellings = SpellingsTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); + string rawReadings = ReadingsTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); + string rawPartOfSpeech = PartOfSpeechStackPanel.Children.OfType() + .FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!; + string rawWordClasses = WordClassTextBox.Text.Replace("\t", " ", StringComparison.Ordinal); - string[]? readings = rawReadings.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); - if (readings.Length is 0 - || (spellings.Length is 1 && readings.Length is 1 && spellings[0] == readings[0])) - { - readings = null; - } + string[] spellings = rawSpellings.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); - string[]? wordClasses = rawWordClasses.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); - if (wordClasses.Length is 0) - { - wordClasses = null; - } + string[]? readings = rawReadings.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + if (readings.Length is 0 + || (spellings.Length is 1 && readings.Length is 1 && spellings[0] == readings[0])) + { + readings = null; + } - DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global" - ? DictType.CustomWordDictionary - : DictType.ProfileCustomWordDictionary; + string[]? wordClasses = rawWordClasses.Split(';', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); + if (wordClasses.Length is 0) + { + wordClasses = null; + } - Dict dict = DictUtils.SingleDictTypeDicts[dictType]; - if (dict.Active) - { - CustomWordLoader.AddToDictionary(spellings, readings, definitions, rawPartOfSpeech, wordClasses, dict.Contents); - } + DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global" + ? DictType.CustomWordDictionary + : DictType.ProfileCustomWordDictionary; + + Dict dict = DictUtils.SingleDictTypeDicts[dictType]; + if (dict.Active) + { + CustomWordLoader.AddToDictionary(spellings, readings, definitions, rawPartOfSpeech, wordClasses, dict.Contents); + } - PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow); - Close(); + PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow); + Close(); - string line = string.IsNullOrWhiteSpace(rawWordClasses) - ? $"{rawSpellings}\t{rawReadings}\t{rawDefinitions}\t{rawPartOfSpeech}\n" - : $"{rawSpellings}\t{rawReadings}\t{rawDefinitions}\t{rawPartOfSpeech}\t{rawWordClasses}\n"; + string line = string.IsNullOrWhiteSpace(rawWordClasses) + ? $"{rawSpellings}\t{rawReadings}\t{rawDefinitions}\t{rawPartOfSpeech}\n" + : $"{rawSpellings}\t{rawReadings}\t{rawDefinitions}\t{rawPartOfSpeech}\t{rawWordClasses}\n"; - string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath); - await File.AppendAllTextAsync(path, line).ConfigureAwait(false); - } + string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath); + await File.AppendAllTextAsync(path, line).ConfigureAwait(false); } private void Window_Closed(object sender, EventArgs e) diff --git a/JL.Windows/Utilities/BacklogUtils.cs b/JL.Windows/Utilities/BacklogUtils.cs index b4241b89..c97ec92f 100644 --- a/JL.Windows/Utilities/BacklogUtils.cs +++ b/JL.Windows/Utilities/BacklogUtils.cs @@ -131,19 +131,19 @@ public static void ShowAllBacklog() public static Task WriteBacklog() { - if (ConfigManager.EnableBacklog - && ConfigManager.AutoSaveBacklogBeforeClosing - && Backlog.Count > 0) + if (!ConfigManager.EnableBacklog + || !ConfigManager.AutoSaveBacklogBeforeClosing + || Backlog.Count is 0) { - string directory = Path.Join(Utils.ApplicationPath, "Backlogs"); - if (!Directory.Exists(directory)) - { - _ = Directory.CreateDirectory(directory); - } + return Task.CompletedTask; + } - return File.WriteAllLinesAsync(Path.Join(directory, string.Create(CultureInfo.InvariantCulture, $"{ProfileUtils.CurrentProfileName}_{Process.GetCurrentProcess().StartTime:yyyy.MM.dd_HH.mm.ss}-{DateTime.Now:yyyy.MM.dd_HH.mm.ss}.txt")), Backlog); + string directory = Path.Join(Utils.ApplicationPath, "Backlogs"); + if (!Directory.Exists(directory)) + { + _ = Directory.CreateDirectory(directory); } - return Task.CompletedTask; + return File.WriteAllLinesAsync(Path.Join(directory, string.Create(CultureInfo.InvariantCulture, $"{ProfileUtils.CurrentProfileName}_{Process.GetCurrentProcess().StartTime:yyyy.MM.dd_HH.mm.ss}-{DateTime.Now:yyyy.MM.dd_HH.mm.ss}.txt")), Backlog); } }