UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

54 lines 2.49 kB
import { __awaiter } from "tslib"; import { checkLocale, KeywordsOptions, KeywordsParser, languageFromLocale, } from '../nlp'; export class SearchByKeywords { constructor(cms, normalizer, keywordsOptions = {}) { this.cms = cms; this.normalizer = normalizer; this.keywordsOptions = keywordsOptions; } /** * It will assign a score based on the ratio of the matched substring length wit respect to the input length */ searchContentsFromInput(inputText, matchType, context, paging) { return __awaiter(this, void 0, void 0, function* () { checkLocale(context.locale); const contentsWithKeywords = yield this.cms.contentsWithKeywords(context, paging); const options = this.keywordsOptions[context.locale] || this.keywordsOptions[languageFromLocale(context.locale)] || new KeywordsOptions(); const kws = new KeywordsParser(context.locale, matchType, this.normalizer, options); contentsWithKeywords.forEach(content => kws.addCandidate(content, content.common.keywords)); const results = kws.findCandidatesWithKeywordsAt(inputText); return results.map(res => { const score = res.match.length / inputText.raw.length; return res.candidate.withResult(res.match, score); }); }); } /** * Chitchat contents need special treatment: does not make sense to ask user to disambiguate, * have less priority than non-chitchat contents,... * @return which contents must be displayed */ filterChitchat(words, results) { const isChitChat = (cc) => cc.getCallbackIfChitchat(); const chitchats = results.filter(isChitChat); if (chitchats.length == 0) { return results; } if (chitchats.length < results.length) { const noChitchats = results.filter(c => !isChitChat(c)); if (chitchats[0].match.length - noChitchats[0].match.length < 2) { return noChitchats; } } // all are chitchats const estimatedNoChitchatWords = words.length - chitchats.length * 2; if (estimatedNoChitchatWords > 2) { // avoid that a sentence with chitchat and a question without recognized keywords is answered as chitchat return []; } return [chitchats[0]]; } } //# sourceMappingURL=search-by-keywords.js.map