Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 15, 2023
1 parent a9c9bfd commit af344e2
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 128 deletions.
42 changes: 21 additions & 21 deletions JL.Core/Dicts/EPWING/EpwingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ internal static class EpwingUtils
'→', '━'
};

public static bool IsValidEpwingResultForDictType(IEpwingRecord epwingRecord, Dict dict)
public static bool IsValidEpwingResultForDictType(string primarySpelling, string? reading, string[] definitions, Dict dict)
{
foreach (char c in epwingRecord.PrimarySpelling)
foreach (char c in primarySpelling)
{
if (s_invalidCharacters.Contains(c) || char.IsWhiteSpace(c))
{
Expand All @@ -40,27 +40,27 @@ public static bool IsValidEpwingResultForDictType(IEpwingRecord epwingRecord, Di
case DictType.Kenkyuusha:
if ((dict.Options?.Examples?.Value ?? ExamplesOptionValue.None) is ExamplesOptionValue.None)
{
if (epwingRecord.Definitions.Length > 2)
if (definitions.Length > 2)
{
for (int i = 2; i < epwingRecord.Definitions.Length; i++)
for (int i = 2; i < definitions.Length; i++)
{
if (!char.IsDigit(epwingRecord.Definitions[i][0]))
if (!char.IsDigit(definitions[i][0]))
{
epwingRecord.Definitions = epwingRecord.Definitions.RemoveAt(i);
definitions = definitions.RemoveAt(i);
--i;
}
}
}
}
else if (dict.Options is { Examples.Value: ExamplesOptionValue.One })
{
if (epwingRecord.Definitions.Length > 2)
if (definitions.Length > 2)
{
bool isMainExample = true;

for (int i = 2; i < epwingRecord.Definitions.Length; i++)
for (int i = 2; i < definitions.Length; i++)
{
if (char.IsDigit(epwingRecord.Definitions[i][0]))
if (char.IsDigit(definitions[i][0]))
{
isMainExample = true;
}
Expand All @@ -69,7 +69,7 @@ public static bool IsValidEpwingResultForDictType(IEpwingRecord epwingRecord, Di
{
if (!isMainExample)
{
epwingRecord.Definitions = epwingRecord.Definitions.RemoveAt(i);
definitions = definitions.RemoveAt(i);
--i;
}

Expand All @@ -79,60 +79,60 @@ public static bool IsValidEpwingResultForDictType(IEpwingRecord epwingRecord, Di
}
}

epwingRecord.Definitions = epwingRecord.Definitions.Select(static def => def.Replace("┏", "", StringComparison.Ordinal)).ToArray();
definitions = definitions.Select(static def => def.Replace("┏", "", StringComparison.Ordinal)).ToArray();

break;

case DictType.Daijisen:
// Kanji definitions
if (epwingRecord.Definitions.Any(static def => def.Contains("[音]", StringComparison.Ordinal)))
if (definitions.Any(static def => def.Contains("[音]", StringComparison.Ordinal)))
{
return false;
}

break;
}

return FilterDuplicateEntries(epwingRecord, dict);
return FilterDuplicateEntries(primarySpelling, reading, definitions, dict);
}

private static bool FilterDuplicateEntries(IEpwingRecord epwingRecord, Dict dict)
private static bool FilterDuplicateEntries(string primarySpelling, string? reading, string[] definitions, Dict dict)
{
if (dict.Contents.TryGetValue(
JapaneseUtils.KatakanaToHiragana(epwingRecord.PrimarySpelling),
JapaneseUtils.KatakanaToHiragana(primarySpelling),
out IList<IDictRecord>? previousResults))
{
for (int i = 0; i < previousResults.Count; i++)
{
IEpwingRecord previousResult = (IEpwingRecord)previousResults[i];

if (previousResult.Definitions.SequenceEqual(epwingRecord.Definitions))
if (previousResult.Definitions.SequenceEqual(definitions))
{
// If an entry has reading info while others don't, keep the one with the reading info.
if (string.IsNullOrEmpty(previousResult.Reading) &&
!string.IsNullOrEmpty(epwingRecord.Reading))
!string.IsNullOrEmpty(reading))
{
previousResults.RemoveAt(i);
break;
}

if (epwingRecord.Reading == previousResult.Reading)
if (reading == previousResult.Reading)
{
return false;
}
}
}
}

else if (epwingRecord.Reading is not null && dict.Contents.TryGetValue(
JapaneseUtils.KatakanaToHiragana(epwingRecord.Reading),
else if (reading is not null && dict.Contents.TryGetValue(
JapaneseUtils.KatakanaToHiragana(reading),
out previousResults))
{
for (int i = 0; i < previousResults.Count; i++)
{
IEpwingRecord previousResult = (IEpwingRecord)previousResults[i];

if (previousResult.Definitions.SequenceEqual(epwingRecord.Definitions))
if (previousResult.Definitions.SequenceEqual(definitions))
{
if (string.IsNullOrEmpty(previousResult.Reading))
{
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/EPWING/IEpwingRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ internal interface IEpwingRecord : IDictRecord
{
public string PrimarySpelling { get; }
public string? Reading { get; }
public string[] Definitions { get; set; }
public string[] Definitions { get; }
}
25 changes: 11 additions & 14 deletions JL.Core/Dicts/EPWING/Nazeka/EpwingNazekaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,41 @@ public static async Task Load(Dict dict)
string primarySpelling = spellingList[0];
string[]? alternativeSpellings = spellingList.RemoveAtToArray(0);

EpwingNazekaRecord tempRecord = new(primarySpelling, reading, alternativeSpellings, definitions);
if (!EpwingUtils.IsValidEpwingResultForDictType(tempRecord, dict))
if (!EpwingUtils.IsValidEpwingResultForDictType(primarySpelling, reading, definitions, dict))
{
continue;
}

AddRecordToDictionary(primarySpelling, tempRecord, nazekaEpwingDict);

if (dict.Type is not DictType.NonspecificNameNazeka)
EpwingNazekaRecord record = new(primarySpelling, reading, alternativeSpellings, definitions);
AddRecordToDictionary(primarySpelling, record, nazekaEpwingDict);
if (dict.Type is not DictType.NonspecificNameNazeka and not DictType.NonspecificKanjiNazeka)
{
AddRecordToDictionary(reading, tempRecord, nazekaEpwingDict);
AddRecordToDictionary(reading, record, nazekaEpwingDict);
}

for (int i = 1; i < spellingList.Count; i++)
{
primarySpelling = spellingList[i];
alternativeSpellings = spellingList.RemoveAtToArray(i);

tempRecord = new EpwingNazekaRecord(primarySpelling, reading, alternativeSpellings, definitions);
if (!EpwingUtils.IsValidEpwingResultForDictType(tempRecord, dict))
if (!EpwingUtils.IsValidEpwingResultForDictType(primarySpelling, reading, definitions, dict))
{
continue;
}

AddRecordToDictionary(primarySpelling, tempRecord, nazekaEpwingDict);
AddRecordToDictionary(primarySpelling, new EpwingNazekaRecord(primarySpelling, reading, alternativeSpellings, definitions), nazekaEpwingDict);
}
}

else
{
EpwingNazekaRecord tempRecord = new(reading, null, null, definitions);

if (!EpwingUtils.IsValidEpwingResultForDictType(tempRecord, dict))
if (!EpwingUtils.IsValidEpwingResultForDictType(reading, null, definitions, dict))
{
continue;
}

AddRecordToDictionary(reading, tempRecord, nazekaEpwingDict);
EpwingNazekaRecord record = new(reading, null, null, definitions);
AddRecordToDictionary(reading, record, nazekaEpwingDict);
}
}

Expand All @@ -122,7 +119,7 @@ public static async Task Load(Dict dict)
dict.Contents.TrimExcess();
}

private static void AddRecordToDictionary(string key, EpwingNazekaRecord record, Dictionary<string, IList<IDictRecord>> dictionary)
private static void AddRecordToDictionary(string key, IDictRecord record, Dictionary<string, IList<IDictRecord>> dictionary)
{
string keyInHiragana = JapaneseUtils.KatakanaToHiragana(key).GetPooledString();
if (dictionary.TryGetValue(keyInHiragana, out IList<IDictRecord>? result))
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/EPWING/Nazeka/EpwingNazekaRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class EpwingNazekaRecord : IEpwingRecord, IGetFrequency
public string PrimarySpelling { get; }
public string? Reading { get; }
public string[]? AlternativeSpellings { get; }
public string[] Definitions { get; set; }
public string[] Definitions { get; }

public EpwingNazekaRecord(string primarySpelling, string? reading, string[]? alternativeSpellings, string[] definitions)
{
Expand Down
69 changes: 56 additions & 13 deletions JL.Core/Dicts/EPWING/Yomichan/EpwingYomichanLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,73 @@ public static async Task Load(Dict dict)

foreach (string jsonFile in jsonFiles)
{
List<List<JsonElement>>? jsonObjects;
List<List<JsonElement>>? jsonElementLists;

FileStream fileStream = File.OpenRead(jsonFile);
await using (fileStream.ConfigureAwait(false))
{
jsonObjects = await JsonSerializer
jsonElementLists = await JsonSerializer
.DeserializeAsync<List<List<JsonElement>>>(fileStream)
.ConfigureAwait(false);
}

if (jsonObjects is null)
if (jsonElementLists is null)
{
continue;
}

foreach (List<JsonElement> jsonObj in jsonObjects)
foreach (List<JsonElement> jsonElements in jsonElementLists)
{
AddToDictionary(new EpwingYomichanRecord(jsonObj), dict);
string primarySpelling = jsonElements[0].GetString()!.GetPooledString();
string? reading = jsonElements[1].GetString();
if (string.IsNullOrEmpty(reading) || reading == primarySpelling)
{
reading = null;
}
else
{
reading = reading.GetPooledString();
}

string[]? definitions = EpwingYomichanRecord.GetDefinitions(jsonElements[5]);
definitions?.DeduplicateStringsInArray();

if (definitions is null
|| !EpwingUtils.IsValidEpwingResultForDictType(primarySpelling, reading, definitions, dict))
{
continue;
}

string[]? definitionTags = null;
JsonElement definitionTagsElement = jsonElements[2];
if (definitionTagsElement.ValueKind is JsonValueKind.String)
{
definitionTags = definitionTagsElement.GetString()!.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (definitionTags.Length is 0)
{
definitionTags = null;
}
else
{
definitionTags.DeduplicateStringsInArray();
}
}

string[]? wordClasses = jsonElements[3].GetString()!.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (wordClasses.Length is 0)
{
wordClasses = null;
}
else
{
wordClasses.DeduplicateStringsInArray();
}

//jsonElements[4].TryGetInt32(out int score);
//jsonElements[6].TryGetInt32(out int sequence);
//string[] termTags = jsonElements[7].ToString();

AddToDictionary(new EpwingYomichanRecord(primarySpelling, reading, definitions, wordClasses, definitionTags), dict);
}
}

Expand All @@ -50,14 +99,7 @@ public static async Task Load(Dict dict)

private static void AddToDictionary(IEpwingRecord yomichanRecord, Dict dict)
{
if (yomichanRecord.Definitions.Length is 0
|| !EpwingUtils.IsValidEpwingResultForDictType(yomichanRecord, dict))
{
return;
}

string hiraganaExpression = JapaneseUtils.KatakanaToHiragana(yomichanRecord.PrimarySpelling);

if (dict.Contents.TryGetValue(hiraganaExpression, out IList<IDictRecord>? records))
{
records.Add(yomichanRecord);
Expand All @@ -67,7 +109,8 @@ private static void AddToDictionary(IEpwingRecord yomichanRecord, Dict dict)
dict.Contents[hiraganaExpression] = new List<IDictRecord> { yomichanRecord };
}

if (dict.Type is not DictType.NonspecificNameYomichan && !string.IsNullOrEmpty(yomichanRecord.Reading))
if (dict.Type is not DictType.NonspecificNameYomichan and not DictType.NonspecificKanjiWithWordSchemaYomichan and not DictType.KanjigenYomichan
&& !string.IsNullOrEmpty(yomichanRecord.Reading))
{
string hiraganaReading = JapaneseUtils.KatakanaToHiragana(yomichanRecord.Reading);

Expand Down
Loading

0 comments on commit af344e2

Please sign in to comment.