Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Dec 9, 2023
1 parent abcea40 commit ec7bffe
Show file tree
Hide file tree
Showing 8 changed files with 607 additions and 235 deletions.
223 changes: 145 additions & 78 deletions JL.Core/Dicts/EDICT/JMdict/JmdictDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,108 +193,175 @@ FROM record r
using SqliteDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
string searchKey = dataReader.GetString(nameof(searchKey));
int id = dataReader.GetInt32(nameof(id));
string primarySpelling = dataReader.GetString(nameof(primarySpelling));
JmdictRecord record = GetRecord(dataReader);

string[]? primarySpellingOrthographyInfo = null;
if (dataReader[nameof(primarySpellingOrthographyInfo)] is string primarySpellingOrthographyInfoFromDB)
string searchKey = dataReader.GetString(nameof(searchKey));
if (results.TryGetValue(searchKey, out IList<IDictRecord>? result))
{
primarySpellingOrthographyInfo = JsonSerializer.Deserialize<string[]>(primarySpellingOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
result.Add(record);
}

string[]?[]? spellingRestrictions = null;
if (dataReader[nameof(spellingRestrictions)] is string spellingRestrictionsFromDB)
else
{
spellingRestrictions = JsonSerializer.Deserialize<string[]?[]>(spellingRestrictionsFromDB, Utils.s_jsoNotIgnoringNull);
results[searchKey] = new List<IDictRecord> { record };
}
}

string[]? alternativeSpellings = null;
if (dataReader[nameof(alternativeSpellings)] is string alternativeSpellingsFromDB)
{
alternativeSpellings = JsonSerializer.Deserialize<string[]>(alternativeSpellingsFromDB, Utils.s_jsoNotIgnoringNull);
}
return results;
}

string[]?[]? alternativeSpellingsOrthographyInfo = null;
if (dataReader[nameof(alternativeSpellingsOrthographyInfo)] is string alternativeSpellingsOrthographyInfoFromDB)
{
alternativeSpellingsOrthographyInfo = JsonSerializer.Deserialize<string[]?[]>(alternativeSpellingsOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
}
public static void LoadFromDB(Dict dict)
{
using SqliteConnection connection = new(string.Create(CultureInfo.InvariantCulture, $"Data Source={DictUtils.GetDBPath(dict.Name)};Mode=ReadOnly"));
connection.Open();
using SqliteCommand command = connection.CreateCommand();

string[]? readings = null;
if (dataReader[nameof(readings)] is string readingsFromDB)
{
readings = JsonSerializer.Deserialize<string[]>(readingsFromDB, Utils.s_jsoNotIgnoringNull);
}
command.CommandText =
"""
SELECT json_array(rsk.search_key) AS searchKeys,
r.edict_id AS id,
r.primary_spelling AS primarySpelling,
r.primary_spelling_orthography_info AS primarySpellingOrthographyInfo,
r.spelling_restrictions AS spellingRestrictions,
r.alternative_spellings AS alternativeSpellings,
r.alternative_spellings_orthography_info AS alternativeSpellingsOrthographyInfo,
r.readings AS readings,
r.readings_orthography_info AS readingsOrthographyInfo,
r.reading_restrictions AS readingRestrictions,
r.glossary AS definitions,
r.glossary_info AS definitionInfo,
r.part_of_speech AS wordClasses,
r.fields AS fields,
r.misc AS misc,
r.dialects AS dialects,
r.loanword_etymology AS loanwordEtymology,
r.cross_references AS relatedTerms,
r.antonyms AS antonyms
FROM record r
INNER JOIN record_search_key rsk ON r.id = rsk.record_id
GROUP BY r.id
""";

string[]?[]? readingRestrictions = null;
if (dataReader[nameof(readingRestrictions)] is string readingRestrictionsFromDB)
{
readingRestrictions = JsonSerializer.Deserialize<string[]?[]>(readingRestrictionsFromDB, Utils.s_jsoNotIgnoringNull);
}
using SqliteDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
JmdictRecord record = GetRecord(dataReader);

string[]?[]? readingsOrthographyInfo = null;
if (dataReader[nameof(readingsOrthographyInfo)] is string readingsOrthographyInfoFromDB)
string[] searchKeys = JsonSerializer.Deserialize<string[]>(dataReader.GetString(nameof(searchKeys)), Utils.s_jsoNotIgnoringNull)!;
for (int i = 0; i < searchKeys.Length; i++)
{
readingsOrthographyInfo = JsonSerializer.Deserialize<string[]?[]>(readingsOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
string searchKey = searchKeys[i];
if (dict.Contents.TryGetValue(searchKey, out IList<IDictRecord>? result))
{
result.Add(record);
}
else
{
dict.Contents[searchKey] = new List<IDictRecord> { record };
}
}
}

string[][] definitions = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(definitions)), Utils.s_jsoNotIgnoringNull)!;
string[][] wordClasses = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(wordClasses)), Utils.s_jsoNotIgnoringNull)!;
foreach ((string key, IList<IDictRecord> recordList) in dict.Contents)
{
dict.Contents[key] = recordList.ToArray();
}

