Ejemplo n.º 1
0
    def number_spoken_casual(cls, n):
        """
        Split the number in groups of 2. Pronounce each one separately
        but say "oh 5" for the final group if it's less than 10.

        >>> EnglishUS.number_spoken_casual(302)
        "three oh two"
        >>> EnglishUS.number_spoken_casual(1267)
        "twelve sixty seven"

        """
        if n > 9999 or n % 1000 == 0:
            return cls.number_spoken_full(n)
        if n == 0:
            return 'zero'

        groups = split_num(n, 2)
        if len(groups) > 1 and groups[-1] == 0:
            s = "{} hundred".format(cls.lt_1000_spoken_full(groups[0]))
        elif len(groups) > 1 and groups[-1] < 10:
            s = "{} oh {}".format(
                cls.lt_1000_spoken_full(groups[0]),
                cls.lt_1000_spoken_full(groups[1])
            )
        else:
            s = " ".join([cls.lt_1000_spoken_full(g) for g in groups])
        return s
Ejemplo n.º 2
0
    def number_spoken_full(cls, n):
        """
        The basic algorithm is to split the number into groups
        of 3. Pronounce each group of 3 and then append the exponent
        part.

        So 9999 gets split into [9, 999]. The first group is
        "nine thousand". The second group is "nine hundred ninety nine"

        """
        if n > MAX_NUM:
            raise ValueError(
                "{} is too large. Largest number is {}".format(n, MAX_NUM)
            )
        if n == 0:
            return 'zero'

        parts = []
        for g, exp in zip(reversed(split_num(n, 3)), _exponents):
            s = cls.lt_1000_spoken_full(g)
            if s:
                if exp:
                    s += " " + exp
                parts.append(s)

        return ' '.join(reversed(parts))
Ejemplo n.º 3
0
    def number_spoken_casual(cls, n):
        """
        Split the number in groups of 2. Pronounce each one separately
        but say "oh 5" for the final group if it's less than 10.

        >>> EnglishUS.number_spoken_casual(302)
        "three oh two"
        >>> EnglishUS.number_spoken_casual(1267)
        "twelve sixty seven"

        """
        if n > 9999 or n % 1000 == 0:
            return cls.number_spoken_full(n)
        if n == 0:
            return 'zero'

        groups = split_num(n, 2)
        if len(groups) > 1 and groups[-1] == 0:
            s = "{} hundred".format(cls.lt_1000_spoken_full(groups[0]))
        elif len(groups) > 1 and groups[-1] < 10:
            s = "{} oh {}".format(cls.lt_1000_spoken_full(groups[0]),
                                  cls.lt_1000_spoken_full(groups[1]))
        else:
            s = " ".join([cls.lt_1000_spoken_full(g) for g in groups])
        return s
Ejemplo n.º 4
0
    def number_spoken_full(cls, n):
        """
        The basic algorithm is to split the number into groups
        of 3. Pronounce each group of 3 and then append the exponent
        part.

        So 9999 gets split into [9, 999]. The first group is
        "nine thousand". The second group is "nine hundred ninety nine"

        """
        if n > MAX_NUM:
            raise ValueError("{} is too large. Largest number is {}".format(
                n, MAX_NUM))
        if n == 0:
            return 'zero'

        parts = []
        for g, exp in zip(reversed(split_num(n, 3)), _exponents):
            s = cls.lt_1000_spoken_full(g)
            if s:
                if exp:
                    s += " " + exp
                parts.append(s)

        return ' '.join(reversed(parts))
Ejemplo n.º 5
0
def test_split():
    assert split_num(1234, 2) == [12, 34]
    assert split_num(123, 2) == [1, 23]
    assert split_num(123, 4) == [123]
    assert split_num(1234567, 4) == [123, 4567]
Ejemplo n.º 6
0
def test_split():
    assert split_num(1234, 2) == [12, 34]
    assert split_num(123, 2) == [1, 23]
    assert split_num(123, 4) == [123]
    assert split_num(1234567, 4) == [123, 4567]