Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 23, 2023
1 parent 97b61f8 commit 7d6d691
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
4 changes: 2 additions & 2 deletions JL.Core/Dicts/CustomWordDict/CustomWordLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public static void AddToDictionary(string[] spellings, string[]? readings, strin
return;
}

if (i is 0)
if (i is 0 && readings is not null)
{
for (int j = 0; j < readings?.Length; j++)
for (int j = 0; j < readings.Length; j++)
{
if (!AddRecordToDictionary(readings[j], newWordRecord, customWordDictionary))
{
Expand Down
3 changes: 3 additions & 0 deletions JL.Core/Lookup/LookupResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class LookupResult
public string[]? PrimarySpellingOrthographyInfoList { get; }
public string[]?[]? ReadingsOrthographyInfoList { get; }
public string[]?[]? AlternativeSpellingsOrthographyInfoList { get; }
public string[]?[]? MiscList { get; }

// Kanji
public string[]? OnReadings { get; }
Expand All @@ -47,6 +48,7 @@ internal LookupResult(
string[]? primarySpellingOrthographyInfoList = null,
string[]?[]? readingsOrthographyInfoList = null,
string[]?[]? alternativeSpellingsOrthographyInfoList = null,
string[]?[]? miscList = null,
string[]? onReadings = null,
string[]? kunReadings = null,
string[]? nanoriReadings = null,
Expand Down Expand Up @@ -74,6 +76,7 @@ internal LookupResult(
PrimarySpellingOrthographyInfoList = primarySpellingOrthographyInfoList;
ReadingsOrthographyInfoList = readingsOrthographyInfoList;
AlternativeSpellingsOrthographyInfoList = alternativeSpellingsOrthographyInfoList;
MiscList = miscList;
OnReadings = onReadings;
KunReadings = kunReadings;
NanoriReadings = nanoriReadings;
Expand Down
70 changes: 51 additions & 19 deletions JL.Core/Lookup/LookupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,23 @@ private static List<LookupResult> SortLookupResults(IEnumerable<LookupResult> lo
return lookupResults
.OrderByDescending(static lookupResult => lookupResult.MatchedText.Length)
.ThenByDescending(static lookupResult => lookupResult.PrimarySpelling == lookupResult.MatchedText)
.ThenByDescending(static lookupResult => lookupResult.Readings?.Contains(lookupResult.MatchedText) ?? false)
.ThenBy(static lookupResult => lookupResult.Dict.Priority)
.ThenBy(static lookupResult =>
{
if (lookupResult.PrimarySpellingOrthographyInfoList is not null)
{
for (int i = 0; i < lookupResult.PrimarySpellingOrthographyInfoList.Length; i++)
{
if (lookupResult.PrimarySpellingOrthographyInfoList[i] is "oK" or "iK" or "rK")
{
return 1;
}
}
}

return 0;
})
.ThenBy(static lookupResult =>
{
int index = lookupResult.Readings is not null
Expand All @@ -310,37 +327,46 @@ private static List<LookupResult> SortLookupResults(IEnumerable<LookupResult> lo
return 3;
}

if (lookupResult.ReadingsOrthographyInfoList is not null)
if (lookupResult.MiscList is not null)
{
string[]? readingsOrthographyInfo = lookupResult.ReadingsOrthographyInfoList[index];
for (int i = 0; i < readingsOrthographyInfo?.Length; i++)
for (int i = 0; i < lookupResult.MiscList.Length; i++)
{
string roi = readingsOrthographyInfo[i];
if (roi is "uk")
if (lookupResult.MiscList[i]?.Contains("uk") ?? false)
{
return 0;
}
if (roi is "ok" or "ik" or "rk")
}
}

if (lookupResult.ReadingsOrthographyInfoList is not null)
{
string[]? readingsOrthographyInfo = lookupResult.ReadingsOrthographyInfoList[index];
if (readingsOrthographyInfo is not null)
{
for (int i = 0; i < readingsOrthographyInfo.Length; i++)
{
return 2;
if (readingsOrthographyInfo[i] is "ok" or "ik" or "rk")
{
return 2;
}
}
}
}

return 1;
})
.ThenBy(static lookupResult => lookupResult.Dict.Priority)
.ThenBy(static lookupResult => lookupResult.Frequencies?.Count > 0 ? lookupResult.Frequencies[0].Freq : int.MaxValue)
.ThenBy(static lookupResult =>
{
int index = lookupResult.Readings is not null
? Array.IndexOf(lookupResult.Readings, lookupResult.MatchedText)
: -1;

return index is not -1
? index
: int.MaxValue;
})
//.ThenBy(static lookupResult =>
//{
// int index = lookupResult.Readings is not null
// ? Array.IndexOf(lookupResult.Readings, lookupResult.MatchedText)
// : -1;

// return index is not -1
// ? index
// : int.MaxValue;
//})
//.ThenByDescending(static lookupResult => lookupResult.PrimarySpelling.Length)
.ToList();
}

Expand Down Expand Up @@ -884,6 +910,7 @@ private static List<LookupResult> BuildJmdictResult(
primarySpellingOrthographyInfoList: jmdictResult.PrimarySpellingOrthographyInfo,
readingsOrthographyInfoList: jmdictResult.ReadingsOrthographyInfo,
alternativeSpellingsOrthographyInfoList: jmdictResult.AlternativeSpellingsOrthographyInfo,
miscList: jmdictResult.Misc,
dict: wordResult.Dict,
formattedDefinitions: jmdictResult.BuildFormattedDefinition(wordResult.Dict.Options),
pitchAccentDict: pitchAccentDict
Expand Down Expand Up @@ -1321,10 +1348,15 @@ private static List<LookupFrequencyResult> GetKanjidicFrequencies(string kanji,

private static string? ProcessDeconjugationProcess(List<List<string>>? processList)
{
if (processList is null)
{
return null;
}

StringBuilder deconjugation = new();
bool first = true;

for (int i = 0; i < processList?.Count; i++)
for (int i = 0; i < processList.Count; i++)
{
List<string> form = processList[i];

Expand Down
13 changes: 13 additions & 0 deletions JL.Core/Utilities/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,17 @@ internal static void DeduplicateStringsInArray(this string[] strings)
strings[i] = strings[i].GetPooledString();
}
}

internal static bool Contains<T>(this T[] source, T item) where T : class
{
for (int i = 0; i < source.Length; i++)
{
if (source[i] == item)
{
return true;
}
}

return false;
}
}

0 comments on commit 7d6d691

Please sign in to comment.