SpaceBridgeX / extension /verticalInference.js
sarransh's picture
Upload folder using huggingface_hub
528c6f5 verified
Raw History Blame Contribute Delete
3.71 kB
/* global self */
/**
* Hotel vs flight capture vertical inference (Auto mode).
* Loaded via importScripts from background.js — keep keyword lists aligned with prompt_rules/shared.md.
*/
(function () {
const HOTEL_KEYWORDS = [
"check-in",
"check in",
"check-out",
"check out",
"hotel",
"room type",
"board basis",
"property",
"nights",
"payable at hotel",
"cancellation policy",
"star hotel",
"accommodation",
"hospitality",
"/hotel",
"hotel?",
];
const FLIGHT_KEYWORDS = [
"flight",
"airline",
"departure",
"arrival",
"terminal",
"boarding",
"e-ticket",
"eticket",
"itinerary",
"pnr",
"record locator",
"baggage",
"cabin class",
"economy class",
"business class",
"flight number",
"flt ",
" amadeus",
"sabre",
"galileo",
"iata",
"/flight",
"airport",
];
function countKeywordHits(corpus, keywords) {
let n = 0;
for (let i = 0; i < keywords.length; i++) {
if (corpus.indexOf(keywords[i]) >= 0) n++;
}
return n;
}
/**
* @param {object} p
* @param {string} p.pageUrl
* @param {string} p.pageTitle
* @param {string} p.visibleText
* @param {Array<{label:string,value:string}>} p.pairs
* @param {string} p.userCaptureVertical - "auto" | "hotel" | "flight"
*/
function inferCaptureVertical(p) {
const user = String(p.userCaptureVertical || "auto").toLowerCase();
if (user === "hotel" || user === "flight") {
return {
resolvedVertical: user,
mode: "user",
scores: { hotel: user === "hotel" ? 1 : 0, flight: user === "flight" ? 1 : 0 },
reason: "user_override",
};
}
const chunks = [
String(p.pageUrl || "").toLowerCase(),
String(p.pageTitle || "").toLowerCase(),
String(p.visibleText || "")
.slice(0, 80000)
.toLowerCase(),
];
const pr = Array.isArray(p.pairs) ? p.pairs : [];
for (let i = 0; i < pr.length && i < 200; i++) {
const x = pr[i];
chunks.push(`${String(x?.label || "").toLowerCase()} ${String(x?.value || "").toLowerCase()}`);
}
const corpus = chunks.join("\n");
const hotelScore = countKeywordHits(corpus, HOTEL_KEYWORDS);
const flightScore = countKeywordHits(corpus, FLIGHT_KEYWORDS);
const strong = 3;
const weak = 1;
if (hotelScore >= strong && flightScore >= strong) {
return {
resolvedVertical: "generic",
mode: "auto",
scores: { hotel: hotelScore, flight: flightScore },
reason: "mixed_signals",
};
}
if (hotelScore > flightScore + 1) {
return {
resolvedVertical: "hotel",
mode: "auto",
scores: { hotel: hotelScore, flight: flightScore },
reason: "keyword_hotel",
};
}
if (flightScore > hotelScore + 1) {
return {
resolvedVertical: "flight",
mode: "auto",
scores: { hotel: hotelScore, flight: flightScore },
reason: "keyword_flight",
};
}
if (hotelScore > 0 || flightScore > 0) {
const pick = hotelScore >= flightScore ? "hotel" : "flight";
return {
resolvedVertical: pick,
mode: "auto",
scores: { hotel: hotelScore, flight: flightScore },
reason: "keyword_weak",
};
}
return {
resolvedVertical: "generic",
mode: "auto",
scores: { hotel: 0, flight: 0 },
reason: "no_signals",
};
}
self.sbxInferCaptureVertical = inferCaptureVertical;
})();