#quote from MIT 'introduction to computation and programming using python, Revised'
EtoF = {'bread':'pain', 'wine':'vin', 'with':'avec', 'I':'Je',
'eat':'manage', 'drink':'bois', 'John':'Jean',
'friends':'amis', 'and':'et', 'of':'du', 'red':'rouge'}
FtoE = {'pain':'bread', 'vin':'wine', 'avec':'with', 'Je':'I',
'mange':'eat', 'bois':'drink', 'Jean':'John',
'amis':'friends', 'et':'and', 'du':'of', 'rouge':'red'}
dicts = {'English to French':EtoF, 'French to English':FtoE}
def translateWord(word, dictionary):
if word in dictionary.keys():
return dictionary[word]
elif word != '':
return '"' + word + '"'
return word
def translate(phrase, dicts, direction):
UCLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LCLetters = 'abcdefghijklmnopqrstuvwxyz'
letters = UCLetters + LCLetters
dictionary = dicts[direction]
translation = ''
word = ''
for c in phrase:
if c in letters:
word += c
else:
translation += translateWord(word, dictionary) + c
word = ''
return translation + ' ' + translateWord(word, dictionary)
print translate('I drink good red wine, and eat bread.',
dicts, 'English to French')
print translate('Je bois du vin rouge.',
dicts, 'French to English')
translate, dictionary demo
最新推荐文章于 2024-03-03 23:52:02 发布