UNPKG

calamarcopollo

Version:
199 lines (187 loc) 8.27 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _actionCreators = require('../actionCreators'); var _tripDialog = require('../tripDialog'); var _fuzzy = require('../fuzzy'); var _calamars = require('calamars'); const { getEntityValue, getEntityMeta } = _calamars.WitDriver; const confidence = 0.77; const confidenceToFillContext = 0.7; const checkConfidences = ({ origin, destination, unknownPlace }, context) => { const hasContext = context !== undefined; const destNotInContext = hasContext && !context.destination; const originNotInContext = hasContext && !context.origin; const placesNotInContext = hasContext && (context.origin && !context.destination || context.destination && !context.origin); const destConfidence = destNotInContext ? confidenceToFillContext : confidence; const originConfidence = originNotInContext ? confidenceToFillContext : confidence; const placeConfidence = placesNotInContext ? confidenceToFillContext : confidence; console.log('checkConfidences', destConfidence, originConfidence, placeConfidence); const hasDestination = destination && destination.confidence >= destConfidence; const hasOrigin = origin && origin.confidence >= originConfidence; const hasUnknownPlace = unknownPlace && unknownPlace.confidence >= placeConfidence; return { hasDestination, hasOrigin, hasUnknownPlace }; }; const routes = [[outcomes => { const hasTripIntent = getEntityValue(outcomes, 'trip') === 'info'; return hasTripIntent; }, (outcomes, { store, chat } = {}) => { console.log('---- trip intent ---'); const { origin, origins, originMeta, destination, destinationMeta, unknownPlace, unknownPlaces, timeFilter, busTypeFilters, priceFilter } = (0, _fuzzy.extractEntities)(outcomes); const context = chat && chat.session ? chat.session : {}; let nextContext = Object.assign({}, context); if (timeFilter) { nextContext.timeFilter = timeFilter; } if (busTypeFilters && busTypeFilters.length) { nextContext.busTypeFilters = busTypeFilters; } if (priceFilter) { nextContext.priceFilter = priceFilter; } if (destination && destination.confidence >= confidence) { nextContext.destination = destination.value; nextContext.destinationMeta = destinationMeta; } if (origin && origin.confidence >= confidence) { nextContext.origin = origin.value; nextContext.originMeta = originMeta; if (!destination) { if (origins && origins.length > 1 && origins[1].confidence >= confidence) { nextContext.destination = origins[1].value; nextContext.destinationMeta = getEntityMeta(origins[1]); } else if (unknownPlace) { nextContext.destination = unknownPlace.value; nextContext.destinationMeta = getEntityMeta(unknownPlace); } } } if (!origin && !destination && unknownPlaces && unknownPlaces.length > 1) { // console.log('[issue #25]: 2 places and no role'); nextContext.origin = unknownPlaces[0].value; nextContext.originMeta = getEntityMeta(unknownPlaces[0]); nextContext.destination = unknownPlaces[1].value; nextContext.destinationMeta = getEntityMeta(unknownPlaces[1]); } if (unknownPlaces && nextContext.destination && !nextContext.origin) { nextContext.origin = unknownPlaces[0].value; nextContext.originMeta = getEntityMeta(unknownPlaces[0]); } if (unknownPlaces && nextContext.origin && !nextContext.destination || unknownPlaces && !nextContext.origin && !nextContext.destination) { nextContext.destination = unknownPlaces[0].value; nextContext.destinationMeta = getEntityMeta(unknownPlaces[0]); } if (store) { store.dispatch((0, _actionCreators.updateChatSession)({ chat: _extends({}, chat, { session: nextContext }) })); } return (0, _tripDialog.tripDialogReply)(nextContext); }], [(outcomes, { chat } = {}) => { const context = chat && chat.session ? chat.session : {}; const { unknownPlace, origin, destination, busTypeFilters, priceFilter, timeFilter } = (0, _fuzzy.extractEntities)(outcomes); const { hasDestination, hasOrigin, hasUnknownPlace } = checkConfidences({ origin, destination, unknownPlace }, context); const result = hasDestination || hasOrigin || hasUnknownPlace || busTypeFilters || priceFilter || timeFilter; return result; }, (outcomes, { store, chat } = {}) => { console.log('---- no trip intent ---'); const context = chat && chat.session ? chat.session : {}; const { unknownPlace, origin, destination, busTypeFilters, priceFilter, timeFilter } = (0, _fuzzy.extractEntities)(outcomes); const { hasDestination, hasOrigin, hasUnknownPlace } = checkConfidences({ origin, destination, unknownPlace }, context); const unknownPlaceMeta = getEntityMeta(unknownPlace); let nextContext = Object.assign({}, context); if (timeFilter) { nextContext.timeFilter = timeFilter; } if (busTypeFilters && busTypeFilters.length) { nextContext.busTypeFilters = busTypeFilters; } if (priceFilter) { nextContext.priceFilter = priceFilter; } if (hasOrigin) { console.log('1 origin', origin); if (hasUnknownPlace) { console.log('1.1 origin + place', origin, unknownPlace); nextContext.origin = origin.value; nextContext.originMeta = getEntityMeta(origin); nextContext.destination = unknownPlace.value; nextContext.destinationMeta = getEntityMeta(unknownPlace); } else if (!context.destination && context.origin) { console.log('2 origin with context missing destination', origin); nextContext.destination = origin.value; nextContext.destinationMeta = getEntityMeta(origin); } else { console.log('3 origin ignoring what is already in context', origin); nextContext.origin = origin.value; nextContext.originMeta = getEntityMeta(origin); } } else { console.log('4 not origin', destination, unknownPlace); if (hasUnknownPlace && hasDestination) { console.log('5 not origin, destination and unknowPlace'); nextContext.origin = unknownPlace.value; nextContext.originMeta = unknownPlaceMeta; } if (hasUnknownPlace && context.destination && !context.origin) { console.log('6 not origin, unknowPlace and the context has only destination'); nextContext.origin = unknownPlace.value; nextContext.originMeta = unknownPlaceMeta; } } if (hasDestination) { console.log('7 destination', destination, destination.confidence); nextContext.destination = destination.value; nextContext.destinationMeta = getEntityMeta(destination); } else { console.log('8 not destination, unknowPlace and the context has only destination'); if (hasUnknownPlace && !context.destination && context.origin || hasUnknownPlace && !context.destination && !context.origin && !origin) { console.log('9 not dest, unknowPlace and the context has nothing or only origin'); nextContext.destination = unknownPlace.value; nextContext.destinationMeta = unknownPlaceMeta; } } // console.log('=====nextContext', nextContext); if (store) { store.dispatch((0, _actionCreators.updateChatSession)({ chat: _extends({}, chat, { session: nextContext }) })); } return (0, _tripDialog.tripDialogReply)(nextContext); }]]; exports.default = routes;