Big update to 0.1.0. Improved error handling, syncing, the works...
[hermes] / package / src / names.py
1 import trans
2
3 """Utilities for determing name variants.
4
5    Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
6    Released under the Artistic Licence."""
7
8 __names__ = [
9     ['Andrew', 'Andy', 'Andi', 'Drew'],
10     ['Christian', 'Chris'],
11     ['Christopher', 'Chris'],
12     ['David', 'Dave'],
13     ['Daniel', 'Dan', 'Danny'],
14     ['Michael', 'Mike', 'Mic', 'Mik', 'Micky'],
15     ['Peter', 'Pete'],
16     ['Robert', 'Rob', 'Bob', 'Bobby', 'Robbie'],
17   ]
18
19 __map__ = {}
20 for row in __names__:
21   for name in row:
22     if (not name in __map__):
23       __map__[name] = set(row)
24     else:
25       __map__[name] = __map__[name].union(row)
26
27 # -----------------------------------------------------------------------
28 def variants(name):
29     """Return a set of names which should be checked for given the input
30        name. Any word which is has a replacement will be replaced, and an
31        iterable list of all variants will be returned."""
32
33     result = set()
34     if (name is None):
35       return result
36     
37     name = unicode(name).encode('trans')
38     result.add(name)
39     bits = name.split(' ')
40     for bit in bits:
41       if (bit in __map__):
42         for replacement in __map__[bit]:
43           result.add(name.replace(bit, replacement))
44
45     return result
46