Some pythonic advice needed
andrew cooke
andrew at acooke.org
Sun Jun 1 13:48:29 EDT 2003
i think you need something like the following (although you need to
extend it with scores, questions, etc.)
note that in my examples the two languages here are "digits" and
"words". i use spanish and english just so that i have several
different words for the same value...!
andrew at tonto:~/src/python$ ./translations.py
translations of one are ['1', '1.0']
translations of 1 are ['one', 'un']
translations of two are ['2']
translations of 2 are ['two', 'dos']
known digits: ['1', '2', '1.0']
andrew
#!/usr/bin/python2.2
class MultiDict:
def __init__(self):
self.dict = {}
def set(self, key, value):
if not self.dict.has_key(key): self.dict[key] = []
self.dict[key].append(value)
def get(self, key):
return self.dict[key]
def keys(self):
return self.dict.keys()
class Translations:
def __init__(self, a, b):
self.a_name = a
self.b_name = b
self.aToB = MultiDict()
self.bToA = MultiDict()
def addPair(self, a, b):
self.aToB.set(a, b)
self.bToA.set(b, a)
def toA(self, b): return self.bToA.get(b)
def toB(self, a): return self.aToB.get(a)
def allA(self): return self.aToB.keys()
def allB(self): return self.bToA.keys()
t = Translations("digits", "names")
t.addPair("1", "one")
t.addPair("1", "un")
t.addPair("1.0", "one")
t.addPair("1.0", "un")
t.addPair("2", "two")
t.addPair("2", "dos")
print "translations of", "one", "are", t.toA("one")
print "translations of", "1", "are", t.toB("1")
print "translations of", "two", "are", t.toA("two")
print "translations of", "2", "are", t.toB("2")
print "known digits:", t.allA()
--
http://www.acooke.org
More information about the Python-list
mailing list