diff --git a/apps/server/src/answers/index.ts b/apps/server/src/answers/index.ts index b0badbe..aafd52b 100644 --- a/apps/server/src/answers/index.ts +++ b/apps/server/src/answers/index.ts @@ -10,7 +10,7 @@ import type { Database } from 'bun:sqlite'; import { cleanTitle } from '@mq/shared'; import { englishToHangul } from './en-ko'; import { kanaToHangul } from './ja-ko'; -import { hasJapanese, readJapanese, type JaReader } from './ja-reading'; +import { hasJapanese, japaneseName, readJapanese, type JaReader } from './ja-reading'; import type { WikiLookup } from './wiki'; export interface SongAnswers { @@ -115,7 +115,7 @@ export async function songAnswersBatch(db: Database | null, songs: { title: stri // the Japanese title: shown as is, or already swapped for its bracket's Korean name by cleanTitle // ("夜に駆ける (밤에 달리다)") — Wikipedia still goes first, so the order is decided on the original // only when the title outside the brackets was Japanese: "몽(夢)" is a Korean title with a 한자 alias, never renamed - const outsideJa = hasJapanese(s.title.replace(/[([{([【「『][^)\]})]】」』]*[)\]})]】」』]/g, ' ')); + const outsideJa = japaneseName(s.title); const jaTitle = hasJapanese(c.title) ? c.title : outsideJa ? c.aliases.find((a) => hasJapanese(a)) : undefined; if (opts.korean && jaTitle) { const bracket = hasJapanese(c.title) ? c.aliases.find((a) => HANGUL.test(a) && !hasJapanese(a)) : c.title; diff --git a/apps/server/src/answers/ja-reading.ts b/apps/server/src/answers/ja-reading.ts index a5da0e6..2738924 100644 --- a/apps/server/src/answers/ja-reading.ts +++ b/apps/server/src/answers/ja-reading.ts @@ -6,6 +6,12 @@ import { join } from 'node:path'; export const hasJapanese = (s: string) => /[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u.test(s) && !/\p{Script=Hangul}/u.test(s); +/** + * The name outside its brackets is Japanese: "夜に駆ける (밤에 달리다)" is, "몽(夢)" and "Good bye-bye (グッバイバイ)" are not + * (a Korean/English name with a Japanese or 한자 alias). + */ +export const japaneseName = (s: string) => hasJapanese(s.replace(/[([{([【「『][^)\]})]】」』]*[)\]})]】」』]/g, ' ')); + export type JaReader = (texts: string[]) => Promise<(string | null)[]>; export const readJapanese: JaReader = async (texts) => { diff --git a/apps/server/src/songs/title-review.ts b/apps/server/src/songs/title-review.ts index 5161494..2682eb1 100644 --- a/apps/server/src/songs/title-review.ts +++ b/apps/server/src/songs/title-review.ts @@ -6,7 +6,7 @@ * charts keep finding the song by its source name. A row whose song was edited since it was made is not applied. */ import type { Database } from 'bun:sqlite'; -import { hasJapanese } from '../answers/ja-reading'; +import { japaneseName } from '../answers/ja-reading'; import { songAnswersBatch, type AnswerOptions } from '../answers'; export type TitleReviewKind = 'title' | 'answers' | 'japanese' | 'check'; @@ -85,7 +85,7 @@ export async function buildTitleReviews( return; } const notes = a.note ? [a.note] : []; - const kind: TitleReviewKind = a.note?.includes('확인 필요') ? 'check' : hasJapanese(s.title) || hasJapanese(s.artist) ? 'japanese' : renamed ? 'title' : 'answers'; + const kind: TitleReviewKind = a.note?.includes('확인 필요') ? 'check' : japaneseName(s.title) || japaneseName(s.artist) ? 'japanese' : renamed ? 'title' : 'answers'; const status = opts.review || kind === 'japanese' || kind === 'check' ? 'pending' : 'auto'; save.run(s.id, kind, status, s.title, s.artist, a.title, a.artist, JSON.stringify(ta), JSON.stringify(aa), notes.join(' · ') || null, now); if (status === 'auto') { diff --git a/apps/server/src/title-review.test.ts b/apps/server/src/title-review.test.ts index ce6e301..067f899 100644 --- a/apps/server/src/title-review.test.ts +++ b/apps/server/src/title-review.test.ts @@ -92,3 +92,20 @@ test('관리자 API: list, apply a kind, skip; guests may not', async () => { await s.stop(); } }); + +test('new songs: a Japanese title with a Korean bracket ("夜に駆ける (밤에 달리다)") still waits as 일본어 곡; "몽(夢)" is a Korean song', async () => { + const db = openDb(tempDbPath()); + const c = new Catalog(db); + const [ja] = c.upsert({ title: '夜に駆ける (밤에 달리다)', artists: [{ name: 'YOASOBI' }], source: 't', ref: '1' }); + const [ko] = c.upsert({ title: '몽(夢)', artists: [{ name: '가수' }], source: 't', ref: '2' }); + const r = await buildTitleReviews(db, { wiki: async () => new Map() }); + expect(r).toMatchObject({ pending: 1, auto: 1 }); + const rows = listTitleReviews(db, {}).rows; + expect(rows).toHaveLength(1); + expect(rows[0]).toMatchObject({ songId: ja, kind: 'japanese', status: 'pending', newTitle: '밤에 달리다' }); + const title = (id: number) => db.query<{ title: string }, [number]>('SELECT title FROM songs WHERE id = ?').get(id)!.title; + expect(title(ja!)).toBe('夜に駆ける (밤에 달리다)'); + // the Korean song was applied at once, keeping its title and the 한자 as an answer + expect(title(ko!)).toBe('몽'); + expect(db.query<{ alias: string }, [number]>("SELECT alias FROM song_aliases WHERE song_id = ? AND part = 'title'").all(ko!).map((x) => x.alias)).toContain('夢'); +});