Skip to content

Commit a0de411

Browse files
committed
Minor
1 parent f978dc2 commit a0de411

File tree

1 file changed

+77
-28
lines changed

1 file changed

+77
-28
lines changed

JL.Core/Mining/MiningUtils.cs

+77-28
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,10 @@ public static class MiningUtils
7474
</style>
7575
""";
7676

77-
private static string? GetMiningParameter(JLField field, LookupResult lookupResult, string currentText, string? formattedDefinitions, string? selectedDefinitions, int currentCharPosition)
77+
private static string? GetMiningParameter(JLField field, LookupResult lookupResult, string currentText, int currentCharPosition)
7878
{
7979
switch (field)
8080
{
81-
case JLField.Nothing:
82-
case JLField.Audio:
83-
case JLField.Image:
84-
case JLField.LocalTime:
85-
default:
86-
return null;
8781
case JLField.LeadingSentencePart:
8882
{
8983
string sentence = JapaneseUtils.FindSentence(currentText, currentCharPosition);
@@ -96,6 +90,7 @@ public static class MiningUtils
9690
int sentenceStartIndex = currentText.IndexOf(sentence, searchStartIndex, StringComparison.Ordinal);
9791
return currentText[sentenceStartIndex..currentCharPosition];
9892
}
93+
9994
case JLField.TrailingSentencePart:
10095
{
10196
string sentence = JapaneseUtils.FindSentence(currentText, currentCharPosition);
@@ -108,6 +103,7 @@ public static class MiningUtils
108103
int sentenceStartIndex = currentText.IndexOf(sentence, searchStartIndex, StringComparison.Ordinal);
109104
return currentText[(lookupResult.MatchedText.Length + currentCharPosition)..(sentenceStartIndex + sentence.Length)];
110105
}
106+
111107
case JLField.Sentence:
112108
{
113109
string sentence = JapaneseUtils.FindSentence(currentText, currentCharPosition);
@@ -122,93 +118,135 @@ public static class MiningUtils
122118
string trailingSentencePart = currentText[(lookupResult.MatchedText.Length + currentCharPosition)..(sentenceStartIndex + sentence.Length)];
123119
return $"{leadingSentencePart}<b>{lookupResult.MatchedText}</b>{trailingSentencePart}";
124120
}
121+
125122
case JLField.SourceText:
126123
{
127124
string leadingSourcePart = currentText[..currentCharPosition].ReplaceLineEndings("<br/>");
128125
string trailingSourcePart = currentText[(currentCharPosition + lookupResult.MatchedText.Length)..].ReplaceLineEndings("<br/>");
129126
return $"{leadingSourcePart}<b>{lookupResult.MatchedText}</b>{trailingSourcePart}".ReplaceLineEndings("<br/>");
130127
}
128+
131129
case JLField.Readings:
132-
return lookupResult.Readings is not null ? string.Join('、', lookupResult.Readings) : null;
130+
return lookupResult.Readings is not null
131+
? string.Join('、', lookupResult.Readings)
132+
: null;
133+
133134
case JLField.ReadingsWithOrthographyInfo:
134135
return lookupResult.ReadingsOrthographyInfoList is not null && lookupResult.Readings is not null
135-
? LookupResultUtils.ElementWithOrthographyInfoToText(lookupResult.Readings, lookupResult.ReadingsOrthographyInfoList)
136-
: lookupResult.Readings is not null ? string.Join('、', lookupResult.Readings) : null;
136+
? LookupResultUtils.ElementWithOrthographyInfoToText(lookupResult.Readings, lookupResult.ReadingsOrthographyInfoList)
137+
: lookupResult.Readings is not null
138+
? string.Join('、', lookupResult.Readings)
139+
: null;
140+
137141
case JLField.FirstReading:
138142
return lookupResult.Readings?[0];
143+
139144
case JLField.PrimarySpellingAndReadings:
140-
return lookupResult.Readings is not null ? $"{lookupResult.PrimarySpelling}[{string.Join('、', lookupResult.Readings)}]" : null;
145+
return lookupResult.Readings is not null
146+
? $"{lookupResult.PrimarySpelling}[{string.Join('、', lookupResult.Readings)}]"
147+
: null;
148+
141149
case JLField.PrimarySpellingAndFirstReading:
142150
return lookupResult.Readings is not null
143151
? $"{lookupResult.PrimarySpelling}[{lookupResult.Readings[0]}]"
144152
: null;
153+
145154
case JLField.PrimarySpellingWithOrthographyInfo:
146155
return lookupResult.PrimarySpellingOrthographyInfoList is not null
147-
? $"{lookupResult.PrimarySpelling} ({string.Join(", ", lookupResult.PrimarySpellingOrthographyInfoList)})"
148-
: lookupResult.PrimarySpelling;
156+
? $"{lookupResult.PrimarySpelling} ({string.Join(", ", lookupResult.PrimarySpellingOrthographyInfoList)})"
157+
: lookupResult.PrimarySpelling;
158+
149159
case JLField.AlternativeSpellings:
150-
return lookupResult.AlternativeSpellings is not null ? string.Join('、', lookupResult.AlternativeSpellings) : null;
160+
return lookupResult.AlternativeSpellings is not null
161+
? string.Join('、', lookupResult.AlternativeSpellings)
162+
: null;
163+
151164
case JLField.AlternativeSpellingsWithOrthographyInfo:
152-
return lookupResult.AlternativeSpellings is not null ? lookupResult.AlternativeSpellingsOrthographyInfoList is not null
165+
return lookupResult.AlternativeSpellings is not null
166+
? lookupResult.AlternativeSpellingsOrthographyInfoList is not null
153167
? LookupResultUtils.ElementWithOrthographyInfoToText(lookupResult.AlternativeSpellings, lookupResult.AlternativeSpellingsOrthographyInfoList)
154-
: string.Join('、', lookupResult.AlternativeSpellings) : null;
168+
: string.Join('、', lookupResult.AlternativeSpellings)
169+
: null;
170+
155171
case JLField.MatchedText:
156172
return lookupResult.MatchedText;
173+
157174
case JLField.PrimarySpelling:
158175
return lookupResult.PrimarySpelling;
176+
159177
case JLField.DeconjugatedMatchedText:
160178
return lookupResult.DeconjugatedMatchedText;
179+
161180
case JLField.KanjiStats:
162181
return lookupResult.KanjiStats;
182+
163183
case JLField.OnReadings:
164-
return lookupResult.OnReadings is not null ? string.Join('、', lookupResult.OnReadings) : null;
184+
return lookupResult.OnReadings is not null
185+
? string.Join('、', lookupResult.OnReadings)
186+
: null;
187+
165188
case JLField.KunReadings:
166-
return lookupResult.KunReadings is not null ? string.Join('、', lookupResult.KunReadings) : null;
189+
return lookupResult.KunReadings is not null
190+
? string.Join('、', lookupResult.KunReadings)
191+
: null;
192+
167193
case JLField.NanoriReadings:
168-
return lookupResult.NanoriReadings is not null ? string.Join('、', lookupResult.NanoriReadings) : null;
194+
return lookupResult.NanoriReadings is not null
195+
? string.Join('、', lookupResult.NanoriReadings)
196+
: null;
197+
169198
case JLField.EdictId:
170199
return lookupResult.EdictId > 0
171200
? lookupResult.EdictId.ToString(CultureInfo.InvariantCulture)
172201
: null;
173202

174203
case JLField.DeconjugationProcess:
175204
return lookupResult.DeconjugationProcess;
205+
176206
case JLField.KanjiComposition:
177207
return lookupResult.KanjiComposition;
208+
178209
case JLField.StrokeCount:
179210
return lookupResult.StrokeCount > 0
180211
? lookupResult.StrokeCount.ToString(CultureInfo.InvariantCulture)
181212
: null;
213+
182214
case JLField.KanjiGrade:
183215
return lookupResult.KanjiGrade != byte.MaxValue
184216
? lookupResult.KanjiGrade.ToString(CultureInfo.InvariantCulture)
185217
: null;
218+
186219
case JLField.RadicalNames:
187220
return lookupResult.RadicalNames is not null
188221
? string.Join('、', lookupResult.RadicalNames)
189222
: null;
190-
case JLField.Definitions:
191-
return formattedDefinitions?.ReplaceLineEndings("<br/>");
223+
192224
case JLField.SelectedDefinitions:
193-
return selectedDefinitions is not null ? selectedDefinitions.ReplaceLineEndings("<br/>") : formattedDefinitions?.ReplaceLineEndings("<br/>");
225+
case JLField.Definitions:
226+
return lookupResult.FormattedDefinitions?.ReplaceLineEndings("<br/>");
227+
194228
case JLField.LeadingSourceTextPart:
195229
return currentText[..currentCharPosition].ReplaceLineEndings("<br/>");
230+
196231
case JLField.TrailingSourceTextPart:
197232
return currentText[(currentCharPosition + lookupResult.MatchedText.Length)..].ReplaceLineEndings("<br/>");
233+
198234
case JLField.DictionaryName:
199235
return lookupResult.Dict.Name;
236+
200237
case JLField.Frequencies:
201238
return lookupResult.Frequencies is not null
202239
? LookupResultUtils.FrequenciesToText(lookupResult.Frequencies, true, lookupResult.Frequencies.Count is 1)
203240
: null;
241+
204242
case JLField.RawFrequencies:
205243
if (lookupResult.Frequencies is not null)
206244
{
207-
List<LookupFrequencyResult> validFrequencies = lookupResult.Frequencies
208-
.Where(static f => f.Freq is > 0 and < int.MaxValue).ToList();
245+
List<LookupFrequencyResult> validFrequencies = lookupResult.Frequencies.Where(static f => f.Freq is > 0 and < int.MaxValue).ToList();
209246
return string.Join(", ", validFrequencies.Select(static f => f.Freq).ToList());
210247
}
211248
return null;
249+
212250
case JLField.PreferredFrequency:
213251
if (lookupResult.Frequencies is not null)
214252
{
@@ -219,14 +257,15 @@ public static class MiningUtils
219257
}
220258
}
221259
return null;
260+
222261
case JLField.FrequencyHarmonicMean:
223262
if (lookupResult.Frequencies is not null)
224263
{
225-
List<LookupFrequencyResult> validFrequencies = lookupResult.Frequencies
226-
.Where(static f => f.Freq is > 0 and < int.MaxValue).ToList();
264+
List<LookupFrequencyResult> validFrequencies = lookupResult.Frequencies.Where(static f => f.Freq is > 0 and < int.MaxValue).ToList();
227265
return CalculateHarmonicMean(validFrequencies).ToString(CultureInfo.InvariantCulture);
228266
}
229267
return null;
268+
230269
case JLField.PitchAccents:
231270
{
232271
if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.PitchAccentYomichan, out Dict? pitchDict) && pitchDict.Active)
@@ -254,6 +293,7 @@ public static class MiningUtils
254293
}
255294
return null;
256295
}
296+
257297
case JLField.NumericPitchAccents:
258298
{
259299
if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.PitchAccentYomichan, out Dict? pitchDict) && pitchDict.Active)
@@ -279,6 +319,7 @@ public static class MiningUtils
279319
}
280320
return null;
281321
}
322+
282323
case JLField.PitchAccentForFirstReading:
283324
{
284325
if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.PitchAccentYomichan, out Dict? pitchDict) && pitchDict.Active)
@@ -296,6 +337,7 @@ public static class MiningUtils
296337
}
297338
return null;
298339
}
340+
299341
case JLField.NumericPitchAccentForFirstReading:
300342
{
301343
if (DictUtils.SingleDictTypeDicts.TryGetValue(DictType.PitchAccentYomichan, out Dict? pitchDict) && pitchDict.Active)
@@ -313,6 +355,13 @@ public static class MiningUtils
313355
}
314356
return null;
315357
}
358+
359+
case JLField.Nothing:
360+
case JLField.Audio:
361+
case JLField.Image:
362+
case JLField.LocalTime:
363+
default:
364+
return null;
316365
}
317366
}
318367

@@ -687,7 +736,6 @@ public static async Task MineToFile(LookupResult lookupResult, string currentTex
687736

688737
public static async ValueTask<bool[]?> CheckDuplicates(LookupResult[] lookupResults, string currentText, int currentCharPosition)
689738
{
690-
691739
Dictionary<MineType, AnkiConfig>? ankiConfigDict = await AnkiConfig.ReadAnkiConfig().ConfigureAwait(false);
692740
if (ankiConfigDict is null)
693741
{
@@ -727,11 +775,12 @@ public static async Task MineToFile(LookupResult lookupResult, string currentTex
727775

728776
Dictionary<string, JLField> userFields = ankiConfig.Fields;
729777
(string firstFieldName, JLField firstField) = userFields.First();
730-
string? firstFieldValue = GetMiningParameter(firstField, lookupResult, currentText, null, null, currentCharPosition);
778+
string? firstFieldValue = GetMiningParameter(firstField, lookupResult, currentText, currentCharPosition);
731779
if (string.IsNullOrEmpty(firstFieldValue))
732780
{
733781
continue;
734782
}
783+
735784
Dictionary<string, string> fields = new(1, StringComparer.Ordinal)
736785
{
737786
{ firstFieldName, firstFieldValue }

0 commit comments

Comments
 (0)