-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·199 lines (183 loc) · 4.87 KB
/
index.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
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
#!/usr/bin/env node
'use strict'
const pinyinOrHanzi = require('pinyin-or-hanzi')
const hanziToZhuyin = require('hanzi-to-zhuyin')
const convertPinyin = require('pinyin-convert')
const splitPinyin = require('pinyin-split')
const express = require('express')
const zhuyin = require('zhuyin')
const corser = require('corser')
const mdbg = require('mdbg')
const http = require('http')
const ip = require('ip')
const app = express()
app.use(corser.create())
app.get('/', (req, res) => {
res.send('<a href="https://github.com/pepebecker/pinyin-rest">View GitHub Repository</a>')
})
const getByHanzi = async (text, everything) => {
const list = []
let index = 0
while (index < text.length) {
let count = text.length - index
let wordFound = false
while (count >= 0) {
const word = text.substr(index, count)
try {
const entry = await mdbg.getByHanzi(word)
wordFound = true
index += count - 1
list.push(entry)
break
} catch (err) {
if (err.type !== 'NotFoundError') console.error(err)
}
count--
}
if (!wordFound && everything) {
if (index === 0 || typeof list[list.length - 1] === 'object') {
list.push(text[index])
} else if (typeof list[list.length - 1] === 'string') {
list[list.length - 1] += text[index]
}
}
index++
}
return list
}
app.get('/hanzi/:query', async (req, res) => {
const text = req.params.query
const type = req.query.getBy || pinyinOrHanzi(text)
let list = []
if (type === 'mandarin' || type === 'hanzi') {
list = await getByHanzi(text)
}
if (type.substr(0, 6) === 'pinyin' || type === 'zhuyin') {
const pinyinList = (type === 'zhuyin' ? zhuyin.toPinyin(text, { numbered: true }) : splitPinyin(text))
let index = 0
while (index < pinyinList.length) {
let count = pinyinList.length - index
while (count >= 0) {
const word = pinyinList.slice(index, index + count).join(' ')
try {
let entry = null
if (req.query.getIndex === 'true') entry = await mdbg.getIndexByPinyin(word)
else entry = await mdbg.getByPinyin(word)
index += count - 1
list.push(entry)
break
} catch (err) {
if (err.type !== 'NotFoundError') console.error(err)
}
count--
}
index++
}
}
res.send(list)
})
app.get('/definition/:query', (req, res) => {
mdbg.get(req.params.query)
.then(data => {
const convert = char => {
return Object.keys(char.definitions).reduce((o, pinyin) => {
o[pinyin] = char.definitions[pinyin].translations
return o
}, {})
}
if (data) {
if (Array.isArray(data)) res.send(data.map(convert))
else res.send(convert(data))
} else {
res.sendStatus(200)
}
})
.catch(err => res.send({ error: err.message }))
})
const convertToPinyin = async query => {
const data = await convertPinyin(query, { everything: true, segmented: true })
const body = {
text: typeof data === 'string' ? data : data.map(part => {
if (typeof part === 'string') {
return part
} else {
return part[0]
}
}).join('')
}
if (typeof data !== 'string') body.data = data
return body
}
app.get('/pinyin/:query', async (req, res) => {
if (req.query.split) {
const list = splitPinyin(req.params.query, req.query.everything, req.query.wrapInList)
res.send({
text: list.join(' '),
data: list
})
return
}
const type = pinyinOrHanzi(req.params.query)
if (type === 'zhuyin') {
const list = zhuyin.toPinyin(req.params.query, { everything: true })
res.send({
text: list.join(''),
data: list
})
}
if (type.substr(0, 6) === 'pinyin' || type === 'mandarin') {
convertToPinyin(req.params.query, { everything: true, segmented: true })
.then(data => res.send(data))
.catch(err => res.send({ error: err.message }))
}
})
app.get('/zhuyin/:query', async (req, res) => {
const text = req.params.query
const type = pinyinOrHanzi(text)
if (type === 'zhuyin') {
if (req.query.split) {
const list = zhuyin.split(text, true)
res.send({
text: list.join(' '),
data: list
})
} else {
res.send({ text: text })
}
}
if (type.substr(0, 6) === 'pinyin') {
const list = zhuyin.fromPinyin(text, true)
res.send({
text: list.join(''),
data: list
})
}
if (type === 'mandarin') {
const list = await hanziToZhuyin(text, { everything: true, segmented: true })
res.send({
text: list.map(item => typeof item === 'string' ? item : item[0]).join(''),
data: list
})
}
})
app.get('/hsk/:query', async (req, res) => {
try {
if (req.query.getIndex === 'true') {
res.send(await mdbg.getIndexByHSK(req.params.query))
} else {
res.send(await mdbg.getByHSK(req.params.query))
}
} catch (err) {
res.send({ error: err })
}
})
const port = Number(process.env.PORT || 8080)
http.createServer(app).listen(port, ip.address(), async err => {
if (err) {
console.error(err)
process.exit(1)
} else {
console.log(`Server running on http://${ip.address()}:${port}`)
convertPinyin.init(await hanziToZhuyin.init(await mdbg.init()))
}
})