This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (54 loc) · 2.26 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
//Romanization System 1.0 by AndroidWG (Samuel Rodrigues)
//Made for SmartLyrics app
var express = require("express");
var port = process.env.PORT;
var app = express(); app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
var bodyParser = require("body-parser");
app.use(bodyParser.text());
const Kuroshiro = require('kuroshiro');
const KuroshiroAnalyzer = require('kuroshiro-analyzer-kuromoji');
//the only part that matters
//Parameters:
//[text]-text to convert
//[to]-convert to one of these: hiragana, katakana, romaji
//[mode]-using one of these modes: normal, spaced, okurigana, furigana
//[romajiSystem]-using one of these systems: nippon, passport, hepburn
app.post("/convert", async (request, response, next) => {
try {
console.log(`Recived convert request from ${request.ip}`);
const kuroshiro = new Kuroshiro();
await kuroshiro.init(new KuroshiroAnalyzer({ dictPath: 'node_modules/kuromoji/dict' }));
console.log(`Parameters: ${request.params}`);
const to = request.query.to;
const mode = request.query.mode;
//will use hepburn if param doesn't exist
const romajiSystem = (request.query.romajiSystem == null) ? "hepburn" : request.query.romajiSystem;
//change line separator
const lineSeparator = (request.query.useHTML == "true") ? "<br>" : "\n";
const text = request.body;
console.log("Parsed request body");
console.log(`Original text: ${text}`);
//split to convert line by line
const lines = text.split(lineSeparator)
const convertedlines = await Promise.all(lines
.map(async line => {
if (Kuroshiro.Util.hasJapanese(line)){
return await kuroshiro.convert(line, { to: to, mode: mode, romajiSystem: romajiSystem});
} else {
return line;
}
}))
const converted = convertedlines.join(lineSeparator);
console.log(`Comverted output: ${converted}`);
response.send(converted);
}
catch (e) {
next(e);
}
});
//simple page to show if the main URL is accessed
app.get('/', function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});