<commit_before>import { updateChatSession } from '../actionCreators';
import { tripDialogReply } from '../tripDialog';
import { extractEntities } from '../fuzzy';
import { WitDriver } from 'calamars';
const { getEntityValue, getEntityMeta } = WitDriver;

const placesConfidenceThreshold = 0.7;

const routes = [[
    outcomes => getEntityValue(outcomes, 'trip') === 'info',
    (outcomes, { store, chat } = {}) => {
        const {
            origin,
            origins,
            originMeta,
            destination,
            destinationMeta,
            unknownPlace,
            unknownPlaces,
            timeFilter
        } = extractEntities(outcomes);
        const context = chat && chat.session ? chat.session : {};
        let nextContext = Object.assign({}, context);
        if (timeFilter) {
            nextContext.timeFilter = timeFilter;
        }
        if (destination && destination.confidence >= placesConfidenceThreshold) {
            nextContext.destination = destination.value;
            nextContext.destinationMeta = destinationMeta;
        }
        if (origin && origin.confidence >= placesConfidenceThreshold) {
            nextContext.origin = origin.value;
            nextContext.originMeta = originMeta;
            if (!destination) {
                if (
                    origins && origins.length > 1 &&
                    origins[1].confidence >= placesConfidenceThreshold
                ) {
                    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(updateChatSession({
                chat: { ...chat, session: nextContext }
            }));
        }
        return tripDialogReply(nextContext);
    }
], [
    outcomes => {
        const {
            unknownPlace,
            origin,
            destination,
            timeFilter
        } = extractEntities(outcomes);
        const place = unknownPlace || origin || destination;
        const result = (
            (origin && destination) ||
            (unknownPlace && destination) ||
            place ||
            timeFilter
        );
        return result;
    },
    (outcomes, { store, chat } = {}) => {
        const context = chat && chat.session ? chat.session : {};
        const {
            unknownPlace,
            origin,
            destination,
            timeFilter
        } = extractEntities(outcomes);
        const unknownPlaceMeta = getEntityMeta(unknownPlace);
        let nextContext = Object.assign({}, context);
        if (timeFilter) {
            nextContext.timeFilter = timeFilter;
        }
        if (origin) {
            // console.log('1 origin', origin);
            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 (unknownPlace && destination) {
                // console.log('5 not origin, destination and unknowPlace');
                nextContext.origin = unknownPlace.value;
                nextContext.originMeta = unknownPlaceMeta;
            }
            if (unknownPlace && context.destination && !context.origin) {
                // console.log('6 not origin, unknowPlace and the context has only destination');
                nextContext.origin = unknownPlace.value;
                nextContext.originMeta = unknownPlaceMeta;
            }
        }
        if (destination) {
            // console.log('7 destination', destination);
            nextContext.destination = destination.value;
            nextContext.destinationMeta = getEntityMeta(destination);
        } else {
            // console.log('8 not destination, unknowPlace and the context has only destination');
            if (
                (unknownPlace && !context.destination && context.origin) ||
                (unknownPlace && !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;
            }
        }
        if (store) {
            store.dispatch(updateChatSession({
                chat: { ...chat, session: nextContext }
            }));
        }
        return tripDialogReply(nextContext);
    }
]];<commit_msg>consider confidence threshold on the route match and on the statements without trip intent as well, fix #14<commit_after>import { updateChatSession } from '../actionCreators';
import { tripDialogReply } from '../tripDialog';
import { extractEntities } from '../fuzzy';
import { WitDriver } from 'calamars';
const { getEntityValue, getEntityMeta } = WitDriver;

const placesConfidenceThreshold = 0.77;

const checkConfidences = ({
    origin,
    destination,
    unknownPlace
}) => {
    const hasDestination = (destination && destination.confidence >= placesConfidenceThreshold);
    const hasOrigin = (origin && origin.confidence >= placesConfidenceThreshold);
    const hasUnknownPlace = (
        unknownPlace && unknownPlace.confidence >= placesConfidenceThreshold
    );
    return { hasDestination, hasOrigin, hasUnknownPlace };
};

const routes = [[
    outcomes => {
        const hasTripIntent = getEntityValue(outcomes, 'trip') === 'info';
        const { origin, destination, unknownPlace } = extractEntities(outcomes);
        const {
            hasDestination, hasOrigin, hasUnknownPlace
        } = checkConfidences({
            origin, destination, unknownPlace
        });
        return hasTripIntent && (
            hasDestination ||
            hasOrigin ||
            hasUnknownPlace
        );
    },
    (outcomes, { store, chat } = {}) => {
        const {
            origin,
            origins,
            originMeta,
            destination,
            destinationMeta,
            unknownPlace,
            unknownPlaces,
            timeFilter
        } = extractEntities(outcomes);
        const context = chat && chat.session ? chat.session : {};
        let nextContext = Object.assign({}, context);
        if (timeFilter) {
            nextContext.timeFilter = timeFilter;
        }
        if (destination && destination.confidence >= placesConfidenceThreshold) {
            nextContext.destination = destination.value;
            nextContext.destinationMeta = destinationMeta;
        }
        if (origin && origin.confidence >= placesConfidenceThreshold) {
            nextContext.origin = origin.value;
            nextContext.originMeta = originMeta;
            if (!destination) {
                if (
                    origins && origins.length > 1 &&
                    origins[1].confidence >= placesConfidenceThreshold
                ) {
                    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(updateChatSession({
                chat: { ...chat, session: nextContext }
            }));
        }
        return tripDialogReply(nextContext);
    }
], [
    outcomes => {
        const {
            unknownPlace,
            origin,
            destination,
            timeFilter
        } = extractEntities(outcomes);
        const {
            hasDestination, hasOrigin, hasUnknownPlace
        } = checkConfidences({
            origin, destination, unknownPlace
        });

        const result = (
            hasDestination ||
            hasOrigin ||
            hasUnknownPlace ||
            timeFilter
        );
        return result;
    },
    (outcomes, { store, chat } = {}) => {
        const context = chat && chat.session ? chat.session : {};
        const {
            unknownPlace,
            origin,
            destination,
            timeFilter
        } = extractEntities(outcomes);
        const {
            hasDestination, hasOrigin, hasUnknownPlace
        } = checkConfidences({
            origin, destination, unknownPlace
        });
        const unknownPlaceMeta = getEntityMeta(unknownPlace);
        let nextContext = Object.assign({}, context);
        if (timeFilter) {
            nextContext.timeFilter = timeFilter;
        }
        if (hasOrigin) {
            // console.log('1 origin', origin);
            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(updateChatSession({
                chat: { ...chat, session: nextContext }
            }));
        }
        return tripDialogReply(nextContext);
    }
]];
{'fraction_non_alphanumeric': 0.10240605970592603, 'fraction_numerical': 0.004232882815980989, 'mean_word_length': 1.8477479382533306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3784777', 'n_tokens_mistral': 3183, 'n_tokens_neox': 3119, 'n_words': 691}