-
-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lyrics-plus): add japanese lyrics conversion tool (#1990)
- Loading branch information
Showing
8 changed files
with
340 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const kuroshiroPath = "https://cdn.jsdelivr.net/npm/[email protected]/dist/kuroshiro.min.js"; | ||
const kuromojiPath = "https://cdn.jsdelivr.net/npm/[email protected]/dist/kuroshiro-analyzer-kuromoji.min.js"; | ||
|
||
const dictPath = "https:/cdn.jsdelivr.net/npm/[email protected]/dict"; | ||
|
||
class Translator { | ||
constructor() { | ||
this.includeExternal(kuroshiroPath); | ||
this.includeExternal(kuromojiPath); | ||
|
||
this.createKuroshiro(); | ||
|
||
this.finished = false; | ||
} | ||
|
||
includeExternal(url) { | ||
var s = document.createElement("script"); | ||
s.setAttribute("type", "text/javascript"); | ||
s.setAttribute("src", url); | ||
var nodes = document.getElementsByTagName("*"); | ||
var node = nodes[nodes.length - 1].parentNode; | ||
node.appendChild(s); | ||
} | ||
|
||
/** | ||
* Fix an issue with kuromoji when loading dict from external urls | ||
* Adapted from: https://github.com/mobilusoss/textlint-browser-runner/pull/7 | ||
*/ | ||
applyKuromojiFix() { | ||
if (typeof XMLHttpRequest.prototype.realOpen !== "undefined") return; | ||
XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open; | ||
XMLHttpRequest.prototype.open = function (method, url, bool) { | ||
if (url.indexOf(dictPath.replace("https://", "https:/")) === 0) { | ||
this.realOpen(method, url.replace("https:/", "https://"), bool); | ||
} else { | ||
this.realOpen(method, url, bool); | ||
} | ||
}; | ||
} | ||
|
||
async createKuroshiro() { | ||
if (typeof Kuroshiro === "undefined" || typeof KuromojiAnalyzer === "undefined") { | ||
//Waiting for JSDeliver to load Kuroshiro and Kuromoji | ||
setTimeout(this.createKuroshiro.bind(this), 50); | ||
return; | ||
} | ||
|
||
this.kuroshiro = new Kuroshiro.default(); | ||
|
||
this.applyKuromojiFix(); | ||
|
||
this.kuroshiro.init(new KuromojiAnalyzer({ dictPath: dictPath })).then( | ||
function () { | ||
this.finished = true; | ||
}.bind(this) | ||
); | ||
} | ||
|
||
async romajifyText(text, target = "romaji", mode = "spaced") { | ||
if (!this.finished) { | ||
setTimeout(this.romajifyText.bind(this), 100, text, target, mode); | ||
return; | ||
} | ||
|
||
return this.kuroshiro.convert(text, { | ||
to: target, | ||
mode: mode | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.