/** * เครื่องยนต์ค้นหาโรงหนัง * * แยกออกจาก `app/lib/cinemas.ts` (ซึ่งเป็น "ข้อมูล + ตัวกรองหมวด") เพื่อให้แต่ละไฟล์ * ทำหน้าที่เดียว อ่านเข้าใจง่าย และเทสต์แยกได้โดยไม่ต้องโหลดข้อมูลทั้งชุด * * ลำดับการทำงาน * 1) normalizeSearchText / foldSearchText — ทำให้ข้อความเทียบกันได้ * 2) searchTokens — แยกคำค้นเป็นหลายคำ (ทุกคำต้องเจอ = AND, เรียงสลับได้) * 3) buildSearchIndex — เตรียมข้อความทุกฟิลด์ของโรงหนึ่ง (ทำครั้งเดียวต่อโรง) * 4) matchLevel + FIELD_WEIGHTS — ให้คะแนนความตรง * 5) scoreCinema / filterCinemas — รวมผลและเรียงจากตรงที่สุด */ import { hasCategory, type Cinema, type CinemaCategory } from "@/lib/cinemas"; /** อักขระล่องหนที่ติดมากับการคัดลอกข้อความ (zero-width, BOM) */ const ZERO_WIDTH = /[\u200B-\u200D\u2060\uFEFF]/g; /** วรรณยุกต์/สระบน-ล่าง (combining marks) — ใช้เทียบแบบหลวม */ const COMBINING_MARKS = /\p{M}+/gu; /** * ปรับข้อความให้เทียบกันได้: NFKC, ตัดอักขระล่องหน, ตัวพิมพ์เล็ก, * แปลงตัวคั่น (· • / & — …) เป็นช่องว่าง แล้วยุบช่องว่างซ้ำ */ export function normalizeSearchText(value: string): string { return value .normalize("NFKC") .replace(ZERO_WIDTH, "") .toLowerCase() .replace(/[·•・,،.\\/_|+&()\[\]{}<>"'’‘`“”«»—–~*:;!?$%^@#№-]+/g, " ") .replace(/\s+/g, " ") .trim(); } /** * รูปแบบ "หลวม" ของข้อความ: ตัดวรรณยุกต์/สระบน-ล่างออก * ทำให้ "Française" = "francaise", "เฮ้าส์" = "เฮาส" (พิมพ์ไม่ครบ/ไม่ใส่วรรณยุกต์ก็เจอ) */ export function foldSearchText(value: string): string { return normalizeSearchText(value) .normalize("NFD") .replace(COMBINING_MARKS, "") .replace(/[^\p{L}\p{N}\s]+/gu, " ") .replace(/\s+/g, " ") .trim(); } /** แยกคำค้นเป็น token — คืนอาร์เรย์ว่างเมื่อไม่มีอะไรให้ค้น */ export function searchTokens(query: string): string[] { const normalized = normalizeSearchText(query); if (normalized === "") return []; return normalized.split(" ").filter((token) => token !== ""); } /** * กลุ่มฟิลด์ที่ค้น + น้ำหนัก * ชื่อโรงสำคัญที่สุด รองลงมาสถานที่/หมวด และรายละเอียดเป็นตัวสุดท้าย */ export const FIELD_WEIGHTS = { names: 400, place: 180, categories: 150, details: 60, } as const; export type FieldGroup = keyof typeof FIELD_WEIGHTS; /** * ระดับความตรง — คูณกับน้ำหนักของกลุ่มฟิลด์ * | ระดับ | ค่า | ตัวอย่าง (ค้น "house") | * |------------------|-------|-----------------------------------| * | เท่ากันทั้งค่า | 3 | ฟิลด์ = "house" | * | ขึ้นต้นด้วยคำ | 2 | "house cinema samyan" | * | มีคำอยู่ข้างใน | 1.2 | "the house of films" | * | แบบหลวม (ตัดวรรณยุกต์) | 0.8 | "เฮ้าส์" จับคู่ "เฮาส" | * | ไม่ตรง | 0 | — | */ export const MATCH_LEVEL = { exact: 3, prefix: 2, contains: 1.2, loose: 0.8, } as const; /** ฟิลด์หนึ่งฟิลด์ที่เตรียมไว้เทียบแล้ว (แปลงครั้งเดียว ใช้ได้ทุกคำค้น) */ export type IndexedField = { group: FieldGroup; exact: string; loose: string; }; /** * รวมข้อความทุกจุดที่ผู้ใช้เห็น (ชื่อ/สถานที่/หมวด/รายละเอียด) แล้วแปลงล่วงหน้า * เรียกครั้งเดียวต่อโรง แล้วนำผลไปให้คะแนนได้หลาย token โดยไม่ต้องแปลงซ้ำ */ export function buildSearchIndex( cinema: Cinema, tagLabels: Record = {}, ): IndexedField[] { const raw: Record = { names: [cinema.name, cinema.name_th ?? "", cinema.slug.replace(/-/g, " ")], place: [ cinema.location_label, cinema.location_label_en ?? "", ...(cinema.address_lines ?? []), ...(cinema.address_lines_en ?? []), ...cinema.transport.flatMap((mode) => [mode.value, mode.value_en ?? ""]), ], categories: [ ...cinema.tags, ...cinema.tags.map((tag) => tagLabels[tag] ?? ""), ], details: [ cinema.description, cinema.description_en ?? "", ...cinema.contacts.flatMap((contact) => [contact.label, contact.value]), ], }; const index: IndexedField[] = []; for (const group of Object.keys(raw) as FieldGroup[]) { for (const value of raw[group]) { if (value.trim() === "") continue; index.push({ group, exact: normalizeSearchText(value), loose: foldSearchText(value), }); } } return index; } /** ความตรงของ "หนึ่งฟิลด์กับหนึ่งคำค้น" — คืน 0 เมื่อไม่ตรง */ function matchLevel( field: IndexedField, token: string, looseToken: string, ): number { if (field.exact === token) return MATCH_LEVEL.exact; if (field.exact.startsWith(token)) return MATCH_LEVEL.prefix; if (field.exact.includes(token)) return MATCH_LEVEL.contains; if (looseToken !== "" && field.loose.includes(looseToken)) { return MATCH_LEVEL.loose; } return 0; } /** * ให้คะแนนโรงหนึ่งกับคำค้น (0 = ไม่ตรง) * ทุกคำต้องเจออย่างน้อยหนึ่งฟิลด์ (AND) แล้วนำคะแนนของแต่ละคำมารวมกัน */ export function scoreCinema( cinema: Cinema, tokens: string[], tagLabels: Record = {}, ): number { if (tokens.length === 0) return 0; const index = buildSearchIndex(cinema, tagLabels); let total = 0; for (const token of tokens) { const looseToken = foldSearchText(token); let bestForToken = 0; for (const field of index) { const level = matchLevel(field, token, looseToken); if (level === 0) continue; bestForToken = Math.max(bestForToken, FIELD_WEIGHTS[field.group] * level); } if (bestForToken === 0) return 0; total += bestForToken; } return total; } /** * กรอง + เรียงผลลัพธ์ * - ไม่มีคำค้น → คงลำดับเดิม (ลำดับรายวัน/ยอดนิยม) แล้วกรองแค่หมวด * - มีคำค้น → เรียงจากคะแนนมากไปน้อย (คะแนนเท่ากันคงลำดับเดิม) */ export function filterCinemas( cinemas: Cinema[], { category, query, tagLabels = {}, }: { category: CinemaCategory; query: string; /** ป้ายหมวดตามภาษาที่แสดงบนการ์ด (messages.tags) */ tagLabels?: Record; }, ): Cinema[] { const inCategory = cinemas.filter((cinema) => hasCategory(cinema, category)); const tokens = searchTokens(query); if (tokens.length === 0) return inCategory; return inCategory .map((cinema, index) => ({ cinema, index, score: scoreCinema(cinema, tokens, tagLabels), })) .filter((row) => row.score > 0) .sort((a, b) => b.score - a.score || a.index - b.index) .map((row) => row.cinema); }