For a given list of (base, inflection)-tuples, returns a list of (count, inflected suffix, [(base suffix, frequency), ... ]): suffixes([("beau", "beaux"), ("jeune", "jeunes"), ("hautain", "hautaines")], n=3) => [(2, "nes", [("ne", 0.5), ("n", 0.5)]), (1, "aux", [("au", 1.0)]
(inflections=[], n=3, top=10, reverse=True)
| 415 | #--- WORD INFLECTION ------------------------------------------------------------------------------- |
| 416 | |
| 417 | def suffixes(inflections=[], n=3, top=10, reverse=True): |
| 418 | """ For a given list of (base, inflection)-tuples, |
| 419 | returns a list of (count, inflected suffix, [(base suffix, frequency), ... ]): |
| 420 | suffixes([("beau", "beaux"), ("jeune", "jeunes"), ("hautain", "hautaines")], n=3) => |
| 421 | [(2, "nes", [("ne", 0.5), ("n", 0.5)]), (1, "aux", [("au", 1.0)])] |
| 422 | """ |
| 423 | # This is utility function we use to train singularize() and lemma() |
| 424 | # in pattern.de, pattern.es, pattern.fr, etc. |
| 425 | d = {} |
| 426 | for x, y in (reverse and (y, x) or (x, y) for x, y in inflections): |
| 427 | x0 = x[:-n] # be- jeu- hautai- |
| 428 | x1 = x[-n:] # -aux -nes -nes |
| 429 | y1 = y[len(x0):] # -au -ne -n |
| 430 | if x0 + y1 != y: |
| 431 | continue |
| 432 | if x1 not in d: |
| 433 | d[x1] = {} |
| 434 | if y1 not in d[x1]: |
| 435 | d[x1][y1] = 0.0 |
| 436 | d[x1][y1] += 1.0 |
| 437 | # Sort by frequency of inflected suffix: 2x -nes, 1x -aux. |
| 438 | # Sort by frequency of base suffixes for each inflection: |
| 439 | # [(2, "nes", [("ne", 0.5), ("n", 0.5)]), (1, "aux", [("au", 1.0)])] |
| 440 | d = [(int(sum(y.values())), x, y.items()) for x, y in d.items()] |
| 441 | d = sorted(d, reverse=True) |
| 442 | d = ((n, x, (sorted(y, key=itemgetter(1)))) for n, x, y in d) |
| 443 | d = ((n, x, [(y, m / n) for y, m in y]) for n, x, y in d) |
| 444 | return list(d)[:top] |
| 445 | |
| 446 | #--- WORD CO-OCCURRENCE ---------------------------------------------------------------------------- |
| 447 |