File: 02-quantify.py

package info (click to toggle)
python-pattern 2.6%2Bgit20180818-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 93,888 kB
  • sloc: python: 28,119; xml: 15,085; makefile: 194
file content (42 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import print_function
from __future__ import unicode_literals

from builtins import str, bytes, dict, int

import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))

from pattern.en import number, numerals, quantify, reflect

# The number() command returns an int or float from a written representation.
# This is useful, for example, in combination with a parser
# to transform "CD" parts-of-speech to actual numbers.
# The algorithm ignores words that aren't recognized as numerals.
print(number("two thousand five hundred and eight"))
print(number("two point eighty-five"))
print("")

# The numerals() command returns a written representation from an int or float.
print(numerals(1.249, round=2))
print(numerals(1.249, round=3))
print("")

# The quantify() commands uses pluralization + approximation to enumerate words.
# This is useful to generate a human-readable summary of a set of strings.
print(quantify(["goose", "goose", "duck", "chicken", "chicken", "chicken"]))
print(quantify(["penguin", "polar bear"]))
print(quantify(["carrot"] * 1000))
print(quantify("parrot", amount=1000))
print(quantify({"carrot": 100, "parrot": 20}))
print("")

# The quantify() command only works with words (strings).
# To quantify a set of Python objects, use reflect().
# This will first create a human-readable name for each object and then quantify these.
print(reflect([0, 1, {}, False, reflect]))
print(reflect(os.path))
print(reflect([False, True], quantify=False))
print(quantify(
    ["bunny rabbit"] +
    reflect([False, True], quantify=False)))