Skip to content

Commit

Permalink
Minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Sep 30, 2023
1 parent 6ff419d commit 09a2e4f
Show file tree
Hide file tree
Showing 30 changed files with 238 additions and 196 deletions.
7 changes: 5 additions & 2 deletions JL.Core.Tests/LookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public void ClassInit()
antonym: new AntonymOption(false)
)));

JmdictLoader.Load(DictUtils.Dicts.Values.First(static dict => dict.Type is DictType.JMdict)).Wait();
Dict dict = DictUtils.Dicts["JMdict"];
DictUtils.BuiltInDictTypeToDict[DictType.JMdict] = dict;
JmdictLoader.Load(dict).Wait();

FreqUtils.FreqDicts = FreqUtils.s_builtInFreqs;
FreqUtils.LoadFrequencies().Wait();
DeconjugatorUtils.DeserializeRules().Wait();
Expand All @@ -59,7 +62,7 @@ public void LookupText_始まる()
frequencies: new List<LookupFrequencyResult> { new("VN (Nazeka)", 759) },
primarySpelling: "始まる",
deconjugatedMatchedText: "始まる",
readings: new List<string> { "はじまる" },
readings: new[] { "はじまる" },
formattedDefinitions: "(v5r, vi) (1) to begin; to start; to commence (v5r, vi) (2) to happen (again); to begin (anew) (v5r, vi) (3) to date (from); to originate (in)",
edictId: 1307500,
alternativeSpellingsOrthographyInfoList: new List<string?>(),
Expand Down
3 changes: 1 addition & 2 deletions JL.Core/Anki/Mining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ public static async Task<bool> Mine(Dictionary<JLField, string> miningParams)
/// Value is the actual content of a mining field (e.g. if the field name is LocalTime, then it should contain the current time) <br/>
/// UserField is the name of the user's field in Anki (e.g. Expression) <br/>
/// </summary>
private static Dictionary<string, object> ConvertFields(Dictionary<string, JLField> userFields,
IReadOnlyDictionary<JLField, string> miningParams)
private static Dictionary<string, object> ConvertFields(Dictionary<string, JLField> userFields, Dictionary<JLField, string> miningParams)
{
Dictionary<string, object> dict = new();
foreach ((string key, JLField value) in userFields)
Expand Down
7 changes: 4 additions & 3 deletions JL.Core/Dicts/CustomNameDict/CustomNameLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ internal static void Load(Dict dict, CancellationToken cancellationToken)
public static void AddToDictionary(string spelling, string? reading, string nameType, Dictionary<string, IList<IDictRecord>> customNameDictionary)
{
CustomNameRecord newNameRecord = new(spelling, reading, nameType);
if (customNameDictionary.TryGetValue(JapaneseUtils.KatakanaToHiragana(spelling), out IList<IDictRecord>? entry))

string spellingInHiragana = JapaneseUtils.KatakanaToHiragana(spelling);
if (customNameDictionary.TryGetValue(spellingInHiragana, out IList<IDictRecord>? entry))
{
if (!entry.Contains(newNameRecord))
{
Expand All @@ -52,8 +54,7 @@ public static void AddToDictionary(string spelling, string? reading, string name

else
{
customNameDictionary.Add(JapaneseUtils.KatakanaToHiragana(spelling),
new List<IDictRecord> { newNameRecord });
customNameDictionary[spellingInHiragana] = new List<IDictRecord> { newNameRecord };
}
}
}
6 changes: 3 additions & 3 deletions JL.Core/Dicts/CustomWordDict/CustomWordLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public static void AddToDictionary(string[] spellings, string[]? readings, strin

private static bool AddRecordToDictionary(string spelling, CustomWordRecord record, Dictionary<string, IList<IDictRecord>> dictionary)
{
if (dictionary.TryGetValue(JapaneseUtils.KatakanaToHiragana(spelling), out IList<IDictRecord>? result))
string spellingInHiragana = JapaneseUtils.KatakanaToHiragana(spelling);
if (dictionary.TryGetValue(spellingInHiragana, out IList<IDictRecord>? result))
{
if (result.Contains(record))
{
Expand All @@ -137,8 +138,7 @@ private static bool AddRecordToDictionary(string spelling, CustomWordRecord reco
}
else
{
dictionary.Add(JapaneseUtils.KatakanaToHiragana(spelling),
new List<IDictRecord> { record });
dictionary[spellingInHiragana] = new List<IDictRecord> { record };
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions JL.Core/Dicts/CustomWordDict/CustomWordRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public override bool Equals(object? obj)
CustomWordRecord customWordRecordObj = (CustomWordRecord)obj;

return PrimarySpelling == customWordRecordObj.PrimarySpelling
&& (customWordRecordObj.AlternativeSpellings?.SequenceEqual(AlternativeSpellings ?? Enumerable.Empty<string>()) ?? AlternativeSpellings is null)
&& (customWordRecordObj.Readings?.SequenceEqual(Readings ?? Enumerable.Empty<string>()) ?? Readings is null)
&& (customWordRecordObj.AlternativeSpellings?.SequenceEqual(AlternativeSpellings ?? Array.Empty<string>()) ?? AlternativeSpellings is null)
&& (customWordRecordObj.Readings?.SequenceEqual(Readings ?? Array.Empty<string>()) ?? Readings is null)
&& customWordRecordObj.Definitions.SequenceEqual(Definitions)
&& customWordRecordObj.WordClasses.SequenceEqual(WordClasses);
}
Expand Down
38 changes: 32 additions & 6 deletions JL.Core/Dicts/DictUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static class DictUtils
}
};

public static readonly Dictionary<DictType, Dict> BuiltInDictTypeToDict = new(7);

public static readonly Dictionary<string, string> JmdictEntities = new(254)
{
{ "bra", "Brazilian" },
Expand Down Expand Up @@ -987,13 +989,37 @@ internal static async Task DeserializeDicts()
dict.Priority = priority;
++priority;

if (dict.Type is DictType.ProfileCustomNameDictionary)
{
dict.Path = ProfileUtils.GetProfileCustomNameDictPath(ProfileUtils.CurrentProfile);
}
else if (dict.Type is DictType.ProfileCustomWordDictionary)
switch (dict.Type)
{
dict.Path = ProfileUtils.GetProfileCustomWordDictPath(ProfileUtils.CurrentProfile);
case DictType.ProfileCustomNameDictionary:
dict.Path = ProfileUtils.GetProfileCustomNameDictPath(ProfileUtils.CurrentProfile);
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.ProfileCustomWordDictionary:
dict.Path = ProfileUtils.GetProfileCustomWordDictPath(ProfileUtils.CurrentProfile);
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.CustomNameDictionary:
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.CustomWordDictionary:
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.JMdict:
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.Kanjidic:
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;

case DictType.JMnedict:
BuiltInDictTypeToDict.Add(dict.Type, dict);
break;
}

dict.Path = Utils.GetPath(dict.Path);
Expand Down
6 changes: 2 additions & 4 deletions JL.Core/Dicts/EDICT/JMdict/JmdictRecordBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ public static void AddToDictionary(JmdictEntry entry, Dictionary<string, IList<I
recordDictionary.Add(key, record);
++index;

if (i is 0
&& allSpellingsWithoutSearchOnlyForms.Count is 0
&& entry.KanjiElements.Count > 0)
if (i is 0 && allSpellingsWithoutSearchOnlyForms.Count is 0)
{
for (int j = 0; j < entry.KanjiElements.Count; j++)
{
Expand All @@ -246,7 +244,7 @@ public static void AddToDictionary(JmdictEntry entry, Dictionary<string, IList<I
}
else
{
jmdictDictionary.Add(key, new List<IDictRecord> { jmdictRecord });
jmdictDictionary[key] = new List<IDictRecord> { jmdictRecord };
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/EDICT/JMnedict/JmnedictRecordBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void AddToDictionary(JmnedictEntry entry, Dictionary<string, IList
}
else
{
jmnedictDictionary.Add(key, new List<IDictRecord> { jmnedictRecord });
jmnedictDictionary[key] = new List<IDictRecord> { jmnedictRecord };
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions JL.Core/Dicts/EDICT/KANJIDIC/KanjidicLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ public static async Task Load(Dict dict)
}
}

foreach ((string key, IList<IDictRecord> recordList) in dict.Contents)
{
dict.Contents[key] = recordList.ToArray();
}

dict.Contents.TrimExcess();
}

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

private static async Task ReadCharacter(XmlReader xmlReader, Dictionary<string, IList<IDictRecord>> kanjidicDictionary)
{
string key = await xmlReader.ReadElementContentAsStringAsync().ConfigureAwait(false);
string key = (await xmlReader.ReadElementContentAsStringAsync().ConfigureAwait(false)).GetPooledString();

int grade = -1;
int strokeCount = 0;
Expand Down Expand Up @@ -142,6 +137,6 @@ private static async Task ReadCharacter(XmlReader xmlReader, Dictionary<string,

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

kanjidicDictionary.Add(key, new List<IDictRecord> { entry });
kanjidicDictionary[key] = new IDictRecord[] { entry };
}
}
6 changes: 3 additions & 3 deletions JL.Core/Dicts/EDICT/ResourceUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static async Task UpdateJmdict()
{
DictUtils.UpdatingJmdict = true;

Dict dict = DictUtils.Dicts.Values.First(static dict => dict.Type is DictType.JMdict);
Dict dict = DictUtils.BuiltInDictTypeToDict[DictType.JMdict];
bool downloaded = await UpdateResource(dict.Path,
DictUtils.s_jmdictUrl,
DictType.JMdict.ToString(), true, false)
Expand Down Expand Up @@ -141,7 +141,7 @@ public static async Task UpdateJmnedict()
{
DictUtils.UpdatingJmnedict = true;

Dict dict = DictUtils.Dicts.Values.First(static dict => dict.Type is DictType.JMnedict);
Dict dict = DictUtils.BuiltInDictTypeToDict[DictType.JMnedict];
bool downloaded = await UpdateResource(dict.Path,
DictUtils.s_jmnedictUrl,
DictType.JMnedict.ToString(), true, false)
Expand Down Expand Up @@ -170,7 +170,7 @@ public static async Task UpdateKanjidic()
{
DictUtils.UpdatingKanjidic = true;

Dict dict = DictUtils.Dicts.Values.First(static dict => dict.Type is DictType.Kanjidic);
Dict dict = DictUtils.BuiltInDictTypeToDict[DictType.Kanjidic];
bool downloaded = await UpdateResource(dict.Path,
DictUtils.s_kanjidicUrl,
DictType.Kanjidic.ToString(), true, false)
Expand Down
5 changes: 3 additions & 2 deletions JL.Core/Dicts/EPWING/EpwingNazeka/EpwingNazekaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,14 @@ public static async Task Load(Dict dict)

private static void AddRecordToDictionary(string key, EpwingNazekaRecord record, Dictionary<string, IList<IDictRecord>> dictionary)
{
if (dictionary.TryGetValue(JapaneseUtils.KatakanaToHiragana(key).GetPooledString(), out IList<IDictRecord>? result))
string keyInHiragana = JapaneseUtils.KatakanaToHiragana(key).GetPooledString();
if (dictionary.TryGetValue(keyInHiragana, out IList<IDictRecord>? result))
{
result.Add(record);
}
else
{
dictionary.Add(key, new List<IDictRecord> { record });
dictionary[keyInHiragana] = new List<IDictRecord> { record };
}
}
}
4 changes: 2 additions & 2 deletions JL.Core/Dicts/EPWING/EpwingYomichan/EpwingYomichanLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static void AddToDictionary(IEpwingRecord yomichanRecord, Dict dict)
}
else
{
dict.Contents.Add(hiraganaExpression, new List<IDictRecord> { yomichanRecord });
dict.Contents[hiraganaExpression] = new List<IDictRecord> { yomichanRecord };
}

if (dict.Type is not DictType.NonspecificNameYomichan && !string.IsNullOrEmpty(yomichanRecord.Reading))
Expand All @@ -76,7 +76,7 @@ private static void AddToDictionary(IEpwingRecord yomichanRecord, Dict dict)
}
else
{
dict.Contents.Add(hiraganaReading, new List<IDictRecord> { yomichanRecord });
dict.Contents[hiraganaReading] = new List<IDictRecord> { yomichanRecord };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal sealed class EpwingYomichanRecord : IEpwingRecord, IGetFrequency
//public int Sequence { get; init; }
//public string TermTags { get; init; }

public EpwingYomichanRecord(IReadOnlyList<JsonElement> jsonElement)
public EpwingYomichanRecord(List<JsonElement> jsonElement)
{
PrimarySpelling = jsonElement[0].ToString().GetPooledString();
Reading = jsonElement[1].ToString();
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/YomichanKanji/YomichanKanjiLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static async Task Load(Dict dict)
}
else
{
dict.Contents.Add(kanji, new List<IDictRecord> { yomichanKanjiRecord });
dict.Contents[kanji] = new List<IDictRecord> { yomichanKanjiRecord };
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Dicts/YomichanKanji/YomichanKanjiRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class YomichanKanjiRecord : IDictRecord
private string[]? Definitions { get; }
private string[]? Stats { get; }

public YomichanKanjiRecord(IReadOnlyList<JsonElement> jsonElement)
public YomichanKanjiRecord(List<JsonElement> jsonElement)
{
OnReadings = jsonElement[1].ToString().Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (OnReadings.Length is 0)
Expand Down
8 changes: 2 additions & 6 deletions JL.Core/Freqs/FrequencyNazeka/FrequencyNazekaLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ public static async Task Load(Freq freq)
{
readingFreqResult.Add(new FrequencyRecord(exactSpelling, frequencyRank));
}

else
{
freqDict.Add(reading.GetPooledString(),
new List<FrequencyRecord> { new(exactSpelling, frequencyRank) });
freqDict[reading.GetPooledString()] = new List<FrequencyRecord> { new(exactSpelling, frequencyRank) };
}

string exactSpellingInHiragana = JapaneseUtils.KatakanaToHiragana(exactSpelling).GetPooledString();

if (exactSpellingInHiragana != reading)
{
if (freqDict.TryGetValue(exactSpellingInHiragana, out IList<FrequencyRecord>? exactSpellingFreqResult))
Expand All @@ -55,8 +52,7 @@ public static async Task Load(Freq freq)

else
{
freqDict.Add(exactSpellingInHiragana,
new List<FrequencyRecord> { new(reading, frequencyRank) });
freqDict[exactSpellingInHiragana] = new List<FrequencyRecord> { new(reading, frequencyRank) };
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions JL.Core/Freqs/FrequencyYomichan/FrequencyYomichanLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static async Task Load(Freq freq)

else
{
freqDict.Add(spellingInHiragana, new List<FrequencyRecord> { new(spelling, frequency) });
freqDict[spellingInHiragana] = new List<FrequencyRecord> { new(spelling, frequency) };
}
}

Expand All @@ -92,7 +92,7 @@ public static async Task Load(Freq freq)

else
{
freqDict.Add(readingInHiragana, new List<FrequencyRecord> { new(spelling, frequency) });
freqDict[readingInHiragana] = new List<FrequencyRecord> { new(spelling, frequency) };
}

if (reading != spelling)
Expand All @@ -104,7 +104,7 @@ public static async Task Load(Freq freq)

else
{
freqDict.Add(spellingInHiragana, new List<FrequencyRecord> { new(reading, frequency) });
freqDict[spellingInHiragana] = new List<FrequencyRecord> { new(reading, frequency) };
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions JL.Core/Lookup/LookupResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class LookupResult
public Dict Dict { get; }
public string PrimarySpelling { get; init; }

public IList<string>? Readings { get; init; }
public string[]? Readings { get; init; }
public string? FormattedDefinitions { get; init; }
public int EdictId { get; init; }
public string[]? AlternativeSpellings { get; init; }
Expand All @@ -34,7 +34,7 @@ internal LookupResult(
string matchedText,
string deconjugatedMatchedText,
Dict dict,
IList<string>? readings,
string[]? readings,
List<LookupFrequencyResult>? frequencies = null,
string[]? alternativeSpellings = null,
string[]? primarySpellingOrthographyInfoList = null,
Expand Down
Loading

0 comments on commit 09a2e4f

Please sign in to comment.