Skip to content

Commit

Permalink
feat(lyrics-plus): Normalize album name before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Aug 11, 2023
1 parent 01644b6 commit 834d118
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
8 changes: 6 additions & 2 deletions CustomApps/lyrics-plus/ProviderNetease.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ const ProviderNetease = (function () {
const expectedDuration = info.duration;
const actualDuration = song.dt;

const expectedAlbumName = Utils.normalize(info.album);
const actualAlbumName = Utils.normalize(song.al.name);
// normalized expected album name
const neAlbumName = Utils.normalize(info.album);
const expectedAlbumName = Utils.containsHanCharacter(neAlbumName)
? await Utils.toSimplifiedChinese(neAlbumName)
: neAlbumName;
const actualAlbumName = Utils.normalize(song.al.name); // usually in Simplified Chinese

if (
actualAlbumName == expectedAlbumName ||
Expand Down
37 changes: 37 additions & 0 deletions CustomApps/lyrics-plus/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ class Utils {
return result.replace(/\s+/g, " ").trim();
}

/**
* Check if the specified string contains Han character.
*
* @param {string} s
*/
static containsHanCharacter(s) {
const hanRegex = /\p{Script=Han}/u;
return hanRegex.test(s);
}

/**
* Singleton Translator instance for {@link toSimplifiedChinese}.
*
* @type {Translator}
*/
static #translator = null;

/**
* Convert all Han characters to Simplified Chinese.
*
* Choosing Simplified Chinese makes the converted result more accurate,
* as the conversion from SC to TC may have multiple possibilities,
* while the conversion from TC to SC usually has only one possibility.
*
* @param {string} s
* @returns {Promise<string>}
*/
static async toSimplifiedChinese(s) {
// create a singleton Translator instance
if (!Utils.#translator) {
Utils.#translator = new Translator("zh");
}

// translate it to Simplified Chinese
return Utils.#translator.convertChinese(s, "tw", "cn");
}

static removeSongFeat(s) {
return (
s
Expand Down

0 comments on commit 834d118

Please sign in to comment.