/
demo.ts
164 lines (154 loc) · 3.26 KB
/
demo.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
/*
Run this as:
```console
npm run demo
```
This will save some output files.
*/
import { sentenceToGraph } from "./index";
import type { Sentence, Tree } from "./interfaces";
import { reverse } from "./utils";
import { writeFileSync } from "fs";
import { execSync } from "child_process";
function treeToDot(keyToPrev: Tree, textToKeys: Tree) {
const keyToText = reverse(textToKeys);
if (!Object.values(keyToText).every((v) => v.length === 1)) {
throw new Error("key not unique?");
}
const links: string[] = [];
const nodes = new Map<string, string>();
function addNode(key: string) {
nodes.set(key, ` ${key} [label="${keyToText[key][0]}"]`);
}
for (const [key, parents] of Object.entries(keyToPrev)) {
addNode(key);
for (const parent of parents) {
addNode(parent);
links.push(` ${parent} -> ${key}`);
}
}
return `digraph graphname {
${Array.from(nodes.values()).join("\n")}
${links.join("\n")}
}`.replace(/\//g, "_");
}
export const demo: Sentence = {
furigana: [
{
ruby: "京",
rt: "きょう",
},
{
ruby: "都",
rt: "と",
},
"で",
"たくさん",
{
ruby: "写",
rt: "しゃ",
},
{
ruby: "真",
rt: "しん",
},
"を",
{
ruby: "撮",
rt: "と",
},
"り",
"まし",
"た",
],
synonyms: [
[
"たくさん",
[
{
ruby: "沢",
rt: "たく",
},
{
ruby: "山",
rt: "さん",
},
],
],
["りまし", ["っ"]],
[
"たくさん写真を",
[
{
ruby: "写",
rt: "しゃ",
},
{
ruby: "真",
rt: "しん",
},
"を",
{
ruby: "沢",
rt: "たく",
},
{
ruby: "山",
rt: "さん",
},
],
],
],
};
export const fake: Sentence = {
furigana: "abcd".split(""),
synonyms: [
["bc", "xyz".split("")],
["d", ["e"]],
["c", ["CcC"]],
["a", ["AAA"]],
],
};
export function sentenceToDot(sentence: Sentence, outfile = "demo.dot") {
const res = sentenceToGraph(sentence);
writeFileSync(outfile, treeToDot(res.keyToPrev, res.textToKeys));
const cmds = [
`dot -Tsvg ${outfile} -o${outfile}.svg`,
`dot -Tpng ${outfile} -o${outfile}.png`,
];
try {
for (const cmd of cmds) execSync(cmd);
} catch {
console.error(`Unable to generate PNG graph. Maybe 'dot' isn't installed? I tried to run
$ ${cmds.join("; ")}`);
}
}
if (module === require.main) {
sentenceToDot(demo, "demo.dot");
sentenceToDot(fake, "fake.dot");
const ex: Sentence = {
furigana: [
["これ"],
["は"],
[{ ruby: "兄", rt: "あに" }],
["が"],
[
{ ruby: "予", rt: "よ" },
{ ruby: "約", rt: "やく" },
],
["し"],
["た"],
[
{ ruby: "旅", rt: "りょ" },
{ ruby: "館", rt: "かん" },
],
["です"],
].flat(),
// english: [
// "This is the Japanese inn that my older brother made a reservation at",
// ],
// citation:
// "My answer to Genki II Workbook, Lesson 15, Topic 4, section II, question 1",
};
console.log(sentenceToGraph(ex));
}