recipe-ingredient-parser-v3
Version:
Natural language parser for recipes and ingredient lists, incl. combining ingredients
46 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.petraPostprocessing = exports.petraPreprocessing = void 0;
var petra_keywords_1 = require("./petra-keywords");
/**
* Process a product string by “underscoring” a product name (a keyword)
* when needed. In our domain the product name might include a number.
*
* In particular, consider these two cases:
*
* 1. "Farina Petra 10 g"
* → No change because the only number ("10") belongs to the quantity.
*
* 2. "Farina Petra 10 10 g"
* → The first "10" is meant to be part of the name.
* The function returns "Farina_Petra_10 10 g".
*
* If no quantity is detected at the end, the function falls back on
* replacing any found keyword (from a predefined list) with its underscored version,
* without changing the order of the words.
*
* @param str The input product string.
* @param units An array of valid unit strings (e.g. ["g", "kg", "ml", "l"]).
* @returns The processed string.
*/
function petraPreprocessing(str, units, _keywords) {
// Get keywords list
var keywords = _keywords || petra_keywords_1.getPetraKeywords();
units;
str = str.trim().replace(/^(-)/, "").toLowerCase();
// Replace any matching keyword with its underscored version
for (var _i = 0, keywords_1 = keywords; _i < keywords_1.length; _i++) {
var keyword = keywords_1[_i];
if (str.includes(keyword)) {
str = str.replace(keyword, keyword.replace(/ /g, "_"));
break;
}
}
return str;
}
exports.petraPreprocessing = petraPreprocessing;
function petraPostprocessing(str) {
return str.replace(/_/g, " ");
}
exports.petraPostprocessing = petraPostprocessing;
//# sourceMappingURL=petra-convert.js.map