-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
335 lines (289 loc) · 11 KB
/
app.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const MiniSearch = require('minisearch')
const constant = require('./constant')
require('dotenv').config()
const { initializeApp, } = require("firebase/app")
const { getDatabase, ref, get, child, } = require('firebase/database')
const { getStorage, ref: sRef, getDownloadURL } = require('firebase/storage')
const express = require('express')
const regex = require('./regex')
const app = express()
const fs = require('fs');
const fetch = require('node-fetch')
const textToSpeech = require('@google-cloud/text-to-speech');
const util = require('util');
const port = process.env.PORT || 3000;
const projectId = process.env.PROJECT_ID;
const client_email = process.env.CLIENT_EMAIL;
const private_key = process.env.PRIVATE_KEY.replace(/\\n/gm, '\n')
const pathToenvFile = 'config/dev.env'
let speechKey = process.env.SPEECH
const getEnv = (key) => {
fs.readFile(pathToenvFile, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
let result = parse(data);
// console.log(result[key]);
if (speechKey === undefined) {
speechKey = result[key]
}
});
}
// Creates a client
const client = new textToSpeech.TextToSpeechClient({
projectId, credentials: {
client_email, private_key
}
});
const addDataToJSON = (docs, abbr) => {
const documentsContent = docs.map(item => {
item["abbr"] = abbr
item["index"] = `${abbr} ${item["index"]}`
return item
})
return documentsContent
}
// SPEECH
app.get('/speech/:query', async (req, res) => {
console.log(`Speech to text ${req.params.query}`)
// Construct the request
// const request = {
// input: { text: req.params.query },
// voice: { languageCode: 'en-US', ssmlGender: 'FEMALE' },
// audioConfig: { audioEncoding: 'MP3' },
// client_email
// };
const request = {
"audioConfig": {
"audioEncoding": "LINEAR16",
"effectsProfileId": [
"handset-class-device"
],
"pitch": -0.8,
"speakingRate": 1
},
"input": {
"text": req.params.query,
},
"voice": {
"languageCode": "en-US",
"name": "en-US-Neural2-D"
}
}
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
// const writeFile = util.promisify(fs.writeFile);
// await writeFile('output.mp3', response.audioContent, 'base64');
res.status(200).json({
status: 'success',
type: "Text to Speech",
requestedAt: req.requestTime,
data: { results: Buffer.from(response.audioContent).toString("base64") }
})
})
// REGULAR SEARCH
app.get('/regular/:query', (req, res) => {
console.log(`Searching MiniSearch Index for ${req.params.query}`)
const results = miniSearchIndex.search(req.params.query, { boost: { text: 10 }, combineWith: 'OR', })
console.log(results.length)
res.status(200).json({
status: 'success',
type: "Regular Search",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
// Limit search to specific book and find exact phrase within that book
// Syntax Exmaple ---> (12Tr) "Scarlet Colored Beast"
app.get('/bookphrase/:query', (req, res) => {
const queryExtracted = req.params.query.split(")")[1].replace(/"/g, "").trim()
const abbrExtracted = req.params.query.match(regex.extractAbbr).join("").replace("(", "")
console.log(`Searching MiniSearch Index for this exact phrase ---> ${queryExtracted} in this exact book ---> ${abbrExtracted}`)
const results = miniSearchIndex.search(queryExtracted, {
fields: ['text'], combineWith: 'AND', filter: (result) => {
console.log(result.abbr)
if (result.abbr === abbrExtracted && result.text.match(new RegExp(`${queryExtracted}`, "i"))) {
return result
}
}
})
res.status(200).json({
status: 'success',
type: "Search in Exact Book for Exact Phrase",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
// Limit search to exact phrase
// Syntax Exmaple ---> "Scarlet Colored Beast"
app.get('/phrase/:query', (req, res) => {
const queryExtracted = req.params.query.match(regex.exactPhraseRegex).join("").replace(/\"/g, "")
console.log(`Searching MiniSearch Index for this exact phrase ---> ${queryExtracted}`)
const results = miniSearchIndex.search(queryExtracted, {
fields: ['text'], combineWith: 'AND', filter: (result) => {
if (result.text.match(new RegExp(`${queryExtracted}`, "i"))) {
return result
}
}
})
res.status(200).json({
status: 'success',
type: "Search by Exact Phrase",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
// Limit search to specific YEAR RANGE
// Syntax Exmaple ---> (1930-1935) Scarlet Colored Beast
app.get('/yearrange/:query', (req, res) => {
const queryExtracted = req.params.query.split(")")[1].replace(/"/g, "").trim()
const startYear = JSON.parse(req.params.query.match(regex.extractYearRangeRegex)[0])
const endYear = JSON.parse(req.params.query.match(regex.extractYearRangeRegex)[1])
console.log(`Searching MiniSearch Index for ---> ${queryExtracted} in year range ${startYear} - ${endYear}`)
const results = miniSearchIndex.search(queryExtracted, {
fields: ['text'], combineWith: 'OR', filter: (result) => {
if (result.year >= startYear && result.year <= endYear) {
return result
}
}
})
res.status(200).json({
status: 'success',
type: "Search by Range",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
// Limit search to specific year
// Syntax Exmaple ---> (1945) Scarlet Colored Beast
app.get('/year/:query', (req, res) => {
const yearSpecified = JSON.parse(req.params.query.match(regex.extractYearRegex).join(""))
const queryExtracted = req.params.query.split(")")[1].trim()
console.log(`Searching MiniSearch Index for ---> ${queryExtracted} within the year ${yearSpecified}`)
const results = miniSearchIndex.search(queryExtracted, {
fields: ['text'], boost: { text: 10 }, combineWith: 'OR', filter: (result) => {
return result.year === yearSpecified
}
})
res.status(200).json({
status: 'success',
type: "Search by Year",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
// Limit search to specific book
app.get('/book/:query', (req, res) => {
const bookSpecified = req.params.query.match(regex.extractAbbr).join("").replace("(", "")
const queryExtracted = req.params.query.split(")")[1].trim()
console.log(`Searching MiniSearch Index for ---> ${queryExtracted} in ${bookSpecified}`)
const results = miniSearchIndex.search(queryExtracted, {
fields: ['text'], combineWith: 'OR',
filter: (result) => result.abbr === bookSpecified
})
res.status(200).json({
status: 'success',
type: "Search by Book",
length: results.length,
requestedAt: req.requestTime,
data: { results }
})
})
app.get('/', (req, res) => {
console.log('Hello from the Server!')
res.status(200).json({
status: 'success',
requestedAt: req.requestTime,
data: {}
})
})
const firebaseConfig = {
apiKey: "AIzaSyD2IKozKgSEd4jm5Ka7c5EZneipoh-_nkA",
authDomain: "vthwritings.firebaseapp.com",
databaseURL: "https://vthwritings.firebaseio.com",
projectId: "vthwritings",
storageBucket: "vthwritings.appspot.com",
messagingSenderId: "140188975056",
appId: "1:140188975056:web:25e50a753a8192d17203d5",
};
const appInit = initializeApp(firebaseConfig)
const firebase = ref(getDatabase(appInit))
let miniSearchIndex = new MiniSearch({
fields: ['text', 'subHeading', 'title'], // fields to index for full-text search
storeFields: ['page', 'text', 'year', 'abbr', 'subHeading', 'title'], // fields to return with search results
processTerm: (term, _fieldName) => constant.stopWords.has(term) ? null : term.toLowerCase(),
idField: 'index'
})
const firebaseFetch = async (path) => {
console.log(path)
const dataSnapShot = await get(child(firebase, path))
return dataSnapShot.val()
}
const addBookAsync = (documents) => {
miniSearchIndex.addAll(documents)
}
const firebaseDocumentURLS = ['literatureDocuments/Tracts', 'literatureDocuments/Old Codes', 'literatureDocuments/Sermon Codes', 'literatureDocuments/Jezreel Letters', 'literatureDocuments/Answerers', 'literatureDocuments/1TG', 'literatureDocuments/2TG', 'literatureDocuments/Miscellaneous', 'literatureDocuments/1SR/1SR', 'literatureDocuments/2SR/2SR']
const getAllBooks = async () => {
const url = firebaseDocumentURLS.splice(0, 1)
const res = await firebaseFetch(`english/${url}`)
if (Array.isArray(res)) {
const abbr = res[0].page.split(" ")[0]
// addDataToJSON(res, abbr)
// addBookAsync(res)
} else {
const catOfBooksFlattened = Object.keys(res).reduce((aggr, abbr) => {
addDataToJSON(res[abbr], abbr)
aggr.push(...res[abbr])
return aggr
}, [])
// addBookAsync(catOfBooksFlattened)
}
if (firebaseDocumentURLS.length) {
// console.log(res)
// getAllBooks()
} else {
console.log("All Books Processed!")
// const file = 'searchIndex.txt';
// writeFile(file, JSON.stringify(miniSearchIndex), (e) => console.log("Index is written out to file", e));
app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
}
}
const loadJSON = (index) => {
return MiniSearch.loadJS(index, {
fields: ['text', 'subHeading', 'title'],
storeFields: ['page', 'text', 'year', 'abbr', 'subHeading', 'title'],
processTerm: (term, _fieldName) => constant.stopWords.has(term) ? null : term.toLowerCase(),
idField: "index"
})
}
const init = async () => {
// if (existsSync("searchIndex.txt")) {
// console.log("Search Index exists!")
// readFile("searchIndex.txt", {}, (e, data) => {
// miniSearchIndex = loadJSON(data)
// },)
if (miniSearchIndex._documentCount === 0) {
// return
// const indexRef = sRef(vthStorage, "searchIndex/searchIndex.txt")
// const uri = { uri: await getDownloadURL(indexRef) }
// console.log(uri)
const dataRaw = await fetch(`https://drive.google.com/uc?export=download&id=1InALaFCKHt0ZzQI8eFEXDnSjF7hYgccT`)
const data = await dataRaw.json()
miniSearchIndex = loadJSON(data)
app.listen(port, () => {
console.log(`App running on port ${port}...`);
});
} else {
console.log("Search Index Already Loaded")
}
// }
}
init()