Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Oct 5, 2024
1 parent d58b217 commit beedf9a
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 152 deletions.
52 changes: 27 additions & 25 deletions JL.Core/Dicts/JMdict/JmdictRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,25 @@ public int GetFrequency(Freq freq)
}
}

if (frequency is int.MaxValue && AlternativeSpellings is not null)
if (frequency is not int.MaxValue || AlternativeSpellings is null)
{
for (int i = 0; i < AlternativeSpellings.Length; i++)
return frequency;
}

for (int i = 0; i < AlternativeSpellings.Length; i++)
{
if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]),
out IList<FrequencyRecord>? alternativeSpellingFreqResults))
{
if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]),
out IList<FrequencyRecord>? alternativeSpellingFreqResults))
int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count;
for (int j = 0; j < alternativeSpellingFreqResultCount; j++)
{
int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count;
for (int j = 0; j < alternativeSpellingFreqResultCount; j++)
FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j];
if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false)
{
FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j];
if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false)
if (frequency > alternativeSpellingFreqResult.Frequency)
{
if (frequency > alternativeSpellingFreqResult.Frequency)
{
frequency = alternativeSpellingFreqResult.Frequency;
}
frequency = alternativeSpellingFreqResult.Frequency;
}
}
}
Expand All @@ -285,7 +287,6 @@ public int GetFrequency(Freq freq)
for (int i = 0; i < Readings.Length; i++)
{
string reading = Readings[i];

if (freq.Contents.TryGetValue(JapaneseUtils.KatakanaToHiragana(reading),
out IList<FrequencyRecord>? readingFreqResults))
{
Expand Down Expand Up @@ -318,7 +319,6 @@ public int GetFrequencyFromDB(Dictionary<string, List<FrequencyRecord>> freqDict
for (int i = 0; i < freqResultCount; i++)
{
FrequencyRecord freqResult = freqResults[i];

if (PrimarySpelling == freqResult.Spelling || (Readings?.Contains(freqResult.Spelling) ?? false))
{
if (frequency > freqResult.Frequency)
Expand All @@ -328,22 +328,24 @@ public int GetFrequencyFromDB(Dictionary<string, List<FrequencyRecord>> freqDict
}
}

if (frequency is int.MaxValue && AlternativeSpellings is not null)
if (frequency is not int.MaxValue || AlternativeSpellings is null)
{
return frequency;
}

for (int i = 0; i < AlternativeSpellings.Length; i++)
{
for (int i = 0; i < AlternativeSpellings.Length; i++)
if (freqDict.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), out List<FrequencyRecord>? alternativeSpellingFreqResults))
{
if (freqDict.TryGetValue(JapaneseUtils.KatakanaToHiragana(AlternativeSpellings[i]), out List<FrequencyRecord>? alternativeSpellingFreqResults))
int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count;
for (int j = 0; j < alternativeSpellingFreqResultCount; j++)
{
int alternativeSpellingFreqResultCount = alternativeSpellingFreqResults.Count;
for (int j = 0; j < alternativeSpellingFreqResultCount; j++)
FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j];
if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false)
{
FrequencyRecord alternativeSpellingFreqResult = alternativeSpellingFreqResults[j];
if (Readings?.Contains(alternativeSpellingFreqResult.Spelling) ?? false)
if (frequency > alternativeSpellingFreqResult.Frequency)
{
if (frequency > alternativeSpellingFreqResult.Frequency)
{
frequency = alternativeSpellingFreqResult.Frequency;
}
frequency = alternativeSpellingFreqResult.Frequency;
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions JL.Core/Freqs/FreqDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,29 @@ WHERE rsk.search_key IN (@1
}

using SqliteDataReader dataReader = command.ExecuteReader();
if (dataReader.HasRows)
if (!dataReader.HasRows)
{
Dictionary<string, List<FrequencyRecord>> results = new(StringComparer.Ordinal);
while (dataReader.Read())
{
FrequencyRecord record = GetRecord(dataReader);
return null;
}

string searchKey = dataReader.GetString(nameof(searchKey));
if (results.TryGetValue(searchKey, out List<FrequencyRecord>? result))
{
result.Add(record);
}
else
{
results[searchKey] = [record];
}
}
Dictionary<string, List<FrequencyRecord>> results = new(StringComparer.Ordinal);
while (dataReader.Read())
{
FrequencyRecord record = GetRecord(dataReader);

return results;
string searchKey = dataReader.GetString(nameof(searchKey));
if (results.TryGetValue(searchKey, out List<FrequencyRecord>? result))
{
result.Add(record);
}
else
{
results[searchKey] = [record];
}
}

return null;
return results;

}

public static List<FrequencyRecord>? GetRecordsFromDB(string dbName, string term)
Expand Down
2 changes: 0 additions & 2 deletions JL.Core/Network/Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public static async Task CheckForJLUpdates(bool isAutoCheck)
foreach (JsonElement asset in assets.EnumerateArray())
{
string latestReleaseUrl = asset.GetProperty("browser_download_url").GetString()!;

// Add OS check?
if (latestReleaseUrl.Contains(architecture, StringComparison.Ordinal))
{
foundRelease = true;
Expand Down
72 changes: 37 additions & 35 deletions JL.Core/Utilities/JapaneseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,56 +356,58 @@ internal static string FindSentence(string text, int position)
? text[startPosition..(endPosition + 1)].Trim()
: "";

if (sentence.Length > 1)
if (sentence.Length <= 1)
{
if (s_rightToLeftBracketDict.ContainsKey(sentence[0]))
return sentence;
}

if (s_rightToLeftBracketDict.ContainsKey(sentence[0]))
{
sentence = sentence[1..];
}

if (s_leftToRightBracketDict.ContainsKey(sentence.LastOrDefault()))
{
sentence = sentence[..^1];
}

if (s_leftToRightBracketDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket))
{
if (sentence[^1] == rightBracket)
{
sentence = sentence[1..];
sentence = sentence[1..^1];
}

if (s_leftToRightBracketDict.ContainsKey(sentence.LastOrDefault()))
else if (!sentence.Contains(rightBracket, StringComparison.Ordinal))
{
sentence = sentence[..^1];
sentence = sentence[1..];
}

if (s_leftToRightBracketDict.TryGetValue(sentence.FirstOrDefault(), out char rightBracket))
else
{
if (sentence[^1] == rightBracket)
{
sentence = sentence[1..^1];
}
else if (!sentence.Contains(rightBracket, StringComparison.Ordinal))
int numberOfLeftBrackets = sentence.Count(p => p == sentence[0]);
int numberOfRightBrackets = sentence.Count(p => p == rightBracket);

if (numberOfLeftBrackets == numberOfRightBrackets + 1)
{
sentence = sentence[1..];
}
else
{
int numberOfLeftBrackets = sentence.Count(p => p == sentence[0]);
int numberOfRightBrackets = sentence.Count(p => p == rightBracket);

if (numberOfLeftBrackets == numberOfRightBrackets + 1)
{
sentence = sentence[1..];
}
}
}
}

else if (s_rightToLeftBracketDict.TryGetValue(sentence.LastOrDefault(), out char leftBracket))
else if (s_rightToLeftBracketDict.TryGetValue(sentence.LastOrDefault(), out char leftBracket))
{
if (!sentence.Contains(leftBracket, StringComparison.Ordinal))
{
sentence = sentence[..^1];
}
else
{
if (!sentence.Contains(leftBracket, StringComparison.Ordinal))
int numberOfLeftBrackets = sentence.Count(p => p == leftBracket);
int numberOfRightBrackets = sentence.Count(p => p == sentence[^1]);

if (numberOfRightBrackets == numberOfLeftBrackets + 1)
{
sentence = sentence[..^1];
}
else
{
int numberOfLeftBrackets = sentence.Count(p => p == leftBracket);
int numberOfRightBrackets = sentence.Count(p => p == sentence[^1]);

if (numberOfRightBrackets == numberOfLeftBrackets + 1)
{
sentence = sentence[..^1];
}
}
}
}

Expand Down
60 changes: 31 additions & 29 deletions JL.Windows/GUI/AddNameWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,47 @@ private async void SaveButton_Click(object? sender, RoutedEventArgs? e)
SpellingTextBox.ClearValue(BorderBrushProperty);
}

if (isValid)
if (!isValid)
{
return;
}

#pragma warning disable CA1308 // Normalize strings to uppercase
string nameType =
NameTypeStackPanel.Children.OfType<RadioButton>()
.FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!.ToLowerInvariant();
string nameType =
NameTypeStackPanel.Children.OfType<RadioButton>()
.FirstOrDefault(static r => r.IsChecked.HasValue && r.IsChecked.Value)!.Content.ToString()!.ToLowerInvariant();
#pragma warning restore CA1308 // Normalize strings to uppercase

string spelling = SpellingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
string spelling = SpellingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();

string? reading = ReadingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
if (reading.Length is 0 || reading == spelling)
{
reading = null;
}
string? reading = ReadingTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
if (reading.Length is 0 || reading == spelling)
{
reading = null;
}

string? extraInfo = ExtraInfoTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
if (extraInfo.Length is 0)
{
extraInfo = null;
}
string? extraInfo = ExtraInfoTextBox.Text.Replace("\t", " ", StringComparison.Ordinal).Trim();
if (extraInfo.Length is 0)
{
extraInfo = null;
}

DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global"
? DictType.CustomNameDictionary
: DictType.ProfileCustomNameDictionary;
DictType dictType = ComboBoxDictType.SelectedValue.ToString() is "Global"
? DictType.CustomNameDictionary
: DictType.ProfileCustomNameDictionary;

Dict dict = DictUtils.SingleDictTypeDicts[dictType];
if (dict.Active)
{
CustomNameLoader.AddToDictionary(spelling, reading, nameType, extraInfo, dict.Contents);
}
Dict dict = DictUtils.SingleDictTypeDicts[dictType];
if (dict.Active)
{
CustomNameLoader.AddToDictionary(spelling, reading, nameType, extraInfo, dict.Contents);
}

PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
Close();
PopupWindowUtils.HidePopups(MainWindow.Instance.FirstPopupWindow);
Close();

string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath);
string line = $"{spelling}\t{reading}\t{nameType}\t{extraInfo}\n";
await File.AppendAllTextAsync(path, line).ConfigureAwait(false);
}
string path = Path.GetFullPath(dict.Path, Utils.ApplicationPath);
string line = $"{spelling}\t{reading}\t{nameType}\t{extraInfo}\n";
await File.AppendAllTextAsync(path, line).ConfigureAwait(false);
}

private void Window_Closed(object sender, EventArgs e)
Expand Down
Loading

0 comments on commit beedf9a

Please sign in to comment.