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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
import wiki, {
type eventOptions,
type eventResult,
type fcOptions,
type geoOptions,
type randomFormats,
} from "wikipedia";
import { handlePromise } from "../utils";
export async function fetchWordInWiki(s: string) {
const params = new URLSearchParams();
params.append("action", "parse");
params.append("page", s);
params.append("format", "json");
params.append("prop", "templates|text");
params.append("formatversion", "2");
const p = params.toString();
const url = `https://en.wiktionary.org/w/api.php?${p}`;
const res = await fetch(url);
console.log(res.headers.get("content-type"));
const j = await res.json();
return j.parse.text as string;
}
export async function* readWiktionaryDump() {
const file = Bun.file(
"/home/y/code/prosody/resources/wiktionary/raw-wiktextract-data.jsonl",
);
const reader = file
.stream()
.pipeThrough(new TextDecoderStream("utf-8"))
.getReader();
let remainder = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
let lines = (remainder + value).split(/\r?\n/);
remainder = lines.pop() || "";
for (const line of lines) yield line;
}
if (remainder) yield remainder;
}
function fixToday(today: eventResult) {
const empty: any = [];
today.selected = today.selected
? Array.isArray(today.selected)
? today.selected
: empty
: empty;
today.births = today.births
? Array.isArray(today.births)
? today.births
: empty
: empty;
today.deaths = today.deaths
? Array.isArray(today.deaths)
? today.deaths
: empty
: empty;
today.events = today.events
? Array.isArray(today.events)
? today.events
: empty
: empty;
today.holidays = today.holidays
? Array.isArray(today.holidays)
? today.holidays
: empty
: empty;
return today;
}
export async function fetchWikipedia(lang: string) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const setLang = wiki.setLang(lang);
const ff = wiki.featuredContent();
const rr = wiki.random();
const tt = wiki.onThisDay();
const [featured, today, random] = await Promise.all([ff, tt, rr]);
return { ok: { lang, setLang, featured, random, today: fixToday(today) } };
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaPage(title: string) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const ppage = wiki.page(title);
const pinfo = wiki.infobox(title);
const pintro = wiki.intro(title);
const pimages = wiki.images(title);
const psummary = wiki.summary(title);
const phtml = wiki.html(title);
const pmobileHtml = wiki.mobileHtml(title);
const ppdf = wiki.pdf(title);
const pcontent = wiki.content(title);
const pcategories = wiki.categories(title);
const prelated = wiki.related(title);
const pmedia = wiki.media(title);
const plinks = wiki.links(title);
const preferences = wiki.references(title);
const pcoordinates = wiki.coordinates(title);
const plangLinks = wiki.langLinks(title);
const ptables = wiki.tables(title);
const pcitation = wiki.citation(title);
const [
page,
info,
intro,
images,
summary,
html,
mobileHtml,
pdf,
content,
categories,
related,
media,
links,
references,
coordinates,
langLinks,
tables,
citation,
] = await Promise.allSettled([
ppage,
pinfo,
pintro,
pimages,
psummary,
phtml,
pmobileHtml,
ppdf,
pcontent,
pcategories,
prelated,
pmedia,
plinks,
preferences,
pcoordinates,
plangLinks,
ptables,
pcitation,
]);
return {
ok: {
page: handlePromise(page),
info: handlePromise(info),
intro: handlePromise(intro),
images: handlePromise(images),
media: handlePromise(media),
summary: handlePromise(summary),
html: handlePromise(html),
mobileHtml: handlePromise(mobileHtml),
pdf: handlePromise(pdf),
content: handlePromise(content),
categories: handlePromise(categories),
related: handlePromise(related),
links: handlePromise(links),
references: handlePromise(references),
coordinates: handlePromise(coordinates),
langLinks: handlePromise(langLinks),
tables: handlePromise(tables),
citation: handlePromise(citation),
},
};
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaSearch(query: string) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const psearchResults = wiki.search(query);
const pcitation = wiki.citation(query);
const psuggestions = wiki.suggest(query);
const pautoComplete = wiki.autocompletions(query);
const [searchResults, citation, suggestions, autoComplete] =
await Promise.all([
psearchResults,
pcitation,
psuggestions,
pautoComplete,
]);
return { ok: { searchResults, citation, suggestions, autoComplete } };
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaGeoSearch({
latitude,
longitude,
options,
}: {
latitude: number;
longitude: number;
options?: geoOptions;
}) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const searchResults = await wiki.geoSearch(latitude, longitude, options);
return { ok: searchResults };
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaFeatured(opts: fcOptions) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const featured = await wiki.featuredContent(opts);
return { ok: featured };
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaDate(opts: eventOptions) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const date = await wiki.onThisDay(opts);
return { ok: fixToday(date) };
} catch (e) {
return { error: `${e}` };
}
}
export async function fetchWikipediaRandom(opts?: randomFormats) {
// LANG: 2 letters! or en-gb zh-hans etc.
try {
// const languages = await wiki.languages();
const random = await wiki.random(opts);
return { ok: random };
} catch (e) {
return { error: `${e}` };
}
}
|