string[]?[]? fields = null;
if (dataReader[nameof(fields)] is string fieldsFromDB)
{
fields = JsonSerializer.Deserialize<string[]?[]>(fieldsFromDB, Utils.s_jsoNotIgnoringNull);
}
dict.Contents.TrimExcess();
}

string[]?[]? misc = null;
if (dataReader[nameof(misc)] is string miscFromDB)
{
misc = JsonSerializer.Deserialize<string[]?[]>(miscFromDB, Utils.s_jsoNotIgnoringNull);
}
private static JmdictRecord GetRecord(SqliteDataReader dataReader)
{
int id = dataReader.GetInt32(nameof(id));
string primarySpelling = dataReader.GetString(nameof(primarySpelling));

string?[]? definitionInfo = null;
if (dataReader[nameof(definitionInfo)] is string definitionInfoFromDB)
{
definitionInfo = JsonSerializer.Deserialize<string?[]>(definitionInfoFromDB, Utils.s_jsoNotIgnoringNull);
}
string[]? primarySpellingOrthographyInfo = null;
if (dataReader[nameof(primarySpellingOrthographyInfo)] is string primarySpellingOrthographyInfoFromDB)
{
primarySpellingOrthographyInfo = JsonSerializer.Deserialize<string[]>(primarySpellingOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? dialects = null;
if (dataReader[nameof(dialects)] is string dialectsFromDB)
{
dialects = JsonSerializer.Deserialize<string[]?[]>(dialectsFromDB, Utils.s_jsoNotIgnoringNull);
}
string[]?[]? spellingRestrictions = null;
if (dataReader[nameof(spellingRestrictions)] is string spellingRestrictionsFromDB)
{
spellingRestrictions = JsonSerializer.Deserialize<string[]?[]>(spellingRestrictionsFromDB, Utils.s_jsoNotIgnoringNull);
}

LoanwordSource[]?[]? loanwordEtymology = null;
if (dataReader[nameof(loanwordEtymology)] is string loanwordEtymologyFromDB)
{
loanwordEtymology = JsonSerializer.Deserialize<LoanwordSource[]?[]>(loanwordEtymologyFromDB, Utils.s_jsoNotIgnoringNull);
}
string[]? alternativeSpellings = null;
if (dataReader[nameof(alternativeSpellings)] is string alternativeSpellingsFromDB)
{
alternativeSpellings = JsonSerializer.Deserialize<string[]>(alternativeSpellingsFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? relatedTerms = null;
if (dataReader[nameof(relatedTerms)] is string relatedTermsFromDB)
{
relatedTerms = JsonSerializer.Deserialize<string[]?[]>(relatedTermsFromDB, Utils.s_jsoNotIgnoringNull);
}
string[]?[]? alternativeSpellingsOrthographyInfo = null;
if (dataReader[nameof(alternativeSpellingsOrthographyInfo)] is string alternativeSpellingsOrthographyInfoFromDB)
{
alternativeSpellingsOrthographyInfo = JsonSerializer.Deserialize<string[]?[]>(alternativeSpellingsOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? antonyms = null;
if (dataReader[nameof(antonyms)] is string antonymsFromDB)
{
antonyms = JsonSerializer.Deserialize<string[]?[]>(antonymsFromDB, Utils.s_jsoNotIgnoringNull);
}
string[]? readings = null;
if (dataReader[nameof(readings)] is string readingsFromDB)
{
readings = JsonSerializer.Deserialize<string[]>(readingsFromDB, Utils.s_jsoNotIgnoringNull);
}

if (results.TryGetValue(searchKey, out IList<IDictRecord>? result))
{
result.Add(new JmdictRecord(id, primarySpelling, primarySpellingOrthographyInfo, alternativeSpellings, alternativeSpellingsOrthographyInfo, readings, readingsOrthographyInfo, definitions, wordClasses, spellingRestrictions, readingRestrictions, fields, misc, definitionInfo, dialects, loanwordEtymology, relatedTerms, antonyms));
}
string[]?[]? readingRestrictions = null;
if (dataReader[nameof(readingRestrictions)] is string readingRestrictionsFromDB)
{
readingRestrictions = JsonSerializer.Deserialize<string[]?[]>(readingRestrictionsFromDB, Utils.s_jsoNotIgnoringNull);
}

else
{
results[searchKey] = new List<IDictRecord> { new JmdictRecord(id, primarySpelling, primarySpellingOrthographyInfo, alternativeSpellings, alternativeSpellingsOrthographyInfo, readings, readingsOrthographyInfo, definitions, wordClasses, spellingRestrictions, readingRestrictions, fields, misc, definitionInfo, dialects, loanwordEtymology, relatedTerms, antonyms) };
}
string[]?[]? readingsOrthographyInfo = null;
if (dataReader[nameof(readingsOrthographyInfo)] is string readingsOrthographyInfoFromDB)
{
readingsOrthographyInfo = JsonSerializer.Deserialize<string[]?[]>(readingsOrthographyInfoFromDB, Utils.s_jsoNotIgnoringNull);
}

return results;
string[][] definitions = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(definitions)), Utils.s_jsoNotIgnoringNull)!;
string[][] wordClasses = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(wordClasses)), Utils.s_jsoNotIgnoringNull)!;

string[]?[]? fields = null;
if (dataReader[nameof(fields)] is string fieldsFromDB)
{
fields = JsonSerializer.Deserialize<string[]?[]>(fieldsFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? misc = null;
if (dataReader[nameof(misc)] is string miscFromDB)
{
misc = JsonSerializer.Deserialize<string[]?[]>(miscFromDB, Utils.s_jsoNotIgnoringNull);
}

string?[]? definitionInfo = null;
if (dataReader[nameof(definitionInfo)] is string definitionInfoFromDB)
{
definitionInfo = JsonSerializer.Deserialize<string?[]>(definitionInfoFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? dialects = null;
if (dataReader[nameof(dialects)] is string dialectsFromDB)
{
dialects = JsonSerializer.Deserialize<string[]?[]>(dialectsFromDB, Utils.s_jsoNotIgnoringNull);
}

LoanwordSource[]?[]? loanwordEtymology = null;
if (dataReader[nameof(loanwordEtymology)] is string loanwordEtymologyFromDB)
{
loanwordEtymology = JsonSerializer.Deserialize<LoanwordSource[]?[]>(loanwordEtymologyFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? relatedTerms = null;
if (dataReader[nameof(relatedTerms)] is string relatedTermsFromDB)
{
relatedTerms = JsonSerializer.Deserialize<string[]?[]>(relatedTermsFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]?[]? antonyms = null;
if (dataReader[nameof(antonyms)] is string antonymsFromDB)
{
antonyms = JsonSerializer.Deserialize<string[]?[]>(antonymsFromDB, Utils.s_jsoNotIgnoringNull);
}

return new JmdictRecord(id, primarySpelling, primarySpellingOrthographyInfo, alternativeSpellings, alternativeSpellingsOrthographyInfo, readings, readingsOrthographyInfo, definitions, wordClasses, spellingRestrictions, readingRestrictions, fields, misc, definitionInfo, dialects, loanwordEtymology, relatedTerms, antonyms);
}
}
82 changes: 65 additions & 17 deletions JL.Core/Dicts/EDICT/JMnedict/JmnedictDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,84 @@ FROM record r
using SqliteDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
string searchKey = dataReader.GetString(nameof(searchKey));
int id = dataReader.GetInt32(nameof(id));
string primarySpelling = dataReader.GetString(nameof(primarySpelling));
JmnedictRecord record = GetRecord(dataReader);

string[]? readings = null;
if (dataReader[nameof(readings)] is string readingsFromDB)
string searchKey = dataReader.GetString(nameof(searchKey));
if (results.TryGetValue(searchKey, out IList<IDictRecord>? result))
{
readings = JsonSerializer.Deserialize<string[]>(readingsFromDB, Utils.s_jsoNotIgnoringNull);
result.Add(record);
}

string[]? alternativeSpellings = null;
if (dataReader[nameof(alternativeSpellings)] is string alternativeSpellingsFromDB)
else
{
alternativeSpellings = JsonSerializer.Deserialize<string[]>(alternativeSpellingsFromDB, Utils.s_jsoNotIgnoringNull);
results[searchKey] = new List<IDictRecord> { record };
}
}

string[][] definitions = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(definitions)), Utils.s_jsoNotIgnoringNull)!;
string[][] nameTypes = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(nameTypes)), Utils.s_jsoNotIgnoringNull)!;
return results;
}

if (results.TryGetValue(searchKey, out IList<IDictRecord>? result))
public static void LoadFromDB(Dict dict)
{
using SqliteConnection connection = new(string.Create(CultureInfo.InvariantCulture, $"Data Source={DictUtils.GetDBPath(dict.Name)};Mode=ReadOnly"));
connection.Open();
using SqliteCommand command = connection.CreateCommand();

command.CommandText =
"""
SELECT r.primary_spelling_in_hiragana AS searchKey,
r.jmnedict_id AS id,
r.primary_spelling AS primarySpelling,
r.readings AS readings,
r.alternative_spellings AS alternativeSpellings,
r.glossary AS definitions,
r.name_types AS nameTypes
FROM record r
""";

using SqliteDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
JmnedictRecord record = GetRecord(dataReader);

string searchKey = dataReader.GetString(nameof(searchKey));
if (dict.Contents.TryGetValue(searchKey, out IList<IDictRecord>? result))
{
result.Add(new JmnedictRecord(id, primarySpelling, alternativeSpellings, readings, definitions, nameTypes));
result.Add(record);
}

else
{
results[searchKey] = new List<IDictRecord> { new JmnedictRecord(id, primarySpelling, alternativeSpellings, readings, definitions, nameTypes) };
dict.Contents[searchKey] = new List<IDictRecord> { record };
}
}

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

dict.Contents.TrimExcess();
}

private static JmnedictRecord GetRecord(SqliteDataReader dataReader)
{
int id = dataReader.GetInt32(nameof(id));
string primarySpelling = dataReader.GetString(nameof(primarySpelling));

string[]? readings = null;
if (dataReader[nameof(readings)] is string readingsFromDB)
{
readings = JsonSerializer.Deserialize<string[]>(readingsFromDB, Utils.s_jsoNotIgnoringNull);
}

string[]? alternativeSpellings = null;
if (dataReader[nameof(alternativeSpellings)] is string alternativeSpellingsFromDB)
{
alternativeSpellings = JsonSerializer.Deserialize<string[]>(alternativeSpellingsFromDB, Utils.s_jsoNotIgnoringNull);
}

string[][] definitions = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(definitions)), Utils.s_jsoNotIgnoringNull)!;
string[][] nameTypes = JsonSerializer.Deserialize<string[][]>(dataReader.GetString(nameof(nameTypes)), Utils.s_jsoNotIgnoringNull)!;

return new JmnedictRecord(id, primarySpelling, alternativeSpellings, readings, definitions, nameTypes);
}
}
Loading

0 comments on commit ec7bffe

Please sign in to comment.