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

To help you get started, we’ve selected a few @microsoft/recognizers-text-number 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 / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseMerged.ts View on Github external
private hasTokenIndex(source: string, regex: RegExp): { matched: boolean, index: number } {
        // This part is different from C# because no Regex RightToLeft option in JS
        let result = { matched: false, index: -1 };
        let matchResult = RegExpUtility.getMatches(regex, source);
        let match = matchResult[matchResult.length - 1];
        if (match && !source.substr(match.index + match.length).trim().length) {
            result.matched = true;
            result.index = match.index;
        }
        return result;
    }
}
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
private parseBasicRegexMatch(text: string, referenceTime: Date): DateTimeResolutionResult {
        let trimmedText = text.trim().toLowerCase();
        let offset = 0;

        let matches = RegExpUtility.getMatches(this.config.atRegex, trimmedText);
        if (matches.length === 0) {
            matches = RegExpUtility.getMatches(this.config.atRegex, this.config.timeTokenPrefix + trimmedText);
            offset = this.config.timeTokenPrefix.length;
        }

        if (matches.length > 0 && matches[0].index === offset && matches[0].length === trimmedText.length) {
            return this.match2Time(matches[0], referenceTime);
        }

        // parse hour pattern, like "twenty one", "16"
        // create a extract result which content the pass-in text
        let hour = this.config.numbers.get(text) || Number(text);
        if (hour) {
            if (hour >= 0 && hour <= 24) {
                let ret = new DateTimeResolutionResult();

                if (hour === 24) {
                    hour = 0;
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
else {
                min = Number.parseInt(minStr, 10);
                hasMin = true;
            }

            // get second
            let secStr = match.groups('sec').value.toLowerCase();
            if (!StringUtility.isNullOrWhitespace(secStr)) {
                second = Number.parseInt(secStr, 10);
                hasSec = true;
            }
        }

        // adjust by desc string
        let descStr = match.groups('desc').value.toLowerCase();
        if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
            || RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('iam').value)) {

            if (hour >= 12) {
                hour -= 12;
            }

            if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
                hasAm = true;
            }
        }
        else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {

            if (hour < 12) {
                hour += 12;
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
second = Number.parseInt(secStr, 10);
                hasSec = true;
            }
        }

        // adjust by desc string
        let descStr = match.groups('desc').value.toLowerCase();
        if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
            || RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('iam').value)) {

            if (hour >= 12) {
                hour -= 12;
            }

            if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
                hasAm = true;
            }
        }
        else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {

            if (hour < 12) {
                hour += 12;
            }

            hasPm = true;
        }

        // adjust min by prefix
        let timePrefix = match.groups('prefix').value.toLowerCase();
        if (!StringUtility.isNullOrWhitespace(timePrefix)) {
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
// adjust by desc string
        let descStr = match.groups('desc').value.toLowerCase();
        if (RegExpUtility.getMatches(this.config.utilityConfiguration.amDescRegex, descStr).length > 0
            || RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('iam').value)) {

            if (hour >= 12) {
                hour -= 12;
            }

            if (RegExpUtility.getMatches(this.config.utilityConfiguration.amPmDescRegex, descStr).length === 0) {
                hasAm = true;
            }
        }
        else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {

            if (hour < 12) {
                hour += 12;
            }

            hasPm = true;
        }

        // adjust min by prefix
        let timePrefix = match.groups('prefix').value.toLowerCase();
        if (!StringUtility.isNullOrWhitespace(timePrefix)) {
            let adjust = { hour: hour, min: min, hasMin: hasMin };
            this.config.adjustByPrefix(timePrefix, adjust);
            hour = adjust.hour; min = adjust.min; hasMin = adjust.hasMin;
        }
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
specialsRegexMatch(text: string, refDate: Date): Token[] {
        let ret = [];
        // handle "ish"
        if (this.config.ishRegex !== null) {
            let matches = RegExpUtility.getMatches(this.config.ishRegex, text);
            matches.forEach(match => {
                ret.push(new Token(match.index, match.index + match.length));
            });
        }
        return ret;
    }
}
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
return ret;
                    }
                }
            }

            // get minute
            let minStr = match.groups('min').value.toLowerCase();
            if (StringUtility.isNullOrWhitespace(minStr)) {
                minStr = match.groups('minnum').value;
                if (!StringUtility.isNullOrWhitespace(minStr)) {
                    min = this.config.numbers.get(minStr);
                    hasMin = true;
                }

                let tensStr = match.groups('tens').value;
                if (!StringUtility.isNullOrWhitespace(tensStr)) {
                    min += this.config.numbers.get(tensStr);
                    hasMin = true;
                }
            }
            else {
                min = Number.parseInt(minStr, 10);
                hasMin = true;
            }

            // get second
            let secStr = match.groups('sec').value.toLowerCase();
            if (!StringUtility.isNullOrWhitespace(secStr)) {
                second = Number.parseInt(secStr, 10);
                hasSec = true;
            }
        }
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
}
            else {
                hour = Number.parseInt(hourStr, 10);
                if (!hour) {
                    hour = this.config.numbers.get(hourStr);
                    if (!hour) {
                        return ret;
                    }
                }
            }

            // get minute
            let minStr = match.groups('min').value.toLowerCase();
            if (StringUtility.isNullOrWhitespace(minStr)) {
                minStr = match.groups('minnum').value;
                if (!StringUtility.isNullOrWhitespace(minStr)) {
                    min = this.config.numbers.get(minStr);
                    hasMin = true;
                }

                let tensStr = match.groups('tens').value;
                if (!StringUtility.isNullOrWhitespace(tensStr)) {
                    min += this.config.numbers.get(tensStr);
                    hasMin = true;
                }
            }
            else {
                min = Number.parseInt(minStr, 10);
                hasMin = true;
            }

            // get second
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
}
            else if (!StringUtility.isNullOrWhitespace(match.groups('midafternoon').value)) {
                hour = 14;
                min = 0;
                second = 0;
            }
            else if (!StringUtility.isNullOrWhitespace(match.groups('midday').value)) {
                hour = 12;
                min = 0;
                second = 0;
            }
        }
        else {
            // get hour
            let hourStr = match.groups('hour').value;
            if (StringUtility.isNullOrWhitespace(hourStr)) {
                hourStr = match.groups('hournum').value.toLowerCase();
                hour = this.config.numbers.get(hourStr);
                if (!hour) {
                    return ret;
                }
            }
            else {
                hour = Number.parseInt(hourStr, 10);
                if (!hour) {
                    hour = this.config.numbers.get(hourStr);
                    if (!hour) {
                        return ret;
                    }
                }
            }
github microsoft / Recognizers-Text / JavaScript / packages / recognizers-date-time / src / dateTime / baseTime.ts View on Github external
hasAm = true;
            }
        }
        else if (RegExpUtility.getMatches(this.config.utilityConfiguration.pmDescRegex, descStr).length > 0
            || !StringUtility.isNullOrEmpty(match.groups('ipm').value)) {

            if (hour < 12) {
                hour += 12;
            }

            hasPm = true;
        }

        // adjust min by prefix
        let timePrefix = match.groups('prefix').value.toLowerCase();
        if (!StringUtility.isNullOrWhitespace(timePrefix)) {
            let adjust = { hour: hour, min: min, hasMin: hasMin };
            this.config.adjustByPrefix(timePrefix, adjust);
            hour = adjust.hour; min = adjust.min; hasMin = adjust.hasMin;
        }

        // adjust hour by suffix
        let timeSuffix = match.groups('suffix').value.toLowerCase();
        if (!StringUtility.isNullOrWhitespace(timeSuffix)) {
            let adjust = { hour: hour, min: min, hasMin: hasMin, hasAm: hasAm, hasPm: hasPm };
            this.config.adjustBySuffix(timeSuffix, adjust);
            hour = adjust.hour; min = adjust.min; hasMin = adjust.hasMin; hasAm = adjust.hasAm; hasPm = adjust.hasPm;
        }

        if (hour === 24) {
            hour = 0;
        }

@microsoft/recognizers-text-number

recognizers-text-number provides robust recognition and resolution of numbers expressed in multiple languages.

MIT
Latest version published 1 year ago

Package Health Score

79 / 100
Full package analysis