Skip to content

Commit

Permalink
Only store and show a single list of PoS/misc/field info if they are …
Browse files Browse the repository at this point in the history
…the same for all gloss senses
  • Loading branch information
rampaa committed Feb 1, 2025
1 parent 3543b40 commit b991c01
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 58 deletions.
2 changes: 1 addition & 1 deletion JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static class EpwingYomichanUtils
}
}

return definitions.TrimListToArray();
return definitions.TrimToArray();
}

private static string? GetDefinitionsFromJsonArray(JsonElement jsonElement, string? parentTag = null)
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/JMdict/JmdictDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
53 changes: 47 additions & 6 deletions JL.Core/Dicts/JMdict/JmdictRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -87,14 +100,42 @@ 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)
{
_ = defResult.Append(CultureInfo.InvariantCulture, $"({i + 1}) ");
}

if (showWordClassInfo)
if (!showFirstWordClassInfo && showWordClassInfo)
{
_ = defResult.Append(CultureInfo.InvariantCulture, $"({string.Join(", ", WordClasses[i])}) ");
}
Expand All @@ -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)
Expand All @@ -113,7 +154,7 @@ public string BuildFormattedDefinition(DictOptions options)
}
}

if (showDialectInfo)
if (!showFirstDialectInfo && showDialectInfo)
{
string[]? dialects = Dialects![i];
if (dialects is not null)
Expand All @@ -122,7 +163,7 @@ public string BuildFormattedDefinition(DictOptions options)
}
}

if (showWordTypeInfo)
if (!showFirstWordTypeInfo && showWordTypeInfo)
{
string[]? fields = Fields![i];
if (fields is not null)
Expand Down
82 changes: 41 additions & 41 deletions JL.Core/Dicts/JMdict/JmdictRecordBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary<string, ILi
{
List<KanjiElement> 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;
Expand Down Expand Up @@ -73,7 +73,7 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary<string, ILi
&& (readingElement.ReRestrList.Count is 0 || readingElement.ReRestrList.Contains(kanjiElement.Keb)))
{
readingList.Add(readingElement.Reb);
readingsOrthographyInfoList.Add(readingElement.ReInfList.TrimListToArray());
readingsOrthographyInfoList.Add(readingElement.ReInfList.TrimToArray());
}
}

Expand All @@ -98,36 +98,36 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary<string, ILi
{
definitionList.Add(sense.GlossList.ToArray());
wordClassList.Add(sense.PosList.ToArray());
readingRestrictionList.Add(sense.StagRList.TrimListToArray());
spellingRestrictionList.Add(sense.StagKList.TrimListToArray());
fieldList.Add(sense.FieldList.TrimListToArray());
miscList.Add(sense.MiscList.TrimListToArray());
dialectList.Add(sense.DialList.TrimListToArray());
readingRestrictionList.Add(sense.StagRList.TrimToArray());
spellingRestrictionList.Add(sense.StagKList.TrimToArray());
fieldList.Add(sense.FieldList.TrimToArray());
miscList.Add(sense.MiscList.TrimToArray());
dialectList.Add(sense.DialList.TrimToArray());
definitionInfoList.Add(sense.SInf);
relatedTermList.Add(sense.XRefList.TrimListToArray());
antonymList.Add(sense.AntList.TrimListToArray());
loanwordSourceList.Add(sense.LSourceList.TrimListToArray());
relatedTermList.Add(sense.XRefList.TrimToArray());
antonymList.Add(sense.AntList.TrimToArray());
loanwordSourceList.Add(sense.LSourceList.TrimToArray());
}
}

JmdictRecord record = new(entry.Id,
kanjiElement.Keb,
definitionList.ToArray(),
wordClassList.ToArray(),
wordClassList.ToSingleElementArrayIfIdentical(),
allKanjiOrthographyInfoWithoutSearchOnlyForms[index],
allSpellingsWithoutSearchOnlyForms.RemoveAt(index),
allKanjiOrthographyInfoWithoutSearchOnlyForms.RemoveAt(index),
readingList.TrimListToArray(),
readingsOrthographyInfoList.TrimListWithNullableElementsToArray(),
spellingRestrictionList.TrimListWithNullableElementsToArray(),
readingRestrictionList.TrimListWithNullableElementsToArray(),
fieldList.TrimListWithNullableElementsToArray(),
miscList.TrimListWithNullableElementsToArray(),
definitionInfoList.TrimListWithNullableElementsToArray(),
dialectList.TrimListWithNullableElementsToArray(),
loanwordSourceList.TrimListWithNullableElementsToArray(),
relatedTermList.TrimListWithNullableElementsToArray(),
antonymList.TrimListWithNullableElementsToArray());
readingList.TrimToArray(),
readingsOrthographyInfoList.TrimListOfNullableElementsToArray(),
spellingRestrictionList.TrimListOfNullableElementsToArray(),
readingRestrictionList.TrimListOfNullableElementsToArray(),
fieldList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
miscList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
definitionInfoList.TrimListOfNullableElementsToArray(),
dialectList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
loanwordSourceList.TrimListOfNullableElementsToArray(),
relatedTermList.TrimListOfNullableElementsToArray(),
antonymList.TrimListOfNullableElementsToArray());

recordDictionary.Add(key, record);

