pini-print-bot / engine /extractor.js
dotandru's picture
74
691cd90
Raw
History Blame Contribute Delete
3.49 kB
/** engine/extractor.js - With Remove Logic */
const KEYWORD_MAP = {
'bc': 'bc', 'ื›ืจื˜ื™ืก': 'bc', 'ื›ืจื˜ื™ืกื™ื': 'bc', 'ื‘ื™ืงื•ืจ': 'bc',
'flyer': 'flyer', 'ืคืœื™ื™ืจ': 'flyer', 'ืคืœื™ื™ืจื™ื': 'flyer', 'ืขืœื•ืŸ': 'flyer',
'booklet': 'booklet', 'ื—ื•ื‘ืจืช': 'booklet', 'ื—ื•ื‘ืจื•ืช': 'booklet', 'ืกืคืจ': 'booklet', 'ืกืคืจื™ื': 'booklet', 'ืงื˜ืœื•ื’': 'booklet',
'invitation': 'invitation', 'ื”ื–ืžื ื”': 'invitation', 'ื”ื–ืžื ื•ืช': 'invitation', 'ื—ืชื•ื ื”': 'invitation',
'rollup': 'rollup', 'ืจื•ืœืืค': 'rollup', 'ื‘ืื ืจ': 'rollup',
'poster': 'poster', 'ืคื•ืกื˜ืจ': 'poster', 'ืงื ื‘ืก': 'poster',
'sticker': 'sticker', 'ืžื“ื‘ืงื”': 'sticker', 'ืžื“ื‘ืงื•ืช': 'sticker',
'envelope': 'envelope', 'ืžืขื˜ืคื”': 'envelope', 'ืžืขื˜ืคื•ืช': 'envelope',
'folder': 'folder', 'ืคื•ืœื“ืจ': 'folder'
};
const HEBREW_NUMBERS = {
'ืื—ื“': 1, 'ืื—ืช': 1, 'ืฉื ื™': 2, 'ืฉืชื™': 2, 'ืฉืœื•ืฉ': 3, 'ืฉืœื•ืฉื”': 3,
'ืืจื‘ืข': 4, 'ืืจื‘ืขื”': 4, 'ื—ืžืฉ': 5, 'ื—ืžื™ืฉื”': 5, 'ืฉืฉ': 6, 'ืฉื™ืฉื”': 6,
'ืฉื‘ืข': 7, 'ืฉืžื•ื ื”': 8, 'ืชืฉืข': 9, 'ืขืฉืจ': 10, 'ืžืื”': 100, 'ืืœืฃ': 1000
};
function extractParameters(text) {
let cleanText = text.toLowerCase().replace(/,/g, '');
const result = {
products: [],
qty: null,
isReset: false,
isRemove: false, // <--- ื—ื“ืฉ
targetIndex: null, // <--- ื—ื“ืฉ
isCartStatus: false,
raw_text: text
};
// 1. ื–ื™ื”ื•ื™ ืžื—ื™ืงื” ืกืคืฆื™ืคื™ืช
const removeKeywords = ['ืžื—ืง', 'ื”ืกืจ', 'ื‘ื˜ืœ', 'ืœื”ื•ืจื™ื“', 'ืชื•ืจื™ื“'];
if (removeKeywords.some(w => cleanText.includes(w))) {
result.isRemove = true;
// ื ื ืกื” ืœืžืฆื•ื ืžืกืคืจ (ืœืžืฉืœ: "ืžื—ืง ืืช 1")
const numMatch = cleanText.match(/(\d+)/);
if (numMatch) result.targetIndex = parseInt(numMatch[0]);
// ืื ืื™ืŸ ืžืกืคืจ, ื ื ืกื” ืœืžืฆื•ื ืฉื ืžื•ืฆืจ ืœืžื˜ื”...
}
// 2. ื–ื™ื”ื•ื™ ืื™ืคื•ืก ืžืœื
const resetKeywords = ['reset', 'ื”ืชื—ืœ', 'ืชืคืจื™ื˜', 'ื ืงื” ื”ื›ืœ', 'ืื™ืคื•ืก', 'ื™ืฆื™ืื”'];
if (resetKeywords.some(word => cleanText.includes(word)) && !result.isRemove) {
result.isReset = true;
return result;
}
// 3. ื–ื™ื”ื•ื™ ืขื’ืœื”
if (cleanText.includes('cart') || cleanText.includes('ืขื’ืœื”') || cleanText.includes('ืกื™ื›ื•ื') || cleanText.includes('ืกื˜ื˜ื•ืก')) {
result.isCartStatus = true;
return result;
}
// 4. ื–ื™ื”ื•ื™ ืžื•ืฆืจื™ื
const foundProducts = new Set();
Object.keys(KEYWORD_MAP).forEach(keyword => {
if (cleanText.includes(keyword)) {
foundProducts.add(KEYWORD_MAP[keyword]);
}
});
result.products = Array.from(foundProducts);
// 5. ื–ื™ื”ื•ื™ ื›ืžื•ืช
const kMatch = cleanText.match(/(\d+)k/);
if (kMatch) {
result.qty = parseInt(kMatch[1]) * 1000;
} else {
const numMatch = cleanText.match(/\d+/);
if (numMatch) {
// ืื ื–ื” ืœื ืžื—ื™ืงื”, ื ื™ืงื— ืืช ื”ืžืกืคืจ ื›ื›ืžื•ืช
if (!result.isRemove) result.qty = parseInt(numMatch[0]);
} else {
for (const [word, val] of Object.entries(HEBREW_NUMBERS)) {
if (cleanText.includes(word)) {
result.qty = val;
break;
}
}
}
}
return result;
}
module.exports = { extractParameters };