1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TableDict = void 0;
|
4 | const segment_1 = require("segment-dict/lib/loader/segment");
|
5 | const cjk_1 = require("../util/cjk");
|
6 | const core_1 = require("./core");
|
7 | const util_1 = require("../util");
|
8 |
|
9 |
|
10 |
|
11 | class TableDict extends core_1.default {
|
12 | constructor() {
|
13 | super(...arguments);
|
14 | this.TABLE = {};
|
15 | this.TABLE2 = {};
|
16 | }
|
17 | exists(data) {
|
18 | let w, p, f;
|
19 | if (typeof data == 'string') {
|
20 | w = data;
|
21 | }
|
22 | else if (Array.isArray(data)) {
|
23 | [w, p, f] = data;
|
24 | }
|
25 | else {
|
26 | ({ w, p, f } = data);
|
27 | }
|
28 | return this.TABLE[w] || null;
|
29 | }
|
30 | __handleInput(data) {
|
31 | let w, p, f;
|
32 | let plus;
|
33 | if (typeof data == 'string') {
|
34 | w = data;
|
35 | }
|
36 | else if (Array.isArray(data)) {
|
37 | [w, p, f, ...plus] = data;
|
38 | }
|
39 | else {
|
40 | ({ w, p, f } = data);
|
41 | }
|
42 | if (typeof w !== 'string' || w === '') {
|
43 | throw new TypeError(JSON.stringify(data));
|
44 | }
|
45 | p = (typeof p != 'number' || Number.isNaN(p)) ? 0 : p;
|
46 | f = (typeof f != 'number' || Number.isNaN(f)) ? 0 : f;
|
47 | return {
|
48 | data: {
|
49 | w, p, f,
|
50 | },
|
51 | plus,
|
52 | };
|
53 | }
|
54 | add(data, skipExists) {
|
55 | let w, p, f;
|
56 | let plus;
|
57 | {
|
58 | let ret = this.__handleInput(data);
|
59 | ({ w, p, f } = ret.data);
|
60 | plus = ret.plus;
|
61 | }
|
62 | if (skipExists && this.exists(w)) {
|
63 | return this;
|
64 | }
|
65 | if (plus && plus.length) {
|
66 |
|
67 | }
|
68 | this._add({ w, p, f, s: true });
|
69 | let self = this;
|
70 | |
71 |
|
72 |
|
73 |
|
74 | if (1 && this.options.autoCjk) {
|
75 | let wa = cjk_1.text_list(w);
|
76 | wa.forEach(function (w2) {
|
77 | if (w2 != w && !self.exists(w2)) {
|
78 | self._add({ w: w2, p, f });
|
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 | return this;
|
109 | }
|
110 | _add({ w, p, f, s }) {
|
111 | let len = w.length;
|
112 | this.TABLE[w] = {
|
113 | p,
|
114 | f,
|
115 | s,
|
116 | };
|
117 | if (!this.TABLE2[len])
|
118 | this.TABLE2[len] = {};
|
119 | this.TABLE2[len][w] = this.TABLE[w];
|
120 | }
|
121 | remove(target) {
|
122 | let { data, plus } = this.__handleInput(target);
|
123 | this._remove(data);
|
124 | return this;
|
125 | }
|
126 | _remove({ w, p, f, s }) {
|
127 | let len = w.length;
|
128 | delete this.TABLE[w];
|
129 | if (this.TABLE2[len]) {
|
130 | delete this.TABLE2[len][w];
|
131 | }
|
132 | return this;
|
133 | }
|
134 | json() {
|
135 | return util_1.cloneDeep(this.TABLE);
|
136 | }
|
137 | |
138 |
|
139 |
|
140 | stringify(LF = "\n") {
|
141 | let self = this;
|
142 | return Object.entries(self.TABLE)
|
143 | .reduce(function (a, [w, { p, f }]) {
|
144 | let line = segment_1.stringifyLine([w, p, f]);
|
145 | a.push(line);
|
146 | return a;
|
147 | }, [])
|
148 | .join(typeof LF === 'string' ? LF : "\n");
|
149 | }
|
150 | }
|
151 | exports.TableDict = TableDict;
|
152 | exports.default = TableDict;
|
153 |
|
\ | No newline at end of file |