-
Notifications
You must be signed in to change notification settings - Fork 3
/
lawdata.ts
168 lines (150 loc) · 5.96 KB
/
lawdata.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
import * as std from "../law/std";
import * as analyzer from "../analyzer";
import * as util from "../util";
import { parse } from "../parser/lawtext";
import { EL } from "../node/el";
import { ErrorMessage } from "../parser/cst/error";
import { LawXMLStruct } from "./loaders/common";
import { ElawsLawData } from "../elawsApi";
import { xmlToEL } from "../node/el/xmlToEL";
import addSentenceChildrenControls from "../parser/addSentenceChildrenControls";
export interface LawDataCore {
el: std.Law,
analysis: analyzer.Analysis,
pictURL: Map<string, {url: string, type: string}>;
}
export interface BaseXMLLawDataProps {
xml: string,
lawXMLStruct: LawXMLStruct | null,
}
export interface BaseLawtextLawDataProps {
lawtext: string,
}
export type BaseLawDataProps = BaseXMLLawDataProps | BaseLawtextLawDataProps;
export type LawDataResult<TLawDataProps extends BaseLawDataProps> =
{ ok: true, lawData: LawDataCore & TLawDataProps, lawtextErrors?: ErrorMessage[]} | {ok: false, error: Error};
export class Timing {
public searchLawNum: number | null = null;
public fetchStoredLawInfo: number | null = null;
public loadData: number | null = null;
public extractPict: number | null = null;
public parseXMLOrLawtext: number | null = null;
public addControlTags: number | null = null;
public analyze: number | null = null;
public updateComponents: number | null = null;
public toString(): string {
return `
Timing {
searchLawNum: ${this.searchLawNum} ms,
fetchStoredLawInfo: ${this.fetchStoredLawInfo} ms,
loadData: ${this.loadData} ms,
extractPict: ${this.extractPict} ms,
parseXMLOrLawtext: ${this.parseXMLOrLawtext} ms,
addControlTags: ${this.addControlTags} ms,
analyze: ${this.analyze} ms,
updateComponents: ${this.updateComponents} ms,
}
`.trim();
}
}
function *findFig(el: EL): Iterable<std.Fig> {
if (std.isFig(el)) {
yield el;
}
for (const child of el.children) {
if (typeof child === "string") continue;
yield* findFig(child);
}
}
export const toLawData = async <TLawDataProps extends BaseLawDataProps>(
props: TLawDataProps,
onMessage: (message: string) => unknown,
timing: Timing,
): Promise<LawDataResult<TLawDataProps>> => {
const _props = props as BaseLawDataProps;
try {
if ("xml" in _props) {
onMessage("法令XMLをパースしています...");
// console.log("toLawData: parsing law xml...");
await util.wait(30);
const [parseXMLOrLawtextTime, el] = await util.withTime(xmlToEL)(_props.xml);
timing.parseXMLOrLawtext = parseXMLOrLawtextTime;
onMessage("制御タグを追加しています...");
// console.log("onNavigated: adding control tags...");
await util.wait(30);
const [addControlTagsTime] = await util.withTime(addSentenceChildrenControls)(el as std.Law);
timing.addControlTags = addControlTagsTime;
onMessage("法令を解析しています...");
// console.log("onNavigated: analysing law...");
await util.wait(30);
const [analyzeTime, analysis] = await util.withTime(analyzer.analyze)(el as std.Law);
timing.analyze = analyzeTime;
const pictURL = new Map<string, {url: string, type: string}>();
const lawXMLStruct = _props.lawXMLStruct;
if (lawXMLStruct) {
onMessage("画像情報を読み込んでいます...");
await util.wait(30);
const start = new Date();
if (lawXMLStruct instanceof ElawsLawData) {
const pict = await lawXMLStruct.ensurePict();
if (pict) {
for (const [src] of pict.entries()) {
const url = await lawXMLStruct.getPictFileOrBlobURL(src);
if (url) pictURL.set(src, url);
}
}
} else {
for (const fig of findFig(el)) {
const src = fig.attr.src;
if (!src) continue;
const url = await lawXMLStruct.getPictFileOrBlobURL(src);
if (url) pictURL.set(src, url);
}
}
timing.extractPict = (new Date()).getTime() - start.getTime();
}
return {
ok: true,
lawData: {
...(_props as typeof props),
el: el as std.Law,
analysis,
pictURL,
},
};
} else if ("lawtext" in _props) {
onMessage("Lawtextをパースしています...");
// console.log("onNavigated: parsing lawtext...");
await util.wait(30);
const [parseXMLOrLawtextTime, { value: el, errors }] = await util.withTime(parse)(_props.lawtext);
timing.parseXMLOrLawtext = parseXMLOrLawtextTime;
onMessage("法令を解析しています...");
// console.log("onNavigated: analysing law...");
await util.wait(30);
const [analyzeTime, analysis] = await util.withTime(analyzer.analyze)(el);
timing.analyze = analyzeTime;
return {
ok: true,
lawData: {
...(_props as typeof props),
el: el as std.Law,
analysis,
pictURL: new Map(),
},
lawtextErrors: errors,
};
} else {
throw util.assertNever(_props);
}
} catch (origError) {
const error = new Error(`読み込んだ法令データにエラーがあります: ${origError}`);
error.stack = `${error.stack}
Original Error:
${(origError as Error).stack}
`;
return {
ok: false,
error,
};
}
};