tags:

views:

77

answers:

0

Hi!
I have 2 different dictionary programs that I want combined (or put together).
The first one has the possibility to get good translation with grammar etc.
Here is the code so far:

words = {('i',): 'jeg', ('read',): 'leste', ('the', 'book'): 'boka'}
max_group = len(max(words))
sentence = "I read the book".lower().split()
translation = []
position = 0

while sentence:
    for m in range(max_group - 1, -1, -1):
        piece = tuple(sentence[:position + m])
        if piece in words:
            translation.append(words[piece])
            sentence = sentence[position + m:]
    position += 1

print(' '.join(translation))

In program 2 you can write more than just one sentence at a time.
You will also get the first letter in a sentence capitalized.
The third thing is that you get the word that isn’t in the dictionary printed out.

Here is the code for program 2:

import re
words = {'i':'jeg','am':'er','happy':'glad'}

sentence =input('Write a sentence:')
sentence = sentence.split()
translation = []
for word in sentence:
    piece = re.sub('[^a-z0-9]', '', word.lower())
    punctuation = word[-1] if word[-1].lower() != piece[-1] else ''
    if piece in words:
        translation.append(words[piece] + punctuation)
    else:
        translation.append(word)
translation = ' '.join(translation).split('. ')
print('. '.join(s.capitalize() for s in translation))

So the question is: How could I use the best of each program and just make a good one?...

I appreciate every good effort…
Thanx in advance!