From 239c3147645bbf528756f1914982151bb118aaaa Mon Sep 17 00:00:00 2001 From: EJClaw Date: Wed, 4 Nov 2026 15:52:43 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9B=90=EA=B3=A1=EC=9D=B4=20=EC=95=84?= =?UTF-8?q?=EB=8B=8C=20=EA=B3=A1=20=EC=A0=95=EB=A6=AC=201=EB=8B=A8?= =?UTF-8?q?=EA=B3=84:=20=EC=A0=9C=EB=AA=A9=20=EC=A0=95=EB=A6=AC=20?= =?UTF-8?q?=ED=95=A8=EC=88=98(cleanTitle=20=E2=80=94=20=EA=B4=84=ED=98=B8?= =?UTF-8?q?=C2=B7feat.=C2=B7=EB=B2=84=EC=A0=84=20=ED=91=9C=EC=8B=9C?= =?UTF-8?q?=EB=A5=BC=20=EC=A7=80=EC=9A=B0=EA=B3=A0=20=EA=B4=84=ED=98=B8=20?= =?UTF-8?q?=EC=95=88=20=EB=8B=A4=EB=A5=B8=20=EC=9D=B4=EB=A6=84=EC=9D=80=20?= =?UTF-8?q?=EC=A0=95=EB=8B=B5=20=EB=B3=84=EC=B9=AD=EC=9C=BC=EB=A1=9C,=20?= =?UTF-8?q?=EC=95=9E=20=EA=B4=84=ED=98=B8=C2=B7Part=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=EB=8A=94=20=EB=8B=A8=EC=96=B4=EB=A1=9C=20=EB=82=A8=EA=B9=80,?= =?UTF-8?q?=20=EC=9D=BC=EB=B3=B8=EC=96=B4=20=EC=A0=9C=EB=AA=A9+=ED=95=9C?= =?UTF-8?q?=EA=B8=80=20=EA=B4=84=ED=98=B8=EB=8A=94=20=ED=95=9C=EA=B8=80=20?= =?UTF-8?q?=EC=A0=9C=EB=AA=A9)=EC=99=80=20=EC=9B=90=EA=B3=A1=20=EC=95=84?= =?UTF-8?q?=EB=8B=98=20=ED=8C=90=EB=B3=84(versionKind:=20remix/live/inst/a?= =?UTF-8?q?coustic/sped,=20videoVersionKind:=20=EC=98=81=EC=83=81=EB=A7=8C?= =?UTF-8?q?=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=C2=B7=EB=B0=98=EC=A3=BC).=20drop?= =?UTF-8?q?-versions=20CLI(--dry-run,=20--keep):=20=EB=A6=AC=EB=AF=B9?= =?UTF-8?q?=EC=8A=A4=C2=B7=EB=9D=BC=EC=9D=B4=EB=B8=8C=C2=B7Inst.=20?= =?UTF-8?q?=EA=B3=A1=EA=B3=BC=20=EC=98=81=EC=83=81=EC=9D=B4=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=EB=B8=8C=C2=B7=EB=B0=98=EC=A3=BC=EC=9D=B8=20=EC=9E=AC?= =?UTF-8?q?=EC=83=9D=20=EA=B3=A1=EC=9D=84=20=ED=9C=B4=EC=A7=80=ED=86=B5?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C.=20=EC=B0=A8=ED=8A=B8=EB=8A=94=20=EB=A6=AC?= =?UTF-8?q?=EB=AF=B9=EC=8A=A4=C2=B7=EB=9D=BC=EC=9D=B4=EB=B8=8C=C2=B7Inst.?= =?UTF-8?q?=EB=A5=BC=20=EC=83=88=20=EA=B3=A1=EC=9C=BC=EB=A1=9C=20=EB=84=A3?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EC=9D=8C.=20=EC=9B=90=EA=B3=A1=20?= =?UTF-8?q?=EC=B0=BE=EA=B8=B0:=20=EC=98=81=EC=83=81=20=EC=A0=9C=EB=AA=A9?= =?UTF-8?q?=EC=9D=B4=20'-=20Bossa=20Unplugged'=EC=B2=98=EB=9F=BC=20?= =?UTF-8?q?=EB=8B=A4=EB=A5=B8=20=EB=B2=84=EC=A0=84=EC=9D=B4=EB=A9=B4=20?= =?UTF-8?q?=EC=97=B0=EA=B2=B0=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9D=8C(?= =?UTF-8?q?=EB=86=93=EC=B9=98=EB=8D=98=20=EA=B2=83).=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/collector/charts.ts | 5 +- apps/server/src/collector/cli.ts | 13 +- apps/server/src/collector/match-youtube.ts | 4 +- apps/server/src/collector/text.test.ts | 69 +++++++- apps/server/src/collector/text.ts | 31 +++- packages/shared/src/index.ts | 1 + packages/shared/src/lite.ts | 1 + packages/shared/src/title-clean.test.ts | 76 ++++++++ packages/shared/src/title-clean.ts | 192 +++++++++++++++++++++ 9 files changed, 384 insertions(+), 8 deletions(-) create mode 100644 packages/shared/src/title-clean.test.ts create mode 100644 packages/shared/src/title-clean.ts diff --git a/apps/server/src/collector/charts.ts b/apps/server/src/collector/charts.ts index dcffb48..e9cae1a 100644 --- a/apps/server/src/collector/charts.ts +++ b/apps/server/src/collector/charts.ts @@ -4,7 +4,7 @@ * the others only attach to songs we already have. 2.5s between requests, a source stops at the first block signal. */ import type { Database } from 'bun:sqlite'; -import { songMatchKey } from '@mq/shared'; +import { songMatchKey, versionKind } from '@mq/shared'; import { Catalog, Job, sleep } from './catalog'; import { decodeEntities } from './text'; @@ -175,7 +175,8 @@ export function saveChart(db: Database, spec: Pick { for (const row of rows) { let songId = findSong(db, spec.source, row); - if (!songId && spec.create) { + // a remix / live / inst. on the chart is not a new song for the quiz (docs/13): left unlinked + if (!songId && spec.create && !versionKind(row.title)) { const [id, isNew] = cat.upsert({ melonId: spec.source === 'melon' ? Number(row.ref) : null, title: row.title, diff --git a/apps/server/src/collector/cli.ts b/apps/server/src/collector/cli.ts index 05af690..9ce599f 100644 --- a/apps/server/src/collector/cli.ts +++ b/apps/server/src/collector/cli.ts @@ -12,6 +12,7 @@ * bun apps/server/src/collector/cli.ts age-audio [--limit N] [--delay ms] (연령제한 곡: download their audio with the adult-verified cookie, docs/08 §2-1) * bun apps/server/src/collector/cli.ts levels * bun apps/server/src/collector/cli.ts fix-text (one-off: decode `'`/`&` saved in titles, let 원곡 찾기 retry them) + * bun apps/server/src/collector/cli.ts drop-versions [--dry-run] [--keep id,id] (one-off: remix/live/inst. songs and songs linked to such a video → 휴지통) * bun apps/server/src/collector/cli.ts stats * In the container: docker exec music_quiz-app-1 bun apps/server/src/collector/cli.ts … */ @@ -29,7 +30,7 @@ import { collectLoudness } from './loudness'; import { computeLevels } from './levels'; import { collectAgeAudio } from './age-audio'; import { audioDir } from '../songs/audio'; -import { fixEntities } from './text'; +import { dropVersions, fixEntities } from './text'; const [cmd, ...rest] = process.argv.slice(2); const flag = (name: string) => { @@ -96,11 +97,19 @@ switch (cmd) { case 'fix-text': console.log(JSON.stringify(fixEntities(db), null, 1)); break; + case 'drop-versions': { + const i = rest.indexOf('--keep'); + const keep = i >= 0 ? rest[i + 1]!.split(',').map(Number).filter(Number.isFinite) : []; + const r = dropVersions(db, { dryRun: rest.includes('--dry-run'), keep }); + for (const s of r.songs) console.log(`${s.id}\t${s.why}:${s.kind}\t${s.artist} - ${s.title}${s.why === 'video' ? `\t(영상: ${s.video})` : ''}`); + console.log(`${rest.includes('--dry-run') ? 'dry run: would trash' : 'trashed'} ${rest.includes('--dry-run') ? r.songs.length : r.trashed}`); + break; + } case 'stats': console.log(JSON.stringify(catalogStats(db), null, 1)); break; default: - console.log('usage: cli.ts import-norabose|import-sta|match-youtube|reconfirm-youtube|research-youtube|charts|melon-detail|heatmap|loudness|age-audio|levels|fix-text|stats'); + console.log('usage: cli.ts import-norabose|import-sta|match-youtube|reconfirm-youtube|research-youtube|charts|melon-detail|heatmap|loudness|age-audio|levels|fix-text|drop-versions|stats'); process.exitCode = 1; } db.close(); diff --git a/apps/server/src/collector/match-youtube.ts b/apps/server/src/collector/match-youtube.ts index 7a4e1e8..d9ba268 100644 --- a/apps/server/src/collector/match-youtube.ts +++ b/apps/server/src/collector/match-youtube.ts @@ -5,6 +5,7 @@ * Paced 5s apart (2s ran into a 429 and then an ended session on 11/03); the cron caps a day at 300 songs. */ import type { Database } from 'bun:sqlite'; +import { videoVersionKind } from '@mq/shared'; import { Job, sleep } from './catalog'; import { checkEmbed } from './oembed'; import { addsJunk, addsVideo, confirmedDespiteLength, isConfident, namesOtherCut, scoreResult, type Scored } from './score'; @@ -49,9 +50,10 @@ function setAutoVideo(db: Database, id: number, status: 'matched' | 'candidate', /** * The actual video (its YouTube title, not the search row) turns out to be a music video, inst., live … — whatever the * length — or, for a result confirmed despite a length difference, another mix/edit/year: left as 후보. + * videoVersionKind also catches "- Bossa Unplugged", "(Live At …)" written as the video's own version (docs/13). */ function isOtherCut(c: Scored, videoTitle: string, songTitle: string): boolean { - if (addsVideo(videoTitle, songTitle) || addsJunk(videoTitle, songTitle)) return true; + if (addsVideo(videoTitle, songTitle) || addsJunk(videoTitle, songTitle) || videoVersionKind(videoTitle, songTitle)) return true; return confirmedDespiteLength(c) && namesOtherCut(videoTitle, songTitle); } diff --git a/apps/server/src/collector/text.test.ts b/apps/server/src/collector/text.test.ts index 9644a92..d5f99f7 100644 --- a/apps/server/src/collector/text.test.ts +++ b/apps/server/src/collector/text.test.ts @@ -6,7 +6,8 @@ import { openDb } from '../db'; import { tempDbPath } from '../test-utils'; import { Catalog } from './catalog'; import { parseMelon, saveChart } from './charts'; -import { decodeEntities, fixEntities } from './text'; +import { decodeEntities, dropVersions, fixEntities } from './text'; +import { matchYoutube } from './match-youtube'; test('entities: hex, decimal and named, one pass, unknown ones kept', () => { expect(decodeEntities('Somebody's Watching Me')).toBe("Somebody's Watching Me"); @@ -66,3 +67,69 @@ test('fix-text repairs saved songs, chart rows and artists, retries 원곡 찾 ]); expect(fixEntities(db)).toMatchObject({ songs: 0, retry: 0, artists: 0, chartRows: 0 }); }); + +test('drop-versions: remix/live/inst. songs and songs linked to such a video go to the trash, kept ids and originals stay', () => { + const db = openDb(tempDbPath()); + const add = (title: string, artist: string, status = 'matched', video: string | null = title) => + Number( + db + .query('INSERT INTO songs (title, artist, match_key, youtube_status, youtube_title, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 0, 0)') + .run(title, artist, songMatchKey(title, artist), status, video).lastInsertRowid, + ); + const remix = add('Macarena (Bayside Boys Remix)', 'Los Del Rio'); + const siren = add('사이렌 Remix', '호미들', 'matched', 'Siren Remix (Feat. UNEDUCATED KID)'); + const live = add('Signs (Live At The Trocadero / 1990)', 'Tesla'); + const inst = add('아침(Inst.)', '김민기', 'none', null); + const liveVideo = add('바다 이야기', '윤종신', 'matched', 'Over See (Live) (바다 이야기)'); + const mrVideo = add('달콤한 빈말', '백아연', 'matched', '달콤한 빈말 (Feat. 바버렛츠) (MR)'); + const opus = add('Live Is Life (Digitally Remastered) (Single Ver.)', 'Opus', 'matched', 'Live Is Life (Digitally Remastered) [Live] (Single Version)'); + const candidate = add('그대 내품에', '박정운', 'candidate', '그대 내품에 (Live) (그대 내품에)'); + const originals = ['Live Wire', 'How Do I Live', '미스터 (Mr.)', 'Radio Ga Ga (Remastered 2011)'].map((t) => add(t, '가수')); + const karaoke = add('노래방에서', '장범준', 'matched', 'karaoke (노래방에서)'); + + const dry = dropVersions(db, { dryRun: true, keep: [siren, opus] }); + expect(dry.trashed).toBe(0); + expect(dry.songs.map((s) => [s.id, s.why, s.kind])).toEqual([ + [remix, 'title', 'remix'], + [live, 'title', 'live'], + [inst, 'title', 'inst'], + [liveVideo, 'video', 'live'], + [mrVideo, 'video', 'inst'], + ]); + expect(db.query<{ n: number }, []>('SELECT COUNT(*) n FROM songs WHERE deleted_at IS NOT NULL').get()!.n).toBe(0); + expect(dropVersions(db, { keep: [siren, opus] }).trashed).toBe(5); + const alive = db.query<{ id: number }, []>('SELECT id FROM songs WHERE deleted_at IS NULL ORDER BY id').all().map((r) => r.id); + expect(alive).toEqual([siren, opus, candidate, ...originals, karaoke]); +}); + +test('charts never add a remix / live / inst. as a new song', () => { + const db = openDb(tempDbPath()); + const r = saveChart(db, { source: 'melon', chart: 'realtime', period: '2026-11-04', create: 'kpop' }, [ + { rank: 1, ref: '101', title: '거북선 Remix', artists: [{ name: '팔로알토' }] }, + { rank: 2, ref: '102', title: 'Fields Of Gold (Live At Villa Il Palagio, Italy/2001)', artists: [{ name: 'Sting' }] }, + { rank: 3, ref: '103', title: 'Live Wire', artists: [{ name: '서태지' }] }, + ]); + expect(r).toEqual({ created: 1, linked: 1, rows: 3 }); + expect(db.query<{ title: string }, []>('SELECT title FROM songs').all().map((x) => x.title)).toEqual(['Live Wire']); +}); + +test('원곡 찾기 never links a video whose own title says Live / MR / Unplugged (2026-11-04 prod cases)', async () => { + const db = openDb(tempDbPath()); + const c = new Catalog(db); + const songs = [ + ['바다 이야기', '윤종신', 'Over See (Live) (바다 이야기)'], + ['달콤한 빈말', '백아연', '달콤한 빈말 (Feat. 바버렛츠) (MR)'], + ['Only When I Sleep', '이은주', 'Only When I Sleep - Bossa Unplugged'], + ] as const; + const titles: Record = {}; + const results: Record = {}; + songs.forEach(([title, artist, video], i) => { + c.upsert({ title, artists: [{ name: artist }], durationSec: 240, source: 't', ref: String(i) }); + const id = `vid${i}`.padEnd(11, '0'); + titles[id] = video; + results[`${artist} ${title}`] = [{ videoId: id, title, artists: artist, album: null, durationSec: 240 }]; + }); + const stats = await matchYoutube(db, { searchSongs: async (q) => results[q] ?? [] }, { delayMs: 0 }, async (id) => ({ state: 'ok', author: 'x - Topic', title: titles[id]! })); + expect(stats.matched).toBe(0); + expect(db.query<{ n: number }, []>("SELECT COUNT(*) n FROM songs WHERE youtube_status = 'matched'").get()!.n).toBe(0); +}); diff --git a/apps/server/src/collector/text.ts b/apps/server/src/collector/text.ts index 9755e73..ef440eb 100644 --- a/apps/server/src/collector/text.ts +++ b/apps/server/src/collector/text.ts @@ -1,7 +1,7 @@ /** HTML entities in collected song text (`Somebody's`, `Salty & Sweet`): decoding and the one-off DB repair. */ import type { Database } from 'bun:sqlite'; -import { songMatchKey } from '@mq/shared'; -import { mergeSongs } from '../songs/admin-ops'; +import { songMatchKey, versionKind, videoVersionKind, type VersionKind } from '@mq/shared'; +import { mergeSongs, trashSongs } from '../songs/admin-ops'; const NAMED: Record = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' }; @@ -67,3 +67,30 @@ export function fixEntities(db: Database, now = Date.now()) { return out; })(); } + +/** + * 원곡이 아닌 곡 지우기 (2026-11-04, docs/13): songs whose title is a remix / live take / instrumental … and played songs + * whose linked video is one while the title is not ("바다 이야기" → "Over See (Live)"). They go to the 휴지통 (30 days, + * 관리자 → 곡 → 휴지통에서 되살리기). `keep` = ids an admin chose to keep (사이렌 Remix). + */ +export function dropVersions(db: Database, opts: { dryRun?: boolean; keep?: number[] } = {}, now = Date.now()) { + const keep = new Set(opts.keep ?? []); + const out: { id: number; title: string; artist: string; why: 'title' | 'video'; kind: VersionKind; video: string | null }[] = []; + const rows = db + .query<{ id: number; title: string; artist: string; youtube_status: string; youtube_title: string | null }, []>( + 'SELECT id, title, artist, youtube_status, youtube_title FROM songs WHERE deleted_at IS NULL ORDER BY id', + ) + .all(); + for (const r of rows) { + if (keep.has(r.id)) continue; + const t = versionKind(r.title); + if (t) { + out.push({ id: r.id, title: r.title, artist: r.artist, why: 'title', kind: t, video: r.youtube_title }); + continue; + } + const v = r.youtube_status === 'matched' && r.youtube_title ? videoVersionKind(r.youtube_title, r.title) : null; + if (v) out.push({ id: r.id, title: r.title, artist: r.artist, why: 'video', kind: v, video: r.youtube_title }); + } + const trashed = opts.dryRun ? 0 : trashSongs(db, out.map((x) => x.id), now); + return { trashed, songs: out }; +} diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 715e42b..970b68e 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -9,3 +9,4 @@ export * from './custom-quiz'; export * from './custom-quiz-schema'; export * from './quiz-file'; export * from './sheet'; +export * from './title-clean'; diff --git a/packages/shared/src/lite.ts b/packages/shared/src/lite.ts index d9aef7a..abe0d81 100644 --- a/packages/shared/src/lite.ts +++ b/packages/shared/src/lite.ts @@ -6,3 +6,4 @@ export type { ChatLine, RoomView, RoomPlayerView, ServerMessage, GameView, GameS export * from './custom-quiz'; export * from './quiz-file'; export * from './sheet'; +export * from './title-clean'; diff --git a/packages/shared/src/title-clean.test.ts b/packages/shared/src/title-clean.test.ts new file mode 100644 index 0000000..628b159 --- /dev/null +++ b/packages/shared/src/title-clean.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, test } from 'bun:test'; +import { cleanTitle, versionKind, videoVersionKind } from './title-clean'; + +const shown = (t: string) => cleanTitle(t).title; + +describe('제목 정리 (docs/13)', () => { + test('feat./Prod./OST/remaster/version notes go, nothing is kept as an answer', () => { + expect(cleanTitle('쉬어 (Feat. MINO) (Prod. GRAY)')).toEqual({ title: '쉬어', aliases: [] }); + expect(cleanTitle('Radio Ga Ga (Remastered 2011)')).toEqual({ title: 'Radio Ga Ga', aliases: [] }); + expect(cleanTitle('Seven (feat. Latto) - Clean Ver.')).toEqual({ title: 'Seven', aliases: [] }); + expect(shown('호우주의 (Feat. 개코, 넉살 (Nucksal)) (Prod. 코드 쿤스트)')).toBe('호우주의'); + expect(cleanTitle('호우주의 (Feat. 개코, 넉살 (Nucksal))').aliases).toEqual([]); + expect(shown('Beautiful [도깨비 OST]')).toBe('Beautiful'); + expect(shown('밤양갱 feat. 누구')).toBe('밤양갱'); + expect(shown('그댄 행복에 살텐데 (2022)')).toBe('그댄 행복에 살텐데'); + expect(cleanTitle('시간을 거슬러 (낮에 뜨는 달 X 케이윌)')).toEqual({ title: '시간을 거슬러', aliases: [] }); + expect(cleanTitle('アイドル (애니메이션 \'최애의 아이\' 1기 오프닝곡)')).toEqual({ title: 'アイドル', aliases: [] }); + expect(shown('Poppy (Korean Ver.)')).toBe('Poppy'); + }); + test('version marks go from the title too', () => { + expect(shown('Finesse (Remix) (Feat. Cardi B)')).toBe('Finesse'); + expect(shown('사이렌 Remix')).toBe('사이렌'); + expect(shown('깡 Official Remix')).toBe('깡'); + expect(shown('아침(Inst.)')).toBe('아침'); + }); + test('another name in brackets stays an answer', () => { + expect(cleanTitle('사랑을 했다(LOVE SCENARIO)')).toEqual({ title: '사랑을 했다', aliases: ['LOVE SCENARIO', '사랑을 했다 LOVE SCENARIO'] }); + expect(cleanTitle('광(狂)').title).toBe('광'); + expect(cleanTitle('미스터 (Mr.)')).toEqual({ title: '미스터', aliases: [] }); + }); + test('a leading bracket and a part number stay as words', () => { + expect(cleanTitle("(I Can't Help) Falling In Love With You")).toEqual({ title: "I Can't Help Falling In Love With You", aliases: ['Falling In Love With You'] }); + expect(shown('Love The Way You Lie (Part II) (Feat. Eminem)')).toBe('Love The Way You Lie Part II'); + }); + test('names that only look like marks are left alone', () => { + for (const t of ['Live Wire', 'How Do I Live', 'Time Is [L]over', 'Mr. Simple', 'MR.TAXI', '(여자)아이들']) expect(shown(t)).toBe(t); + }); + test('a Japanese title with its Korean name in brackets shows the Korean name', () => { + expect(cleanTitle('貴方の恋人になりたい (당신의 연인이 되고 싶어)')).toEqual({ title: '당신의 연인이 되고 싶어', aliases: ['貴方の恋人になりたい 당신의 연인이 되고 싶어', '貴方の恋人になりたい'] }); + }); +}); + +describe('원곡이 아닌 곡 (docs/13)', () => { + test('remix / live / inst. titles', () => { + expect(versionKind('Macarena (Bayside Boys Remix)')).toBe('remix'); + expect(versionKind('거북선 Remix')).toBe('remix'); + expect(versionKind("I'm Real (Murder Remix Feat. Ja Rule-Clean Version)")).toBe('remix'); + expect(versionKind('Signs (Live At The Trocadero / 1990)')).toBe('live'); + expect(versionKind('I\'ll Be There (Live at MTV Unplugged, Kaufman Astoria Studios, New York - March 1992) (Feat. Trey Lorenz)')).toBe('live'); + expect(versionKind('아침(Inst.)')).toBe('inst'); + }); + test('originals', () => { + for (const t of [ + 'Live Wire', + 'How Do I Live', + '미스터 (Mr.)', + 'Mr. BangBang', + 'Live Is Life (Digitally Remastered) (Single Ver.)', + 'To Live And Die In L.A. (From "To Live And Die In L.A." Soundtrack)', + 'Radio Ga Ga (Remastered 2011)', + 'Creep (Explicit Ver.)', + 'How Do You Sleep', + ]) + expect(versionKind(t)).toBeNull(); + }); + test('a linked video that is a live take / inst. of a song whose title is not', () => { + expect(videoVersionKind('Over See (Live) (바다 이야기)', '바다 이야기')).toBe('live'); + expect(videoVersionKind('Only When I Sleep - Bossa Unplugged', 'Only When I Sleep')).toBe('live'); + expect(videoVersionKind('Mr. Bang Bang (Inst.)', 'Mr. BangBang')).toBe('inst'); + expect(videoVersionKind('달콤한 빈말 (Feat. 바버렛츠) (MR)', '달콤한 빈말')).toBe('inst'); + expect(videoVersionKind('Lose Yourself (Instrumental)', 'Lose Yourself (Soundtrack Version)')).toBe('inst'); + expect(videoVersionKind('karaoke (노래방에서)', '노래방에서')).toBeNull(); + expect(videoVersionKind('Mr. Chu (On Stage) (MR. CHU (ON Stage))', 'Mr. Chu')).toBeNull(); + expect(videoVersionKind('Siren Remix (Feat. UNEDUCATED KID)', '사이렌 Remix')).toBeNull(); + }); +}); diff --git a/packages/shared/src/title-clean.ts b/packages/shared/src/title-clean.ts new file mode 100644 index 0000000..74096b4 --- /dev/null +++ b/packages/shared/src/title-clean.ts @@ -0,0 +1,192 @@ +/** + * 제목 정리 (docs/13): the title people see has no brackets, no feat. and no version marks + * ("Radio Ga Ga (Remastered 2011)" → "Radio Ga Ga", "Seven (feat. Latto) - Clean Ver." → "Seven"). + * What was inside a bracket that is another name of the song ("사랑을 했다(LOVE SCENARIO)") is kept as an answer alias. + * Artist names are left alone ("(여자)아이들", "LE SSERAFIM (르세라핌)"). + */ + +const PAIRS: Record = { '(': ')', '[': ']', '{': '}', '【': '】', '「': '」', '『': '』', '(': ')', '[': ']', '〈': '〉', '《': '》' }; +const CLOSERS = new Set(Object.values(PAIRS)); + +/** A bracket starting with one of these is a note about the recording or the credits, not a name. */ +const TAIL_START = + /^(?:feat\.?|ft\.?|featuring|prod\.?|produced|with|from|duet|narr\.?|원곡|inst\.?|instrumental|remaster(?:ed)?|digitally|bonus|explicit|clean|dirty|sped\s*up|slowed|live|ver\.?|version|mr|radio|single|album|lp|mono|stereo|extended|original|acoustic|unplugged|stripped|remix|edit|demo|cover|theme|taken\s+from|soundtrack|re-?recorded|revised|english|korean|japanese|chinese|cf\b|(?:19|20)\d{2}\b)/i; +/** …or ending with one of these. */ +const TAIL_END = + /(?:\bver\.?|\bversion|\bremix(?:ed)?|\bmix|\bedit|\bost|\bsoundtrack|\bremaster(?:ed)?|\blive|\bedition|\bcut|\bmr|\binst\.?|\binstrumental|\btheme|\bsolo|\bcf|버전|리믹스|라이브|반주|주제가|삽입곡|가창곡|리메이크곡|배경음악|추천곡|광고|오프닝곡?|엔딩곡?|ost|편)\s*$/i; +/** …or containing one of these anywhere ("Track Masters Remix Featuring …", "드라마 '도깨비' OST Part.1", "여름날 우리 X 로이킴"). */ +const TAIL_ANY = + /\bremix(?:ed)?\b|\bfeat(?:uring)?\b|\bft\.|\bprod\.|\bost\b|\bremaster|\bunplugged\b|\bsoundtrack\b|\btheme\s+from\b|\sX\s|원곡|리믹스|라이브|반주|버전|ver\.|삽입곡|주제가|광고|드라마|웹툰|영화|애니메이션|무한도전|\bopening\b|\bending\b/i; +/** A bracket that numbers the song stays as words: "Love The Way You Lie (Part II)" → "… Part II". */ +const NUMBERING = /^(?:part|pt\.?|vol\.?|no\.?|chapter|ep\.?|season)\s*(?:\d+|[ivx]+|one|two|three|four|five)\.?$/i; + +/** The inside of a bracket (or a ` - ` suffix) is a note, not another name of the song. */ +export function isTitleNote(inner: string): boolean { + const t = inner.trim(); + if (!t) return true; + return TAIL_START.test(t) || TAIL_END.test(t) || TAIL_ANY.test(t) || /^(?:19|20)\d{2}$/.test(t); +} + +/** feat./ft./Featuring/Prod. written after the title without brackets: everything from there on is credits. */ +const BARE_CREDITS = /\s+(?:feat\.?|ft\.|featuring|prod\.|prod\s+by)\s.*$/i; +/** + * A version mark written after the title without brackets: "거북선 Remix", "깡 Official Remix", "S&M Remix", "… Clean Ver.", + * "… Inst.". A bare "Live"/"Acoustic" at the end is part of the name ("How Do I Live"). + */ +const BARE_VERSION = /\s+(?:(?:official|original|korean|english|japanese|radio|club|extended)\s+)?remix\s*$|\s+(?:\S+\s+)?ver\.\s*$|\s+inst\.\s*$/i; +/** ` - Clean Ver.`, ` - Remastered 2011`. */ +const DASH_SUFFIX = /\s+[-–—]\s+([^-–—]+)$/; + +interface Group { + /** Text inside, nested brackets flattened to their words. */ + inner: string; + start: number; + end: number; +} + +/** Top-level bracket groups. A bracket glued inside a word ("Time Is [L]over") is not a group. */ +function groups(s: string): Group[] { + const out: Group[] = []; + const stack: { ch: string; at: number }[] = []; + for (let i = 0; i < s.length; i++) { + const ch = s[i]!; + if (PAIRS[ch]) stack.push({ ch, at: i }); + else if (CLOSERS.has(ch) && stack.length && PAIRS[stack[stack.length - 1]!.ch] === ch) { + const open = stack.pop()!; + if (stack.length) continue; + const glued = /[\p{L}\p{N}]/u.test(s[i + 1] ?? '') && /^[\p{L}\p{N}]{1,3}$/u.test(s.slice(open.at + 1, i)); + if (glued) continue; + out.push({ inner: squash(s.slice(open.at + 1, i).replace(/[([{【「『([〈《)\]}】」』)]〉》]/g, ' ')), start: open.at, end: i + 1 }); + } + } + return out; +} + +export interface CleanTitle { + /** The title to show: no brackets, credits or version marks. Same as the input when nothing had to go. */ + title: string; + /** Other names that were in brackets, as answers: each part alone and the whole title read without brackets. */ + aliases: string[]; +} + +const squash = (s: string) => s.replace(/\s+/g, ' ').trim(); +const KANA_HAN = /[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u; +const HANGUL = /\p{Script=Hangul}/u; +const hangulName = (s: string) => HANGUL.test(s) && !KANA_HAN.test(s) && !/[A-Za-z]/.test(s); + +/** Strips brackets, feat. and version marks from a song title; see the file comment. */ +export function cleanTitle(raw: string): CleanTitle { + const src = squash(raw.normalize('NFC')); + let s = src; + const dash = DASH_SUFFIX.exec(s); + if (dash && isTitleNote(dash[1]!) && s.slice(0, dash.index).trim()) s = s.slice(0, dash.index); + const names: string[] = []; + let shown = ''; + let whole = ''; + let rest = ''; + let at = 0; + for (const g of groups(s)) { + const before = s.slice(at, g.start); + shown += before; + whole += before; + rest += before; + at = g.end; + if (isTitleNote(g.inner)) { + shown += ' '; + whole += ' '; + rest += ' '; + } else if (NUMBERING.test(g.inner) || (g.start === 0 && s.slice(g.end).trim())) { + // "(Part II)" names which song; "(I Just) Died In Your Arms" reads as one title + shown += ` ${g.inner} `; + whole += ` ${g.inner} `; + rest += ' '; + if (g.start === 0) names.push(''); + } else { + names.push(g.inner); + shown += ' '; + whole += ` ${g.inner} `; + rest += ' '; + } + } + shown += s.slice(at); + whole += s.slice(at); + rest += s.slice(at); + const tidy = (x: string) => { + // an unclosed bracket at the end ("Title (feat. X") is cut off + let t = squash(x.replace(/\s*[([{【「『([〈《][^)\]}】」』)]〉》]*$/, ' ')).replace(BARE_CREDITS, ''); + for (let i = 0; i < 2; i++) t = squash(t).replace(BARE_VERSION, ''); + return squash(t).replace(/^[\s\-–—:/,·]+|[\s\-–—:/,·]+$/g, ''); + }; + let title = tidy(shown); + if (!title) return { title: src, aliases: [] }; + const aliases = new Set(); + const add = (x: string) => { + const t = tidy(x); + if (t && t.toLowerCase() !== title.toLowerCase()) aliases.add(t); + }; + for (const n of names) if (n) add(n); + if (names.length) { + add(whole); + add(rest); + } + // 貴方の恋人になりたい (당신의 연인이 되고 싶어): a Japanese title with its Korean name in brackets shows the Korean name + if (KANA_HAN.test(title) && !HANGUL.test(title)) { + const ko = names.find(hangulName); + if (ko) { + aliases.delete(ko); + aliases.add(title); + title = ko; + } + } + return { title, aliases: [...aliases] }; +} + +export type VersionKind = 'remix' | 'live' | 'inst' | 'acoustic' | 'sped'; + +const KIND_RULES: [VersionKind, RegExp][] = [ + ['remix', /\bremix(?:ed)?\b|리믹스/i], + ['live', /^\s*live\b|\blive\s*(?:ver\.?|version)?\s*$|\blive\s+(?:at|in|from|on|session|concert|tour)\b|\bunplugged\b|라이브|실황/i], + ['inst', /^\s*(?:inst\.?|instrumental|mr|karaoke)\s*$|\binstrumental\b|\binst\.(?=\W|$)|\bkaraoke\b|반주|노래방/i], + ['acoustic', /\bacoustic\b|어쿠스틱/i], + ['sped', /\bsped\s*up\b|\bslowed\b/i], +]; + +/** + * The title says the song is not the original recording: a remix, a live take, an instrumental/MR, an acoustic or sped-up + * version. Only marks in brackets, a ` - ` suffix or a bare "… Remix" at the end count ("Live Wire", "How Do I Live", + * "미스터 (Mr.)", "(From "To Live And Die In L.A." Soundtrack)" are originals). + */ +export function versionKind(title: string): VersionKind | null { + const t = squash(title.normalize('NFC')); + const parts = groups(t).map((g) => g.inner); + const dash = DASH_SUFFIX.exec(t); + if (dash) parts.push(dash[1]!); + for (const p of parts) { + if (/^\s*(?:from|taken\s+from)\b/i.test(p)) continue; + for (const [kind, re] of KIND_RULES) if (re.test(p)) return kind; + } + let bare = t; + for (const g of groups(t).reverse()) bare = bare.slice(0, g.start) + ' ' + bare.slice(g.end); + const m = BARE_VERSION.exec(squash(bare)); + if (m) { + const w = m[0].toLowerCase(); + if (/remix/.test(w)) return 'remix'; + if (/inst/.test(w)) return 'inst'; + for (const [kind, re] of KIND_RULES) if (re.test(w)) return kind; + } + return null; +} + +const looseName = (s: string) => s.normalize('NFKC').toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ''); + +/** + * The linked video is a remix / live take / instrumental … that the song's own title does not say + * ("Over See (Live) (바다 이야기)" for 바다 이야기). A bracket that is just the song's title is not a mark ("karaoke (노래방에서)"). + */ +export function videoVersionKind(videoTitle: string, songTitle: string): VersionKind | null { + if (versionKind(songTitle)) return null; + const own = new Set([looseName(songTitle), looseName(cleanTitle(songTitle).title)]); + let t = squash(videoTitle.normalize('NFC')); + for (const g of groups(t).reverse()) if (own.has(looseName(g.inner))) t = t.slice(0, g.start) + ' ' + t.slice(g.end); + return versionKind(t); +}