Expand All @@ -140,7 +140,7 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary<string, ILi
if (readingElementsWithoutSearchOnlyFormsExist)
{
string[] allReadingsWithoutSearchOnlyForms = readingElementsWithoutSearchOnlyForms.Select(static rEle => 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;
Expand Down Expand Up @@ -231,36 +231,36 @@ public static void AddToDictionary(in JmdictEntry entry, IDictionary<string, ILi
{
definitionList.Add(sense.GlossList.ToArray());
wordClassList.Add(sense.PosList.ToArray());
readingRestrictionList.Add(sense.StagRList.TrimListToArray());
spellingRestrictionList.Add(sense.StagKList.TrimListToArray());
fieldList.Add(sense.FieldList.TrimListToArray());
miscList.Add(sense.MiscList.TrimListToArray());
dialectList.Add(sense.DialList.TrimListToArray());
readingRestrictionList.Add(sense.StagRList.TrimToArray());
spellingRestrictionList.Add(sense.StagKList.TrimToArray());
fieldList.Add(sense.FieldList.TrimToArray());
miscList.Add(sense.MiscList.TrimToArray());
dialectList.Add(sense.DialList.TrimToArray());
definitionInfoList.Add(sense.SInf);
relatedTermList.Add(sense.XRefList.TrimListToArray());
antonymList.Add(sense.AntList.TrimListToArray());
loanwordSourceList.Add(sense.LSourceList.TrimListToArray());
relatedTermList.Add(sense.XRefList.TrimToArray());
antonymList.Add(sense.AntList.TrimToArray());
loanwordSourceList.Add(sense.LSourceList.TrimToArray());
}
}

JmdictRecord record = new(entry.Id,
primarySpelling,
definitionList.ToArray(),
wordClassList.ToArray(),
wordClassList.ToSingleElementArrayIfIdentical(),
primarySpellingOrthographyInfo,
alternativeSpellings,
alternativeSpellingsOrthographyInfo,
readings,
readingsOrthographyInfo,
spellingRestrictionList.TrimListWithNullableElementsToArray(),
readingRestrictionList.TrimListWithNullableElementsToArray(),
fieldList.TrimListWithNullableElementsToArray(),
miscList.TrimListWithNullableElementsToArray(),
definitionInfoList.TrimListWithNullableElementsToArray(),
dialectList.TrimListWithNullableElementsToArray(),
loanwordSourceList.TrimListWithNullableElementsToArray(),
relatedTermList.TrimListWithNullableElementsToArray(),
antonymList.TrimListWithNullableElementsToArray());
spellingRestrictionList.TrimListOfNullableElementsToArray(),
readingRestrictionList.TrimListOfNullableElementsToArray(),
fieldList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
miscList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
definitionInfoList.TrimListOfNullableElementsToArray(),
dialectList.TrimListOfNullableElementsToSingleElementArrayIfIdentical(),
loanwordSourceList.TrimListOfNullableElementsToArray(),
relatedTermList.TrimListOfNullableElementsToArray(),
antonymList.TrimListOfNullableElementsToArray());

// record.Priorities = kanjiElement.KePriList

Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/JMnedict/JmnedictRecordBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void AddToDictionary(in JmnedictEntry entry, IDictionary<string, I
// relatedTermsArray[j] = translation.XRefList.TrimListToArray();
}

JmnedictRecord record = new(entry.Id, entry.KebList[i], entry.KebList.RemoveAtToArray(i), entry.RebList.TrimListToArray(), definitionsArray, nameTypesArray);
JmnedictRecord record = new(entry.Id, entry.KebList[i], entry.KebList.RemoveAtToArray(i), entry.RebList.TrimToArray(), definitionsArray, nameTypesArray);
// record.RelatedTerms = relatedTermsArray;

recordDictionary.Add(key, record);
Expand Down
10 changes: 5 additions & 5 deletions JL.Core/Dicts/KANJIDIC/KanjidicLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ private static async Task ReadCharacter(XmlReader xmlReader, IDictionary<string,
}
}

string[]? definitions = definitionList.TrimListToArray();
string[]? onReadings = onReadingList.TrimListToArray();
string[]? kunReadings = kunReadingList.TrimListToArray();
string[]? nanoriReadings = nanoriReadingList.TrimListToArray();
string[]? radicalNames = radicalNameList.TrimListToArray();
string[]? definitions = definitionList.TrimToArray();
string[]? onReadings = onReadingList.TrimToArray();
string[]? kunReadings = kunReadingList.TrimToArray();
string[]? nanoriReadings = nanoriReadingList.TrimToArray();
string[]? radicalNames = radicalNameList.TrimToArray();

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

Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/KanjiDict/YomichanKanjiRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public YomichanKanjiRecord(List<JsonElement> jsonElement)
}
}

Definitions = definitionList.TrimListToArray();
Definitions = definitionList.TrimToArray();

JsonElement statsElement = jsonElement[5];
int statsElementPropertyCount = statsElement.GetPropertyCount();
Expand Down
69 changes: 67 additions & 2 deletions JL.Core/Utilities/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ internal static void AddRange<T>(this ConcurrentBag<T> source, IEnumerable<T> it
return array;
}

internal static T[]? TrimListToArray<T>(this List<T> list) where T : notnull
internal static T[]? TrimToArray<T>(this List<T> list) where T : notnull
{
return list.Count is 0
? null
: list.ToArray();
}

internal static T?[]? TrimListWithNullableElementsToArray<T>(this List<T?> list) where T : class
internal static T?[]? TrimListOfNullableElementsToArray<T>(this List<T?> list) where T : class
{
if (list.Count is 0)
{
Expand All @@ -183,6 +183,71 @@ internal static void AddRange<T>(this ConcurrentBag<T> source, IEnumerable<T> it
: list.ToArray();
}

internal static T[][] ToSingleElementArrayIfIdentical<T>(this List<T[]> 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<T>(this List<T[]?> 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);
Expand Down

0 comments on commit b991c01

Please sign in to comment.