-
Notifications
You must be signed in to change notification settings - Fork 0
/
flypy.js
153 lines (142 loc) · 3.74 KB
/
flypy.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
const fs = require("fs");
let flypy = fs.readFileSync("flypy.txt", "utf-8");
flypy = flypy.split("\n"); //.splice(1, 55);
let dict = {};
let reverseDict = new Map();
flypy.map(x => {
x = x.split(",");
dict[x[0]] = x[1];
reverseDict.set(x[1], x[0]);
});
// libre
flypy = fs.readFileSync("libre-flypy.txt", "utf-8");
flypy = flypy.split("\n");
flypy.map(x => {
x = x.trim().split('\t');
const [zi, ma] = x;
if (reverseDict.has(ma)) return;
let i = 1;
while (dict[zi + "=" + i]) i++;
dict[ma + "=" + i] = zi;
}
)
flypy = null;
function 反查(ma) {
return reverseDict.get(ma);
}
let lastcode,
hzxr = "";
function lookup(word, w) {
if (!w) w = 1;
let res = dict[word + "=" + w];
return res ? res : word;
}
function parse(input) {
// parse '
let _input = input.split("'");
if (_input != [input]) {
// 奇偶交替
let is_raw = true;
_input = _input.map(part => {
is_raw = !is_raw;
return is_raw ? part : up(part);
});
let res = _input.join("");
return res;
} else return input;
}
let xparse = input => [parse(input), lastcode, lookupAll(lastcode, true)];
function lookupAll(code, formatting) {
if (code) code = lastcode;
let results = [];
for (let ele of [1, 2, 3, 4, 5]) {
const res = lookup(code, ele);
if (res == code) break;
else results.push(formatting ? ele + "." + res : res);
}
if (results == []) results = [""];
console.log(results);
return formatting ? results.join(" / ") : results;
}
function up(input) {
input = input + "␃";
let chars = input.split("");
let stack = "";
let output = [];
function dealWithStack() {
console.log("==" + stack);
if (!stack) return;
lastcode = stack;
output.push(
/[0-9]$/.test(stack) ?
lookup(stack.slice(0, -1), stack.charAt(stack.length - 1)) : lookup(stack));
stack = "";
}
const punctuations = (",./?<>~!'[{]}" + '"\\').split("");
let allowMoreSpaces = false;
let i = chars.length;
while (i > 0) {
i = i - 1;
var char = chars.shift();
//console.log(char);
let puncflag = false;
console.log({ char, stack });
if ((char === " " && allowMoreSpaces) || char === "␃") {
if (stack) dealWithStack();
} else if (char === " " && !allowMoreSpaces) {
console.info('空格', { stack })
stack ? dealWithStack() : output.push(" ");
} else if (/^[0-9]$/.test(char)) {
console.info("带数字的长度 < 4 的码", { char, stack });
if (stack) {
stack += char;
dealWithStack();
} else {
output += char;
}
} else if (/* puncflag!= false && */ punctuations.indexOf(char) != -1) {
/* if (puncflag === 1) stack = "";
else */ dealWithStack();
switch (char) {
case ".":
output.push("。");
break;
case ",":
output.push(",");
break;
case "!":
output.push("!");
break;
case "?":
output.push("?");
break;
}
stack = "";
//if (/(\.|,|\/)/.test(char)){
}
else if (stack.length == 4) {
console.log("stack length is 4", { char, stack })
if (/^[0-9]$/.test(char)) {
console.log({ char }, "is ^[0-9]$")
stack += char;
dealWithStack();
}
else {
dealWithStack();
stack = char;
}
let puncflag = 1;
} else if (/[a-z]/.test(char)) {
console.log("char is [a-z]", { char, stack });
stack += char;
} else if (/[A-Z]/.test(char)) {
stack ? dealWithStack() : output.push(char);
} else {
puncflag = 2;
}
//else if (chars === []) dealWithStack();
}
//if (stack) dealWithStack();
return output.join("");
}
module.exports = { up, parse, xparse, 反查 };