Returns an approximation of the entire set. Identical words are grouped and counted and then quantified with an approximation.
(*args, **kwargs)
| 300 | # count([word1, word2, ...], plural={}) |
| 301 | # counr({word1:0, word2:0, ...}, plural={}) |
| 302 | def count(*args, **kwargs): |
| 303 | """ Returns an approximation of the entire set. |
| 304 | Identical words are grouped and counted and then quantified with an approximation. |
| 305 | """ |
| 306 | if len(args) == 2 and isinstance(args[0], basestring): |
| 307 | return approximate(args[0], args[1], kwargs.get("plural", {})) |
| 308 | if len(args) == 1 and isinstance(args[0], basestring) and "amount" in kwargs: |
| 309 | return approximate(args[0], kwargs["amount"], kwargs.get("plural", {})) |
| 310 | if len(args) == 1 and isinstance(args[0], dict): |
| 311 | count = args[0] |
| 312 | if len(args) == 1 and isinstance(args[0], (list, tuple)): |
| 313 | # Keep a count of each item in the list. |
| 314 | count = {} |
| 315 | for word in args[0]: |
| 316 | try: |
| 317 | count.setdefault(word, 0) |
| 318 | count[word] += 1 |
| 319 | except: |
| 320 | raise TypeError, "can't count %s, only str and unicode" % word.__class__.__name__ |
| 321 | # Create an iterator of (count, item) tuples, sorted highest-first. |
| 322 | s = [(count[word], word) for word in count] |
| 323 | s = max([n for (n,w) in s]) > 1 and reversed(sorted(s)) or s |
| 324 | # Concatenate approximate quantities of each item, |
| 325 | # starting with the one that has the highest occurence. |
| 326 | phrase = [] |
| 327 | for i, (n, word) in enumerate(s): |
| 328 | phrase.append(approximate(word, n, kwargs.get("plural", {}))) |
| 329 | phrase.append(i==len(count)-2 and " and " or ", ") |
| 330 | return "".join(phrase[:-1]) |
| 331 | |
| 332 | quantify = count |
| 333 |
no test coverage detected
searching dependent graphs…