From b991c01dc700f5717230faa9e96808d01f7cf072 Mon Sep 17 00:00:00 2001 From: rampaa Date: Sat, 1 Feb 2025 21:02:16 +0300 Subject: [PATCH] Only store and show a single list of PoS/misc/field info if they are the same for all gloss senses --- .../EPWING/Yomichan/EpwingYomichanUtils.cs | 2 +- JL.Core/Dicts/JMdict/JmdictDBManager.cs | 2 +- JL.Core/Dicts/JMdict/JmdictRecord.cs | 53 ++++++++++-- JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs | 82 +++++++++---------- .../Dicts/JMnedict/JmnedictRecordBuilder.cs | 2 +- JL.Core/Dicts/KANJIDIC/KanjidicLoader.cs | 10 +-- .../Dicts/KanjiDict/YomichanKanjiRecord.cs | 2 +- JL.Core/Utilities/ExtensionMethods.cs | 69 +++++++++++++++- 8 files changed, 164 insertions(+), 58 deletions(-) diff --git a/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanUtils.cs b/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanUtils.cs index a4e577a5..e34ab974 100644 --- a/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanUtils.cs +++ b/JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanUtils.cs @@ -31,7 +31,7 @@ internal static class EpwingYomichanUtils } } - return definitions.TrimListToArray(); + return definitions.TrimToArray(); } private static string? GetDefinitionsFromJsonArray(JsonElement jsonElement, string? parentTag = null) diff --git a/JL.Core/Dicts/JMdict/JmdictDBManager.cs b/JL.Core/Dicts/JMdict/JmdictDBManager.cs index fdc9089f..b73f67e1 100644 --- a/JL.Core/Dicts/JMdict/JmdictDBManager.cs +++ b/JL.Core/Dicts/JMdict/JmdictDBManager.cs @@ -10,7 +10,7 @@ namespace JL.Core.Dicts.JMdict; internal static class JmdictDBManager { - public const int Version = 3; + public const int Version = 4; public static void CreateDB(string dbName) { diff --git a/JL.Core/Dicts/JMdict/JmdictRecord.cs b/JL.Core/Dicts/JMdict/JmdictRecord.cs index a41b6b87..b55abb86 100644 --- a/JL.Core/Dicts/JMdict/JmdictRecord.cs +++ b/JL.Core/Dicts/JMdict/JmdictRecord.cs @@ -74,11 +74,24 @@ public string BuildFormattedDefinition(DictOptions options) char separator = newlines ? '\n' : ';'; bool multipleDefinitions = Definitions.Length > 1; + bool showWordClassInfo = options.WordClassInfo!.Value; - bool showDialectInfo = options.DialectInfo!.Value && Dialects is not null; - bool showExtraDefinitionInfo = options.ExtraDefinitionInfo!.Value && DefinitionInfo is not null; + bool showFirstWordClassInfo = showWordClassInfo && Definitions.Length > WordClasses.Length; + string[]? firstWordClass = showFirstWordClassInfo ? WordClasses[0] : null; + bool showMiscInfo = options.MiscInfo!.Value && Misc is not null; + bool showFirstMiscInfo = showMiscInfo && Definitions.Length > Misc!.Length; + string[]? firstMiscInfo = showFirstMiscInfo ? Misc![0] : null; + + bool showDialectInfo = options.DialectInfo!.Value && Dialects is not null; + bool showFirstDialectInfo = showDialectInfo && Definitions.Length > Dialects!.Length; + string[]? firstDialect = showFirstDialectInfo ? Dialects![0] : null; + bool showWordTypeInfo = options.WordTypeInfo!.Value && Fields is not null; + bool showFirstWordTypeInfo = showWordTypeInfo && Definitions.Length > Fields!.Length; + string[]? firstWordTypeInfo = showFirstWordTypeInfo ? Fields![0] : null; + + bool showExtraDefinitionInfo = options.ExtraDefinitionInfo!.Value && DefinitionInfo is not null; bool showSpellingRestrictionInfo = options.SpellingRestrictionInfo!.Value; bool showSpellingRestrictions = showSpellingRestrictionInfo && SpellingRestrictions is not null; bool showReadingRestrictionss = showSpellingRestrictionInfo && ReadingRestrictions is not null; @@ -87,6 +100,34 @@ public string BuildFormattedDefinition(DictOptions options) bool showAntonyms = options.Antonym!.Value && Antonyms is not null; StringBuilder defResult = new(); + if (showFirstWordClassInfo || showFirstMiscInfo || showFirstDialectInfo || showFirstWordTypeInfo) + { + if (showFirstWordClassInfo) + { + _ = defResult.Append(CultureInfo.InvariantCulture, $"[{string.Join(", ", firstWordClass!)}] "); + } + + if (showFirstMiscInfo) + { + _ = defResult.Append(CultureInfo.InvariantCulture, $"[{string.Join(", ", firstMiscInfo!)}] "); + } + + if (showFirstDialectInfo) + { + _ = defResult.Append(CultureInfo.InvariantCulture, $"[{string.Join(", ", firstDialect!)}] "); + } + + if (showFirstWordTypeInfo) + { + _ = defResult.Append(CultureInfo.InvariantCulture, $"[{string.Join(", ", firstWordTypeInfo!)}] "); + } + + if (newlines) + { + _ = defResult.Replace(" ", "\n", defResult.Length - 1, 1); + } + } + for (int i = 0; i < Definitions.Length; i++) { if (newlines && multipleDefinitions) @@ -94,7 +135,7 @@ public string BuildFormattedDefinition(DictOptions options) _ = defResult.Append(CultureInfo.InvariantCulture, $"({i + 1}) "); } - if (showWordClassInfo) + if (!showFirstWordClassInfo && showWordClassInfo) { _ = defResult.Append(CultureInfo.InvariantCulture, $"({string.Join(", ", WordClasses[i])}) "); } @@ -104,7 +145,7 @@ public string BuildFormattedDefinition(DictOptions options) _ = defResult.Append(CultureInfo.InvariantCulture, $"({i + 1}) "); } - if (showMiscInfo) + if (!showFirstMiscInfo && showMiscInfo) { string[]? misc = Misc![i]; if (misc is not null) @@ -113,7 +154,7 @@ public string BuildFormattedDefinition(DictOptions options) } } - if (showDialectInfo) + if (!showFirstDialectInfo && showDialectInfo) { string[]? dialects = Dialects![i]; if (dialects is not null) @@ -122,7 +163,7 @@ public string BuildFormattedDefinition(DictOptions options) } } - if (showWordTypeInfo) + if (!showFirstWordTypeInfo && showWordTypeInfo) { string[]? fields = Fields![i]; if (fields is not null) diff --git a/JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs b/JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs index 3b9ca937..1648e2b8 100644 --- a/JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs +++ b/JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs @@ -9,7 +9,7 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary kanjiElementsWithoutSearchOnlyForms = entry.KanjiElements.Where(static ke => !ke.KeInfList.Contains("sK")).ToList(); string[] allSpellingsWithoutSearchOnlyForms = kanjiElementsWithoutSearchOnlyForms.Select(static ke => ke.Keb).ToArray(); - string[]?[] allKanjiOrthographyInfoWithoutSearchOnlyForms = kanjiElementsWithoutSearchOnlyForms.Select(static ke => ke.KeInfList.TrimListToArray()).ToArray(); + string[]?[] allKanjiOrthographyInfoWithoutSearchOnlyForms = kanjiElementsWithoutSearchOnlyForms.Select(static ke => ke.KeInfList.TrimToArray()).ToArray(); string? firstPrimarySpelling; string[]? alternativeSpellingsForFirstPrimarySpelling; @@ -73,7 +73,7 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary rEle.Reb).ToArray(); - string[]?[] allROrthographyInfoWithoutSearchOnlyForms = readingElementsWithoutSearchOnlyForms.Select(static rEle => rEle.ReInfList.TrimListToArray()).ToArray(); + string[]?[] allROrthographyInfoWithoutSearchOnlyForms = readingElementsWithoutSearchOnlyForms.Select(static rEle => rEle.ReInfList.TrimToArray()).ToArray(); string firstReadingInHiragana = JapaneseUtils.KatakanaToHiragana(allReadingsWithoutSearchOnlyForms[0]); index = 0; @@ -231,36 +231,36 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary jsonElement) } } - Definitions = definitionList.TrimListToArray(); + Definitions = definitionList.TrimToArray(); JsonElement statsElement = jsonElement[5]; int statsElementPropertyCount = statsElement.GetPropertyCount(); diff --git a/JL.Core/Utilities/ExtensionMethods.cs b/JL.Core/Utilities/ExtensionMethods.cs index f974ba23..ec7b68cd 100644 --- a/JL.Core/Utilities/ExtensionMethods.cs +++ b/JL.Core/Utilities/ExtensionMethods.cs @@ -154,14 +154,14 @@ internal static void AddRange(this ConcurrentBag source, IEnumerable it return array; } - internal static T[]? TrimListToArray(this List list) where T : notnull + internal static T[]? TrimToArray(this List list) where T : notnull { return list.Count is 0 ? null : list.ToArray(); } - internal static T?[]? TrimListWithNullableElementsToArray(this List list) where T : class + internal static T?[]? TrimListOfNullableElementsToArray(this List list) where T : class { if (list.Count is 0) { @@ -183,6 +183,71 @@ internal static void AddRange(this ConcurrentBag source, IEnumerable it : list.ToArray(); } + internal static T[][] ToSingleElementArrayIfIdentical(this List list) where T : class + { + if (list.Count is 1) + { + return list.ToArray(); + } + + T[] firstElemenmt = list[0]; + for (int i = 1; i < list.Count; i++) + { + if (!list[i].SequenceEqual(firstElemenmt)) + { + return list.ToArray(); + } + } + + return [firstElemenmt]; + } + + internal static T[]?[]? TrimListOfNullableElementsToSingleElementArrayIfIdentical(this List list) where T : class + { + if (list.Count is 0) + { + return null; + } + + T[]? firstElement = list[0]; + if (list.Count is 1) + { + return firstElement is not null + ? [firstElement] + : null; + } + + bool allElementsAreNull = true; + for (int i = 0; i < list.Count; i++) + { + if (list[i] is not null) + { + allElementsAreNull = false; + break; + } + } + + bool allElementsAreIdentical = firstElement is not null; + if (!allElementsAreNull && allElementsAreIdentical) + { + for (int i = 1; i < list.Count; i++) + { + T[]? currentItem = list[i]; + if (currentItem is null || !currentItem.SequenceEqual(firstElement!)) + { + allElementsAreIdentical = false; + break; + } + } + } + + return allElementsAreNull + ? null + : allElementsAreIdentical + ? [firstElement] + : list.ToArray(); + } + internal static string GetPooledString(this string str) { return Utils.StringPoolInstance.GetOrAdd(str);