Esempio n. 1
0
    def spell_time_absolute(self, time):
        """\
        Convert a time expression into words.

        :param time: The 12hr numerical time value in a string, e.g. '08:05:pm'
        :return: time string with all numerals written out as words
        """
        period = ""
        if len(time.split(':')) > 2:
            hours, mins, period = time.split(':')[0:3]
        elif len(time.split(':')) > 1:
            hours, mins, = time.split(':')[0:2]
        else:
            return time  # 'now' and similar

        hours = int(hours)
        mins = int(mins)
        # hack for TTS
        period = "P M" if period == "PM" else period
        period = "A M" if period == "AM" else period

        time_str = [word_for_number(hours)]  # always start with an hour
        if mins == 0:
            return ' '.join(time_str + [period])
        if mins < 9:
            time_str.append('o')  # support one-o-one am
        return ' '.join(time_str + [word_for_number(mins), period])
Esempio n. 2
0
    def __init__(self, *args, **kwargs):
        super(PTIENSLUPreprocessing, self).__init__(*args, **kwargs)

        num_norms = []
        for num in xrange(60):
            num_norms.append(([unicode(num)], word_for_number(num).split()))
        self.text_normalization_mapping += num_norms

        # map ordinal numbers for streets and avenues 1st - 200th
        ord_norms = []
        suffixes = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
        for ord in xrange(1, 200):
            suffix = suffixes[ord % 10] if ord < 10 or ord > 20 else 'th'
            ord_form = str(ord) + suffix
            ord_norms.append(
                ([unicode(ord_form)], word_for_number(ord,
                                                      ordinary=True).split()))
        self.text_normalization_mapping += ord_norms

        self.text_normalization_mapping += [
            (["i'm"], ['i am']),
            (["im"], ['i am']),
            (["it'll"], ['it will']),
            (["i'll"], ['i will']),
            (["that's"], ['that is']),
            (["don't"], ['do not']),
            (["doesn't"], ['does not']),
        ]
Esempio n. 3
0
    def __init__(self, *args, **kwargs):
        super(PTIENSLUPreprocessing, self).__init__(*args, **kwargs)

        num_norms = []
        for num in xrange(60):
            num_norms.append(([unicode(num)], word_for_number(num).split()))
        self.text_normalization_mapping += num_norms

        # map ordinal numbers for streets and avenues 1st - 200th
        ord_norms = []
        suffixes = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
        for ord in xrange(1, 200):
            suffix = suffixes[ord % 10] if ord < 10 or ord > 20 else 'th'
            ord_form = str(ord) + suffix
            ord_norms.append(([unicode(ord_form)], word_for_number(ord, ordinary=True).split()))
        self.text_normalization_mapping += ord_norms

        self.text_normalization_mapping += [
            (["i'm"], ['i am']),
            (["im"], ['i am']),
            (["it'll"], ['it will']),
            (["i'll"], ['i will']),
            (["that's"], ['that is']),
            (["don't"], ['do not']),
            (["doesn't"], ['does not']),
        ]
Esempio n. 4
0
    def spell_time_relative(self, time):
        """\
        Convert a time expression into words.

        :param time: Numerical time value in a string, e.g. '8:05'

        :return: time string with all numerals written out as words 0:15 will generate '15 minutes' and not '0 hours and \
                15 minutes'.
        """
        if ':' not in time:  # 'now' and similar
            return time
        hours, mins = map(int, time.split(':'))

        time_str = []
        if hours is not 0:
            hr_id = 'hour' if hours is 1 else 'hours'
            hours = word_for_number(hours)
            time_str.extend((hours, hr_id))
        if mins == 0 and hours != 0:
            return ' '.join(time_str)
        if time_str:
            time_str.append('and')
        min_id = 'minute' if mins == 1 else 'minutes'
        mins = word_for_number(mins)
        return ' '.join(time_str + [mins, min_id])
Esempio n. 5
0
    def spell_time_absolute(self, time):
        """\
        Convert a time expression into words.

        :param time: The 12hr numerical time value in a string, e.g. '08:05:pm'
        :return: time string with all numerals written out as words
        """
        period = ""
        if len(time.split(':')) > 2:
            hours, mins, period = time.split(':')[0:3]
        elif len(time.split(':')) > 1:
            hours, mins, = time.split(':')[0:2]
        else:
            return time  # 'now' and similar

        hours = int(hours)
        mins = int(mins)
        # hack for TTS
        period = "P M" if period == "PM" else period
        period = "A M" if period == "AM" else period

        time_str = [word_for_number(hours)] # always start with an hour
        if mins == 0:
            return ' '.join(time_str + [period])
        if mins < 9:
            time_str.append('o') # support one-o-one am
        return ' '.join(time_str + [word_for_number(mins), period])
Esempio n. 6
0
    def spell_time_relative(self, time):
        """\
        Convert a time expression into words.

        :param time: Numerical time value in a string, e.g. '8:05'

        :return: time string with all numerals written out as words 0:15 will generate '15 minutes' and not '0 hours and \
                15 minutes'.
        """
        if ':' not in time:  # 'now' and similar
            return time
        hours, mins = map(int, time.split(':'))

        time_str = []
        if hours is not 0:
            hr_id = 'hour' if hours is 1 else 'hours'
            hours = word_for_number(hours)
            time_str.extend((hours, hr_id))
        if mins == 0 and hours != 0:
            return ' '.join(time_str)
        if time_str:
            time_str.append('and')
        min_id = 'minute' if mins == 1 else 'minutes'
        mins = word_for_number(mins)
        return ' '.join(time_str + [mins, min_id])
Esempio n. 7
0
    def spell_temperature(self, value, interval):
        """Convert a temperature expression into words (assuming nominative).

        :param value: Temperature value (whole number in degrees as string), \
                e.g. '1' or '-10'.
        :param interval: Boolean indicating whether to treat this as a start \
                of an interval, i.e. omit the degrees word.
        :return: temperature expression as string
        """
        ret = ''
        value = int(value)
        if value < 0:
            ret += 'minus '
            value = abs(value)
        ret += word_for_number(value)
        if not interval:
            ret += ' degrees'
        return ret
Esempio n. 8
0
    def spell_temperature(self, value, interval):
        """Convert a temperature expression into words (assuming nominative).

        :param value: Temperature value (whole number in degrees as string), \
                e.g. '1' or '-10'.
        :param interval: Boolean indicating whether to treat this as a start \
                of an interval, i.e. omit the degrees word.
        :return: temperature expression as string
        """
        ret = ''
        value = int(value)
        if value < 0:
            ret += 'minus '
            value = abs(value)
        ret += word_for_number(value)
        if not interval:
            ret += ' degrees'
        return ret
Esempio n. 9
0
def spell_number_range(start=0, end=10):
    spelled = []
    while end < start:
        spelled.append(word_for_number(start))
        start += 1
    return spelled
Esempio n. 10
0
def spell_number_range(start=0, end=10):
    spelled = []
    while end < start:
        spelled.append(word_for_number(start))
        start += 1
    return spelled