VR-MEDIA / src /lib /search.ts
Zero124's picture
VR Media Server 2.0 — latest build (custom Blomp thumbnail auto-detection + full pipeline)
5c0175e
Raw History Blame Contribute Delete
4.99 kB
// Search query parser — supports natural-language queries like:
// "VR180 SBS 8K"
// "Fisheye Videos"
// "Passthrough Ready"
// "Quest 3 Compatible"
// "Resolution > 6K"
// "Duration > 20 Minutes"
//
// Tokenizes the query, recognizes keywords, and produces a structured SearchQuery
// the API can use to filter the library.
import {
type Projection,
type Stereo,
type LensType,
type ResolutionTier,
type SearchQuery,
} from "./types";
const PROJECTION_KEYWORDS: Record<string, Projection> = {
vr180: "vr180",
"180vr": "vr180",
"180": "vr180",
vr360: "vr360",
"360vr": "vr360",
"360": "vr360",
};
const STEREO_KEYWORDS: Record<string, Stereo> = {
sbs: "sbs",
sidebyside: "sbs",
"side-by-side": "sbs",
ou: "ou",
overunder: "ou",
"over-under": "ou",
topbottom: "top_bottom",
tb: "top_bottom",
};
const LENS_KEYWORDS: Record<string, LensType> = {
fisheye: "fisheye",
"fish-eye": "fisheye",
dualfisheye: "dual_fisheye",
"dual-fisheye": "dual_fisheye",
"dual_fisheye": "dual_fisheye",
};
const RESOLUTION_KEYWORDS: Record<string, ResolutionTier> = {
"4k": "4K",
"5k": "5K",
"6k": "6K",
"7k": "7K",
"8k": "8K",
"12k": "12K",
"2160p": "4K",
"2880p": "5K",
"3240p": "6K",
"3780p": "7K",
"4320p": "8K",
"6480p": "12K",
};
const RESOLUTION_ORDER: ResolutionTier[] = ["SD", "HD", "FHD", "4K", "5K", "6K", "7K", "8K", "12K"];
const DURATION_REGEX = /duration\s*>\s*(\d+(?:\.\d+)?)\s*(second|minute|hour|sec|min|hr|s|m|h)?/i;
const RESOLUTION_CMP_REGEX = /resolution\s*>\s*(\d+k|2160p|2880p|3240p|3780p|4320p|6480p)/i;
export function parseSearchQuery(raw: string): SearchQuery {
const lower = raw.toLowerCase().trim();
const filters: SearchQuery["filters"] = {};
// ---- Comparison filters: duration > 20 minutes, resolution > 6K ----
const durationMatch = lower.match(DURATION_REGEX);
if (durationMatch) {
const num = parseFloat(durationMatch[1]);
const unit = (durationMatch[2] ?? "minute").toLowerCase();
const multiplier =
unit.startsWith("s") || unit === "sec" ? 1 :
unit.startsWith("m") || unit === "min" ? 60 :
unit.startsWith("h") || unit === "hr" ? 3600 : 60;
filters.minDuration = num * multiplier;
}
const resCmpMatch = lower.match(RESOLUTION_CMP_REGEX);
if (resCmpMatch) {
const tier = RESOLUTION_KEYWORDS[resCmpMatch[1]];
if (tier) filters.minResolution = tier;
}
// ---- Keyword filters: VR180, SBS, Fisheye, Passthrough, Quest ----
// Tokenize on whitespace + word boundaries. We check each token against
// the keyword maps. Multi-word keywords (side-by-side) are pre-joined.
const normalized = lower
.replace(/side-by-side/g, "sidebyside")
.replace(/over-under/g, "overunder")
.replace(/top-bottom/g, "topbottom")
.replace(/dual-fisheye/g, "dualfisheye")
.replace(/fish-eye/g, "fisheye")
.replace(/quest 3/g, "quest3")
.replace(/quest-3/g, "quest3");
const tokens = normalized.split(/[\s,]+/).filter(Boolean);
const textParts: string[] = [];
for (const token of tokens) {
if (PROJECTION_KEYWORDS[token] && !filters.projection) {
filters.projection = PROJECTION_KEYWORDS[token];
} else if (STEREO_KEYWORDS[token] && !filters.stereo) {
filters.stereo = STEREO_KEYWORDS[token];
} else if (LENS_KEYWORDS[token] && !filters.lensType) {
filters.lensType = LENS_KEYWORDS[token];
} else if (RESOLUTION_KEYWORDS[token] && !filters.resolution) {
filters.resolution = RESOLUTION_KEYWORDS[token];
} else if (token === "passthrough" || token === "passthroughready" || token === "pt") {
filters.passthrough = true;
} else if (token === "quest3" || token === "quest" || token === "questready" || token === "compatible") {
filters.questCompatible = true;
} else if (token === "mr" || token === "mixed" || token === "mixedreality") {
// Mixed reality → implies passthrough
filters.passthrough = true;
} else if (token === "ar" || token === "augmented") {
filters.passthrough = true;
} else if (token === "videos" || token === "video" || token === ">" || token === "minute" || token === "minutes" || token === "second" || token === "seconds" || token === "hour" || token === "hours" || token === "ready") {
// Skip — these are filler words
} else if (/^\d+(\.\d+)?$/.test(token)) {
// Skip standalone numbers — already consumed by comparison regex if relevant
} else {
textParts.push(token);
}
}
return {
raw,
text: textParts.join(" "),
filters,
};
}
/** Helper: convert a ResolutionTier into a numeric rank for comparison. */
export function resolutionRank(tier: ResolutionTier | null | undefined): number {
if (!tier) return -1;
const idx = RESOLUTION_ORDER.indexOf(tier);
return idx === -1 ? -1 : idx;
}