-
Notifications
You must be signed in to change notification settings - Fork 8
/
background-script.js
2227 lines (2066 loc) · 66.7 KB
/
background-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017; Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
'use strict';
// updated by a timer looping function, based on local storage set by the options page
// we only use a tiny number of settings here
let settings = {
reader_width: 800,
reader_height: 300,
deconjugator_rules_json: "",
freqlist_mode: 1,
ankiconnect_force_sync: false,
json_ignore_entries: "一,二,三,四,五,六,七,八,九,〇"
};
async function settings_init()
{
try
{
async function getvar(name)
{
let temp = (await browser.storage.local.get(name))[name];
if(temp != undefined)
settings[name] = temp;
}
getvar("reader_width");
getvar("reader_height");
getvar("deconjugator_rules_json");
getvar("freqlist_mode");
getvar("ankiconnect_force_sync");
getvar("json_ignore_entries");
} catch(err) {} // options not stored yet
}
settings_init();
browser.storage.onChanged.addListener((updates, storageArea) =>
{
if(storageArea != "local") return;
for(let setting of Object.entries(updates))
{
let option = setting[0];
let value = setting[1];
if(Object.keys(settings).includes(option))
settings[option] = value.newValue;
if(option == "enabled")
fixicon();
}
});
// We use JMdict converted to JSON. It's like 32MB (larger after converting into a data structure) so we don't want to load it for every tab.
// So we need to use a background script and send messages to it from the content script.
// Unfortunately webextensions don't have an easy way to load files, even from in the extension.
// We have to send an HTTP request and store the result in a string before parsing it into an object.
// Seriously.
let json_dicts = [];
function add_json_reading(dict, text, id)
{
if(text === "")
return;
if (!dict.lookup_json_kana.has(text))
dict.lookup_json_kana.set(text, [id]);
else
dict.lookup_json_kana.get(text).push(id);
}
function add_json_spelling(dict, text, id)
{
if(text === "")
return;
if (!dict.lookup_json_kan.has(text))
dict.lookup_json_kan.set(text, [id]);
else
dict.lookup_json_kan.get(text).push(id);
}
function or_default(val, fallback)
{
if(val !== undefined)
return val;
else
return fallback;
}
async function get_storage_or_default(name, fallback)
{
return or_default((await browser.storage.local.get(name))[name], fallback);
}
async function refresh_json()
{
let custom_dicts = (await get_storage_or_default("custom_dicts", []));
let new_json_dicts = [];
for(let stored_dict of custom_dicts)
{
if(!stored_dict.enabled)
continue;
let name = stored_dict.name;
let entries = JSON.parse(stored_dict.entries);
let lookup_json_kan = new Map();
let lookup_json_kana = new Map();
let dict = {"name": name, "entries": entries, "lookup_json_kan": lookup_json_kan, "lookup_json_kana": lookup_json_kana};
try
{
for (let i = 0; i < entries.length; i++)
{
add_json_reading(dict, entries[i]["r"], i);
for(let spelling of entries[i]["s"])
add_json_spelling(dict, spelling, i);
}
}
catch(e)
{
console.log("error while loading/reloading json dictionary:");
console.log(e);
}
new_json_dicts.push(dict);
}
json_dicts = new_json_dicts;
}
async function migrate_legacy_then_refresh()
{
let myjson = (await browser.storage.local.get(["epwing"]))["epwing"];
if(myjson !== undefined)
{
let epwing = JSON.parse(myjson);
let name = epwing.shift();
let custom_dicts = (await get_storage_or_default("custom_dicts", []));
custom_dicts.push({"name": name, "entries": JSON.stringify(epwing), "enabled": true});
browser.storage.local.set({"custom_dicts":custom_dicts});
browser.storage.local.remove(["epwing"]);
}
refresh_json();
}
migrate_legacy_then_refresh();
function json_lookup_arbitrary_as_is(dict, text)
{
let index = dict.lookup_json_kan.get(text);
if(index === undefined)
index = dict.lookup_json_kana.get(text);
if(index === undefined)
return undefined;
return copy(dict.entries[index]);
}
function json_dict_any_as_is(text)
{
let maybe_found_non_latin = false;
for(let i = 0; i < text.length; i++)
{
let codepoint = text.codePointAt(i);
if(codepoint > 0x100)
maybe_found_non_latin = true;
}
if(maybe_found_non_latin)
{
for(let dict of json_dicts)
{
let ret = json_lookup_arbitrary_as_is(dict, text);
if(ret !== undefined)
return ret;
}
}
return undefined;
}
let dict = [];
let lookup_kan = new Map();
let lookup_kana = new Map();
let dictsloaded = 0;
function builddict()
{
if (this.readyState === 4)
{
if (this.status === 200)
{
dict = dict.concat(JSON.parse(this.responseText));
dictsloaded++;
}
else
console.error(xhr.statusText);
}
}
let lookup_audio = new Set();
let lookup_audio_broken = new Map();
function build_audio_table()
{
if (this.readyState === 4)
{
if (this.status === 200)
{
let i = 0;
let j = 0;
while ((j = this.responseText.indexOf("\n", i)) !== -1)
{
let text = this.responseText.substring(i, j);
if(!text.includes(","))
lookup_audio.add(text);
else
lookup_audio_broken.set(text.split(",")[1], text.split(",")[0]);
i = j + 1;
}
let text = this.responseText.substring(i, j);
if(!text.includes(","))
lookup_audio.add(text);
else
lookup_audio_broken.set(text.split(",")[1], text.split(",")[0]);
}
else
console.error(xhr.statusText);
}
}
let kanji_data = new Map();
function load_kanji()
{
if (this.readyState === 4)
{
if (this.status === 200)
{
let data = JSON.parse(this.responseText);
for(let entry of data)
kanji_data.set(entry["c"], entry);
}
else
console.error(xhr.statusText);
}
}
// Having a >4MB flat text file with stupid adhoc formatting is okay
// but having a >4MB json file with nothing weird in it isn't
// Thanks, linter team!
function load_part_of_dictionary(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", builddict);
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
function load_jdic_audio_table(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", build_audio_table);
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
function load_kanji_data(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", load_kanji);
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_part_of_dictionary("dict/JMdict1.json");
load_part_of_dictionary("dict/JMdict2.json");
load_part_of_dictionary("dict/JMdict3.json");
load_part_of_dictionary("dict/JMdict4.json");
load_part_of_dictionary("dict/JMdict5.json");
load_part_of_dictionary("dict/JMdict6.json");
load_part_of_dictionary("dict/JMdict7.json");
load_part_of_dictionary("dict/JMdict8.json");
load_part_of_dictionary("dict/JMdict9.json");
load_part_of_dictionary("dict/JMdict10.json");
load_part_of_dictionary("dict/JMdict11.json");
load_jdic_audio_table("dict/jdic audio.txt");
load_kanji_data("dict/kanjidata.json");
function buildlookups()
{
// Build map of spellings to dictionary entry.
for (let i = 0; i < dict.length; i++)
{
let entry = dict[i];
if (entry.k_ele != undefined) for (let j = 0; j < entry.k_ele.length; j++)
{
let s = entry.k_ele[j];
if (!lookup_kan.has(s.keb))
lookup_kan.set(s.keb, [i]);
else
lookup_kan.get(s.keb).push(i);
}
for (let j = 0; j < entry.r_ele.length; j++)
{
let s = entry.r_ele[j];
if (!lookup_kana.has(s.reb))
lookup_kana.set(s.reb, [i]);
else
lookup_kana.get(s.reb).push(i);
}
}
for (let i = 0; i < 5; i++)
{
console.log(dict[i]);
}
console.log("built lookup tables");
console.log("size:");
console.log(lookup_kan.size);
console.log(lookup_kana.size);
console.log("dict size:");
console.log(dict.length);
}
let waiter = undefined;
function waittobuildlookups()
{
if(dictsloaded == 11)
{
clearInterval(waiter);
waiter = undefined;
buildlookups();
}
}
waiter = setInterval(waittobuildlookups, 1000);
// In JMdict, part-of-speech tags are XML entities.
// We processed JMdict's XML with entity processing disabled so we can just use the bare tags (e.g. "v1", not "ichidan verb").
// This left the entity syntax &this; behind so we want to cut it off when we return results.
function clip(str)
{
if(str == undefined)
return;
if(str.length <= 2)
return "";
return str.substring(1, str.length-1);
}
function getfromdict(indexes, text)
{
let ret = [];
for(let i = 0; i < indexes.length; i++)
{
let entry = {};
Object.assign(entry, dict[indexes[i]]);
entry.from = text;
if(entry.k_ele)
{
for(let spelling of entry.k_ele)
{
if(spelling.keb == entry.from)
{
entry.found = spelling;
break;
}
}
}
if(!entry.found && entry.r_ele)
{
for(let spelling of entry.r_ele)
{
if(spelling.reb == entry.from)
{
entry.found = spelling;
break;
}
}
}
let lastpos = [];
// JMdict tags part-of-speech for senses sparsely
// e.g. if 1. and 2. share the same list of POSs, only 1. will have a POS list
// undoing this here makes things in the content script WAY less inconvenient
for(let j = 0; j < entry.sense.length; j++)
{
if(entry.sense[j].pos)
{
for(let t = 0; t < entry.sense[j].pos.length; t++)
{
if(entry.sense[j].pos[t].charAt(0)=="&")
entry.sense[j].pos[t] = clip(entry.sense[j].pos[t]);
}
}
else
entry.sense[j].pos = lastpos;
lastpos = entry.sense[j].pos;
}
ret.push(entry);
}
return ret;
}
function replace_hira_with_kata(text)
{
let newtext = "";
for(let i = 0; i < text.length; i++)
{
let codepoint = text.codePointAt(i);
if(codepoint >= 0x3040 && codepoint <= 0x3096)
codepoint += (0x30A0 - 0x3040);
else if(codepoint >= 0x309D && codepoint <= 0x309E)
codepoint += (0x30A0 - 0x3040);
newtext += String.fromCodePoint(codepoint);
}
return newtext;
}
function replace_kata_with_hira(text)
{
let newtext = "";
for(let i = 0; i < text.length; i++)
{
let codepoint = text.codePointAt(i);
if(codepoint >= 0x30A0 && codepoint <= 0x30F6)
codepoint -= (0x30A0 - 0x3040);
else if(codepoint >= 0x30FD && codepoint <= 0x30FE)
codepoint -= (0x30A0 - 0x3040);
newtext += String.fromCodePoint(codepoint);
}
return newtext;
}
function search_inner(text)
{
if (lookup_kan.has(text))
return getfromdict(lookup_kan.get(text), text);
else if (lookup_kana.has(text))
return getfromdict(lookup_kana.get(text), text);
else
return;
}
function search(text)
{
let ret = undefined;
ret = search_inner(text);
if(ret) return ret;
ret = search_inner(replace_hira_with_kata(text));
if(ret) return ret;
ret = search_inner(replace_kata_with_hira(text));
if(ret) return ret;
ret = json_dict_any_as_is(text);
if(ret)
{
let r_ele = [{reb: ret.r}];
let k_ele = [];
let seq = "NONE_"+text;
let sense = [{gloss: [], pos: []}];
for(let spelling of ret.s)
{
if(spelling == "" || is_kana(spelling))
continue;
k_ele.push({keb: spelling});
}
let re_ret = undefined;
if(k_ele == undefined || k_ele.length == 0)
re_ret = [{r_ele: r_ele, seq: seq, sense: sense, from: text, found: r_ele[0]}];
else
re_ret = [{k_ele: k_ele, r_ele: r_ele, seq: seq, sense: sense, from: text, found: k_ele[0]}];
return re_ret;
}
}
// priority rules
let priority_rules = [];
function load_priority_rules(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", () =>
{
if (req.readyState === 4)
{
console.log("loaded priority rules");
if (req.status === 200)
priority_rules = JSON.parse(req.responseText);
else
console.error(req.statusText);
}
});
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_priority_rules("dict/priority.json");
// vn frequency data
let freq_vns = {};
function load_freqlist_vns(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", () =>
{
if (req.readyState === 4)
{
console.log("loaded vn freq data");
if (req.status === 200)
freq_vns = JSON.parse(req.responseText);
else
console.error(req.statusText);
}
});
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_freqlist_vns("dict/freqlist_vns.json");
// narou frequency data
let freq_narou = {};
function load_freqlist_narou(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", () =>
{
if (req.readyState === 4)
{
console.log("loaded narou freq data");
if (req.status === 200)
freq_narou = JSON.parse(req.responseText);
else
console.error(req.statusText);
}
});
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_freqlist_narou("dict/freqlist_narou.json");
// narou frequency data
let freq_novels = {};
function load_freqlist_novels(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", () =>
{
if (req.readyState === 4)
{
console.log("loaded novels freq data");
if (req.status === 200)
freq_novels = JSON.parse(req.responseText);
else
console.error(req.statusText);
}
});
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_freqlist_novels("dict/freqlist_novels.json");
// deconjugation rules
let rules = [];
function load_deconjugation_rules(filename)
{
let req = new XMLHttpRequest();
req.addEventListener("load", () =>
{
if (req.readyState === 4)
{
console.log("loaded deconjugation rules");
if (req.status === 200)
rules = JSON.parse(req.responseText);
else
console.error(req.statusText);
}
});
req.open("GET", browser.extension.getURL(filename));
req.send();
return req;
}
load_deconjugation_rules("dict/deconjugator.json");
// return deconjugated form if stdrule applies to form, return otherwise
function stdrule_deconjugate_inner(my_form, my_rule)
{
// ending doesn't match
if(!my_form.text.endsWith(my_rule.con_end))
return;
// tag doesn't match
if(my_form.tags.length > 0 && my_form.tags[my_form.tags.length-1] != my_rule.con_tag)
return;
let newtext = my_form.text.substring(0, my_form.text.length-my_rule.con_end.length)+my_rule.dec_end;
// I hate javascript reeeeeeeeeeeeeee
let newform = {};//new Object();
newform.text = newtext;
newform.original_text = my_form.original_text;
newform.tags = my_form.tags.slice();
newform.seentext = new Set([...my_form.seentext]);
newform.process = my_form.process.slice();
newform.text = newtext;
newform.process.push(my_rule.detail);
if(newform.tags.length == 0)
newform.tags.push(my_rule.con_tag);
newform.tags.push(my_rule.dec_tag);
if(newform.seentext.size == 0)
newform.seentext.add(my_form.text);
newform.seentext.add(newtext);
return newform;
};
function stdrule_deconjugate(my_form, my_rule)
{
// can't deconjugate nothingness
if(my_form.text == "")
return;
// deconjugated form too much longer than conjugated form
if(my_form.text.length > my_form.original_text.length+10)
return;
// impossibly information-dense
if(my_form.tags.length > my_form.original_text.length+6)
return;
// blank detail mean it can't be the last (first applied, but rightmost) rule
if(my_rule.detail == "" && my_form.tags.length == 0)
return;
let array = undefined;
// pick the first one that is an array
// FIXME: use minimum length for safety reasons? assert all arrays equal length?
if(Array.isArray(my_rule.dec_end))
array = my_rule.dec_end;
else if(Array.isArray(my_rule.con_end))
array = my_rule.con_end;
else if(Array.isArray(my_rule.dec_tag))
array = my_rule.dec_tag;
else if(Array.isArray(my_rule.con_tag))
array = my_rule.con_tag;
if(array == undefined)
return stdrule_deconjugate_inner(my_form, my_rule);
else
{
let collection = new Set();
for(let i = 0; i < array.length; i++)
{
let virtual_rule = {};
virtual_rule.type = my_rule.type;
let index_or_value = function (variable, index)
{
if(Array.isArray(variable))
return variable[index];
else
return variable;
}
virtual_rule.dec_end = index_or_value(my_rule.dec_end, i);
virtual_rule.con_end = index_or_value(my_rule.con_end, i);
virtual_rule.dec_tag = index_or_value(my_rule.dec_tag, i);
virtual_rule.con_tag = index_or_value(my_rule.con_tag, i);
virtual_rule.detail = my_rule.detail;
let ret = stdrule_deconjugate_inner(my_form, virtual_rule);
if(ret) collection.add(ret);
}
return collection;
}
};
function rewriterule_deconjugate(my_form, my_rule)
{
if(my_form.text != my_rule.con_end)
return;
return stdrule_deconjugate(my_form, my_rule);
};
function onlyfinalrule_deconjugate(my_form, my_rule)
{
if(my_form.tags.length != 0)
return;
return stdrule_deconjugate(my_form, my_rule);
};
function neverfinalrule_deconjugate(my_form, my_rule)
{
if(my_form.tags.length == 0)
return;
return stdrule_deconjugate(my_form, my_rule);
};
function contextrule_deconjugate(my_form, my_rule)
{
if(!context_functions[my_rule.contextrule](my_form, my_rule))
return;
return stdrule_deconjugate(my_form, my_rule);
};
function substitution_inner(my_form, my_rule)
{
if(!my_form.text.includes(my_rule.con_end))
return;
let newtext = my_form.text.replace(new RegExp(my_rule.con_end, 'g'), my_rule.dec_end);
// I hate javascript reeeeeeeeeeeeeee
let newform = {};//new Object();
newform.text = newtext;
newform.original_text = my_form.original_text;
newform.tags = my_form.tags.slice();
newform.seentext = new Set([...my_form.seentext]);
newform.process = my_form.process.slice();
newform.text = newtext;
newform.process.push(my_rule.detail);
if(newform.seentext.size == 0)
newform.seentext.add(my_form.text);
newform.seentext.add(newtext);
return newform;
};
function substitution_deconjugate(my_form, my_rule)
{
if(my_form.process.length != 0)
return;
// can't deconjugate nothingness
if(my_form.text == "")
return;
let array = undefined;
// pick the first one that is an array
// FIXME: use minimum length for safety reasons? assert all arrays equal length?
if(Array.isArray(my_rule.dec_end))
array = my_rule.dec_end;
else if(Array.isArray(my_rule.con_end))
array = my_rule.con_end;
if(array == undefined)
return substitution_inner(my_form, my_rule);
else
{
let collection = new Set();
for(let i = 0; i < array.length; i++)
{
let virtual_rule = {};
virtual_rule.type = my_rule.type;
let index_or_value = function (variable, index)
{
if(Array.isArray(variable))
return variable[index];
else
return variable;
}
virtual_rule.dec_end = index_or_value(my_rule.dec_end, i);
virtual_rule.con_end = index_or_value(my_rule.con_end, i);
virtual_rule.detail = my_rule.detail;
let ret = substitution_inner(my_form, virtual_rule);
if(ret) collection.add(ret);
}
return collection;
}
}
let rule_functions = {
stdrule: stdrule_deconjugate,
rewriterule: rewriterule_deconjugate,
onlyfinalrule: onlyfinalrule_deconjugate,
neverfinalrule: neverfinalrule_deconjugate,
contextrule: contextrule_deconjugate,
substitution: substitution_deconjugate
};
function v1inftrap_check(my_form, my_rule)
{
if(my_form.tags.length != 1) return true;
let my_tag = my_form.tags[0];
if(my_tag == "stem-ren")
return false;
return true;
};
function saspecial_check(my_form, my_rule)
{
if(my_form.text == "") return false;
if(!my_form.text.endsWith(my_rule.con_end)) return false;
let base_text = my_form.text.substring(0, my_form.text.length-my_rule.con_end.length);
if(base_text.endsWith("さ"))
return false;
return true;
};
let context_functions = {
v1inftrap: v1inftrap_check,
saspecial: saspecial_check,
};
function union(setA, setB)
{
for (let elem of setB)
setA.add(elem);
return setA;
}
// Returns a set of objects, each containing the "final form" of a series of deconjugations
// The majority of these objects will fail being compared to the dictionary forms
// The algorithm is a right-edge rule checker that finds all applicable series of rules.
// TODO: add all underlay rule types
function deconjugate(mytext)
{
let processed = new Set();
let novel = new Set();
let start_form = {text: mytext, original_text: mytext, tags: [], seentext: new Set(), process: [] };
novel.add(start_form);
let myrules = rules;
try
{
if(settings.deconjugator_rules_json != "")
myrules = JSON.parse(settings.deconjugator_rules_json);
}
catch(err)
{
console.log("error while parsing deconjugation rules:");
console.log(err);
}
while(novel.size > 0)
{
//print_object(novel);
let new_novel = new Set();
for(let form of novel)
{
for(let rule of myrules)
{
if(!(rule instanceof Object))
continue;
let newform = rule_functions[rule.type](form, rule);
if(newform != undefined && newform.constructor === Set)
{
for(let myform of newform)
{
if(myform != undefined && !processed.has(myform) && !novel.has(myform) && !new_novel.has(myform))
new_novel.add(myform);
}
}
else if(newform != undefined && !processed.has(newform) && !novel.has(newform) && !new_novel.has(newform))
new_novel.add(newform);
}
}
processed = union(processed, novel);
novel = new_novel;
}
return processed;
}
// takes deconjugated forms and looks them up in a dictionary
// building a list of all matches
function build_lookup_comb(forms)
{
// FIXME: dumpster fire that shouldn't be anywhere near as complicated as I made it
// This is the only part of this function that doesn't look like a garbage fire.
// Looks for dictionary definitions of each deconjugated form.
let looked_up = {};
for(let form of forms)
{
if(!looked_up.hasOwnProperty(form.text))
{
let result = search(form.text);
let copied_result = [];
if(result)
{
for(let r = 0; r < result.length; r++)
{
let entry = result[r];
//Object.assign(entry, result[r]);
entry.deconj = undefined; // clear if something set it before, we need this later
// store all the parts of speech that this dictionary entry can apply to
// normally, they're scattered across its senses
// FIXME: care about senses with restricted spellings when building part-of-speech tags list
entry.allpos = new Set();
for(let i = 0; i < entry.sense.length; i++)
{
let sense = entry.sense[i];
for(let j = 0; j < sense.pos.length; j++)
entry.allpos.add(sense.pos[j]);
}
copied_result.push(entry);
}
looked_up[form.text] = copied_result;
}
}
}
// FIXME: This is garbage.
// Add deconjugation field to all those entries
for(let form of forms)
{
if(looked_up.hasOwnProperty(form.text))
{
let result = looked_up[form.text];
for(let entry of result)
{
// If the form requires definition to have a particular POS tag, check for it
if(form.tags.length > 0)
{
if(entry.allpos.has(form.tags[form.tags.length-1]))
{
if(entry.deconj === undefined)
entry.deconj = new Set();
entry.deconj.add(form);
}
}
else
{
if(entry.deconj === undefined)
entry.deconj = new Set();
entry.deconj.add(form);
}
}
}
}
// FIXME: This should merge based on map from JMdict entity ID to entry.
// FIXME: Because it's theoretically possible to get the same definition from different spellings from different deconjugations of the same string.
// Make a list of each definition that has an associated deconjugation (even if it's a zero-depth deconjugation)
let merger = [];
for(let result of Object.values(looked_up))
{
for(let entry of result)
{
if(entry.deconj)
{
entry.deconj = Array.from(entry.deconj);
merger.push(entry);
}
}
}
//print_object(merger);
if(merger.length > 0)
return merger;
else
return;
}
function json_lookup_kanji(dict, spelling_list, readings, inexact)
{
let exclude = settings.json_ignore_entries.split(",").map(x => x.trim());
let indexes_set = new Set();
let indexes = [];
for(let spelling of spelling_list)
{
let possibilities = dict.lookup_json_kan.get(spelling);
if(!possibilities)
continue;
for(let id of dict.lookup_json_kan.get(spelling))
{
let bad = false;
for(let spelling of dict.entries[id]["s"])
{
if(exclude.includes(spelling))
{
bad = true;
break;
}
}
if(bad)
break;
if(inexact || readings.includes(dict.entries[id]["r"]))
{
if(!indexes_set.has(id))
{
indexes_set.add(id);
indexes.push(id);
}
}
}
}
return indexes;
}
function json_lookup_kana_exact(dict, kana)
{
let exclude = settings.json_ignore_entries.split(",").map(x => x.trim());