-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathkeyboard.ts
223 lines (197 loc) · 5.83 KB
/
keyboard.ts
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
import { download } from "./common";
import { product } from "./number";
import { validCombines } from "./pinyin";
import configs from "./spconfig.json";
declare global {
type ShuangpinType = keyof typeof configs;
/**
* ```ts
* {
* keyMap: [ // 按键映射
* '${键盘按键小写字符}/${韵母1,韵母2...}/${声母1,声母2...}'
* ],
* zeroMap: [ // 零声母映射
* '${双拼按键}/${对应拼音}'
* ],
* additionalMap: [ // 附加映射
* '${双拼按键}/${对应拼音}'
* ],
* excludingMap: [ // 不匹配映射
* '${双拼按键}/${对应拼音}'
* ]
* }
* ```
*/
type RawShuangPinConfig = {
keyMap: string[],
zeroMap: string[],
additionalMap?: string[],
excludingMap?: string[],
};
type ShuangpinMode = ShuangpinConfig;
}
export const PresetConfigs = configs;
export function encodeShuangpin() {}
export class ShuangpinConfig {
groupByKey = new Map<Char, KeyConfig>(); // 键盘 -> KeyConfig
groupByFollow = new Map<string, KeyConfig>(); // 声母 -> KeyConfig
groupByLead = new Map<string, KeyConfig>(); // 韵母 -> KeyConfig
sp2zero = new Map<string, string>(); // 零声母 -> 双拼
zero2sp = new Map<string, string>(); // 双拼 -> 零声母
sp2py = new Map<string, string>(); // 双拼 -> 拼音
py2sp = new Map<string, string>(); // 拼音 -> 双拼
spNot2py = new Map<string, string>(); // 不匹配映射:双拼 -> 拼音
constructor(
public name: string,
public config: RawShuangPinConfig,
public custom = false
) {
for (const line of config["keyMap"]) {
const [main, follow, lead] = line.split("/");
if (!follow && !lead) {
continue;
}
const keyConfig: KeyConfig = {
main: (main as Char) ?? "",
follows: follow?.split(",") ?? [],
leads: lead?.split(",") ?? [],
};
this.groupByKey.set(keyConfig.main, keyConfig);
keyConfig.leads.forEach((lead) => this.groupByLead.set(lead, keyConfig));
keyConfig.follows.forEach((follow) =>
this.groupByFollow.set(follow, keyConfig)
);
}
for (const line of config["zeroMap"]) {
const [sp, zero] = line.split("/");
this.sp2zero.set(sp, zero);
this.zero2sp.set(zero, sp);
this.py2sp.set(zero, sp);
this.sp2py.set(sp, zero);
}
const additionalMap = config["additionalMap"];
if (additionalMap) {
for (const line of additionalMap) {
const [sp, pinyin] = line.split("/");
this.py2sp.set(pinyin, sp);
this.sp2py.set(sp, pinyin);
}
}
const excludingMap = config["excludingMap"];
if (excludingMap) {
for (const line of excludingMap) {
const [sp, pinyin] = line.split("/");
this.spNot2py.set(sp, pinyin);
}
}
const allCombs = product(
[...this.groupByKey.keys()],
[...this.groupByKey.keys()]
);
for (const [lead, follow] of allCombs) {
const sp = lead + follow;
const leads = this.groupByKey.get(lead)!.leads;
const follows = this.groupByKey.get(follow)!.follows;
const pinyins = product(leads, follows);
for (const [l, f] of pinyins) {
const pinyin = l + f;
if (!(l && f)) {
continue;
}
if (pinyin == this.spNot2py.get(sp)) {
continue;
}
if (validCombines.has(pinyin)) {
this.py2sp.set(pinyin, sp.concat(this.py2sp.get(pinyin) ?? ""));
this.sp2py.set(sp, pinyin);
}
}
}
}
download() {
download(this.name, JSON.stringify(this.config));
}
}
export const keyboardLayout = ["qwertyuiop", " asdfghjkl;", "zxcvbnm"];
export function mergeString([a, b]: string[] = []) {
if (!(a && b && a.length > 2 && b.length > 2)) {
return [a, b].join(" ");
}
const aChars = a.split("");
const bChars = b.split("");
const commonSuffix = [];
while (
aChars.length &&
bChars.length &&
aChars[aChars.length - 1] === bChars[bChars.length - 1]
) {
commonSuffix.push(aChars.pop());
bChars.pop();
}
const prefixs = [aChars.join(""), bChars.join("")].filter(
(v) => v.length > 0
);
if (commonSuffix.length === 0) {
return prefixs.join(" ");
}
const prefix = prefixs.join("/");
const suffix = commonSuffix.reverse().join("");
return `(${prefix})${suffix}`;
}
export function mapConfigToLayout(config: ShuangpinMode) {
return keyboardLayout.map((v) =>
v.split("").map((key) => {
const keyConfig = config.groupByKey.get(key as Char) ?? {
main: key,
leads: [],
follows: [],
};
return {
main: keyConfig.main,
lead: mergeString(keyConfig.leads.filter((v) => v !== keyConfig.main)),
follow: mergeString(keyConfig.follows),
leads: keyConfig.leads,
follows: keyConfig.follows,
};
})
);
}
export function matchSpToPinyin(
mode: ShuangpinMode,
leadKey: Char,
followKey: Char,
targetPinyin: string
) {
const allMatched = !!leadKey && !!followKey;
const leads = mode.groupByKey.get(leadKey)?.leads ?? [];
const follows = mode.groupByKey.get(followKey)?.follows ?? [];
const combines = product(leads.concat(""), follows.concat("")).filter(
([a, b]) => !!a || !!b
);
let lead = leadKey as string;
let follow = followKey as string;
const sp = (lead ?? "") + (follow ?? "");
const valid = mode.sp2py.get(sp) === targetPinyin;
if (valid) {
lead = validCombines.get(targetPinyin)!.lead;
follow = validCombines.get(targetPinyin)!.follow;
}
if (!valid) {
// 匹配声母的情况
combines.some(([l, f]) => {
if (l.length && targetPinyin?.startsWith(l)) {
lead = l;
return true;
}
if (f.length && targetPinyin?.endsWith(f)) {
follow = f;
return true;
}
});
}
return {
valid: valid && allMatched,
lead,
follow,
};
}