How to use @microsoft/recognizers-text-date-time - 10 common examples

To help you get started, we’ve selected a few @microsoft/recognizers-text-date-time examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / BotBuilder-Samples / samples / javascript_nodejs / 40.timex-resolution / ranges.js View on Github external
module.exports.timeRange = () => {
    // Run the recognizer.
    const result = Recognizers.recognizeDateTime('Some time between 6pm and 6:30pm.', Recognizers.Culture.English);

    // We should find a single result in this example.
    result.forEach(result => {
        // The resolution includes a single value because there is no ambiguity.

        // We are interested in the distinct set of TIMEX expressions.
        const distinctTimexExpressions = new Set(
            result.resolution.values
                .filter(({ timex }) => timex !== undefined)
                .map(({ timex }) => timex)
        );

        // The TIMEX expression captures date ambiguity so there will be a single distinct expression for each result.
        console.log(`${ result.text } ( ${ Array.from(distinctTimexExpressions).join(',') } )`);
    });
};
github microsoft / BotBuilder-Samples / samples / javascript_nodejs / 40.timex-resolution / ambiguity.js View on Github external
module.exports.dateTimeAmbiguity = () => {
    // Run the recognizer.
    const result = Recognizers.recognizeDateTime("It will be ready Wednesday at 5 o'clock.", Recognizers.Culture.English);

    // We should find two results in this example.
    result.forEach((result) => {
        // The resolution includes four example values: backwards and forward in the calendar and then AM and PM.

        // Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
        // We are interested in the distinct set of TIMEX expressions.
        const distinctTimexExpressions = new Set(
            result.resolution.values
                .filter(({ timex }) => timex !== undefined)
                .map(({ timex }) => timex)
        );

        // TIMEX expressions don't capture time ambiguity so there will be two distinct expressions for each result.
        console.log(`${ result.text } ( ${ Array.from(distinctTimexExpressions).join(',') } )`);
    });
github microsoft / BotBuilder-Samples / samples / javascript_nodejs / 40.timex-resolution / ranges.js View on Github external
module.exports.dateRange = () => {
    // Run the recognizer.
    const result = Recognizers.recognizeDateTime('Some time in the next two weeks.', Recognizers.Culture.English);

    // We should find a single result in this example.
    result.forEach(result => {
        // The resolution includes a single value because there is no ambiguity.

        // We are interested in the distinct set of TIMEX expressions.
        const distinctTimexExpressions = new Set(
            result.resolution.values
                .filter(({ timex }) => timex !== undefined)
                .map(({ timex }) => timex)
        );

        // The TIMEX expression captures date ambiguity so there will be a single distinct expression for each result.
        console.log(`${ result.text } ( ${ Array.from(distinctTimexExpressions).join(',') } )`);
    });
};
github microsoft / BotBuilder-Samples / samples / javascript_nodejs / 40.timex-resolution / ambiguity.js View on Github external
module.exports.dateAmbiguity = () => {
    // Run the recognizer.
    const result = Recognizers.recognizeDateTime('Either Saturday or Sunday would work.', Recognizers.Culture.English);

    // We should find two results in this example.
    result.forEach(result => {
        // The resolution includes two example values: going backwards and forwards from NOW in the calendar.

        // Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
        // We are interested in the distinct set of TIMEX expressions.

        // There is also either a "value" property on each value or "start" and "end".
        // If you use ToString() on a TimeProperty object you will get same "value".
        const distinctTimexExpressions = new Set(
            result.resolution.values
                .filter(({ timex }) => timex !== undefined)
                .map(({ timex }) => timex)
        );
github microsoft / BotBuilder-Samples / samples / javascript_nodejs / 40.timex-resolution / ambiguity.js View on Github external
module.exports.timeAmbiguity = () => {
    // Run the recognizer.
    const result = Recognizers.recognizeDateTime('We would like to arrive at 4 o\'clock or 5 o\'clock.', Recognizers.Culture.English);

    // We should find two results in this example.
    result.forEach(result => {
        // The resolution includes two example values: one for AM and one for PM.

        // Each result includes a TIMEX expression that captures the inherent date but not time ambiguity.
        // We are interested in the distinct set of TIMEX expressions.

        // There is also either a "value" property on each value or "start" and "end".
        // If you use ToString() on a TimeProperty object you will get same "value".
        const distinctTimexExpressions = new Set(
            result.resolution.values
                .filter(({ timex }) => timex !== undefined)
                .map(({ timex }) => timex)
        );
github microsoft / botbuilder-js / libraries / botbuilder-ai / src / localeConverter.ts View on Github external
private extractDates(message: string, fromLocale: string): TextAndDateTime[] {
        // let fndDates: string[];
        let culture: string = DateTimeRecognizers.Culture.English;
        if (fromLocale.startsWith('fr')) {
            culture = DateTimeRecognizers.Culture.French;
        } else if (fromLocale.startsWith('pt'))  {
            culture = DateTimeRecognizers.Culture.Portuguese;
        } else if (fromLocale.startsWith('zh'))  {
            culture = DateTimeRecognizers.Culture.Chinese;
        } else if (fromLocale.startsWith('es')) {
            culture = DateTimeRecognizers.Culture.Spanish;
        } else if (!fromLocale.startsWith('en')) {
            throw new Error('Unsupported from locale');
        }

        const model: DateTimeRecognizers.IDateTimeModel = new DateTimeRecognizers.DateTimeRecognizer(culture).getDateTimeModel();
        const results: ModelResult[] = model.parse(message);

        const foundDates: TextAndDateTime[] = [];
        results.forEach((result: ModelResult) => {
            let curDateTimeText: TextAndDateTime;
            let momentTime: Date;
            let momentTimeEnd: Date;
            let foundType: string;
            const resolutionValues: any = result.resolution.values[0];
github microsoft / botbuilder-js / libraries / botbuilder-ai / src / localeConverter.ts View on Github external
private extractDates(message: string, fromLocale: string): TextAndDateTime[] {
        // let fndDates: string[];
        let culture: string = DateTimeRecognizers.Culture.English;
        if (fromLocale.startsWith('fr')) {
            culture = DateTimeRecognizers.Culture.French;
        } else if (fromLocale.startsWith('pt'))  {
            culture = DateTimeRecognizers.Culture.Portuguese;
        } else if (fromLocale.startsWith('zh'))  {
            culture = DateTimeRecognizers.Culture.Chinese;
        } else if (fromLocale.startsWith('es')) {
            culture = DateTimeRecognizers.Culture.Spanish;
        } else if (!fromLocale.startsWith('en')) {
            throw new Error('Unsupported from locale');
        }

        const model: DateTimeRecognizers.IDateTimeModel = new DateTimeRecognizers.DateTimeRecognizer(culture).getDateTimeModel();
        const results: ModelResult[] = model.parse(message);

        const foundDates: TextAndDateTime[] = [];
github microsoft / botbuilder-js / libraries / botbuilder-ai / src / localeConverter.ts View on Github external
private extractDates(message: string, fromLocale: string): TextAndDateTime[] {
        // let fndDates: string[];
        let culture: string = DateTimeRecognizers.Culture.English;
        if (fromLocale.startsWith('fr')) {
            culture = DateTimeRecognizers.Culture.French;
        } else if (fromLocale.startsWith('pt'))  {
            culture = DateTimeRecognizers.Culture.Portuguese;
        } else if (fromLocale.startsWith('zh'))  {
            culture = DateTimeRecognizers.Culture.Chinese;
        } else if (fromLocale.startsWith('es')) {
            culture = DateTimeRecognizers.Culture.Spanish;
        } else if (!fromLocale.startsWith('en')) {
            throw new Error('Unsupported from locale');
        }

        const model: DateTimeRecognizers.IDateTimeModel = new DateTimeRecognizers.DateTimeRecognizer(culture).getDateTimeModel();
        const results: ModelResult[] = model.parse(message);

        const foundDates: TextAndDateTime[] = [];
        results.forEach((result: ModelResult) => {
            let curDateTimeText: TextAndDateTime;
            let momentTime: Date;
            let momentTimeEnd: Date;
            let foundType: string;
            const resolutionValues: any = result.resolution.values[0];
            const type: string = result.typeName.replace('datetimeV2.', '');
            if (type.includes('range')) {
                if (type.includes('date') && type.includes('time')) {
                    momentTime = moment(resolutionValues.start).toDate();
                    momentTimeEnd = moment(resolutionValues.end).toDate();
                    foundType = 'datetime';
                } else if (type.includes('date')) {
github microsoft / botbuilder-js / libraries / botbuilder-dialogs / src / prompts / datetimePrompt.ts View on Github external
protected async onRecognize(
        context: TurnContext,
        state: any,
        options: PromptOptions
    ): Promise> {
        const result: PromptRecognizerResult = { succeeded: false };
        const activity: Activity = context.activity;
        const utterance: string = activity.text;
        const locale: string =  activity.locale || this.defaultLocale || 'en-us';
        const results: any[] = Recognizers.recognizeDateTime(utterance, locale);
        if (results.length > 0 && results[0].resolution) {
            result.succeeded = true;
            result.value = results[0].resolution.values;
        }

        return result;
    }
}
github microsoft / botbuilder-js / libraries / botbuilder-prompts / src / datetimePrompt.ts View on Github external
recognize: function recognize(context) {
            const request = context.activity || {};
            const utterance = request.text || '';
            const locale =  request.locale || defaultLocale || 'en-us';
            const results = Recognizers.recognizeDateTime(utterance, locale);
            const values = results.length > 0 && results[0].resolution ? results[0].resolution.values : undefined;
            return Promise.resolve(validator ? validator(context, values) : values as any);
        }
    };

@microsoft/recognizers-text-date-time

recognizers-text provides robust recognition and resolution of date/time expressed in multiple languages.

MIT
Latest version published 9 months ago

Package Health Score

79 / 100
Full package analysis