MCPcopy Index your code
hub / github.com/clips/pattern / approximate

Function approximate

pattern/text/en/inflect_quantify.py:247–281  ·  view source on GitHub ↗

Returns an approximation of the number of given objects. Two objects are described as being "a pair", smaller than eight is "several", smaller than twenty is "a number of", smaller than two hundred are "dozens", anything bigger is described as being tens or h

(word, amount=1, plural={})

Source from the content-addressed store, hash-verified

245quantify_custom_plurals = {}
246
247def approximate(word, amount=1, plural={}):
248 """ Returns an approximation of the number of given objects.
249 Two objects are described as being "a pair",
250 smaller than eight is "several",
251 smaller than twenty is "a number of",
252 smaller than two hundred are "dozens",
253 anything bigger is described as being tens or hundreds of thousands or millions.
254 For example: approximate("chicken", 100) => "dozens of chickens".
255 """
256 try: p = pluralize(word, custom=plural)
257 except:
258 raise TypeError, "can't pluralize %s, only str and unicode" % word.__class__.__name__
259 # Anything up to 200.
260 if amount == 0:
261 return "%s %s" % (NONE, p)
262 if amount == 1:
263 return referenced(word) # "a" chicken, "an" elephant
264 if amount == 2:
265 return "%s %s" % (PAIR, p)
266 if 3 <= amount < 8:
267 return "%s %s" % (SEVERAL, p)
268 if 8 <= amount < 18:
269 return "%s %s" % (NUMBER, p)
270 if 18 <= amount < 23:
271 return "%s %s" % (SCORE, p)
272 if 23 <= amount < 200:
273 return "%s %s" % (DOZENS, p)
274 if amount > 10000000:
275 return "%s %s" % (COUNTLESS, p)
276 # Hundreds and thousands.
277 thousands = int(log(amount, 10) / 3)
278 hundreds = ceil(log(amount, 10) % 3) - 1
279 h = hundreds==2 and "hundreds of " or (hundreds==1 and "tens of " or "")
280 t = thousands>0 and pluralize(ORDER[thousands])+" of " or ""
281 return "%s%s%s" % (h, t, p)
282
283#print approximate("chicken", 0)
284#print approximate("chicken", 1)

Callers 1

countFunction · 0.85

Calls 2

pluralizeFunction · 0.90
referencedFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…