Hi!
I would like to make an ordinary dictionary from english to norwegian, but I have a problem.

I am using this sentence as an example:
"I read the book"

The problem isn't the first two words, but the last ones. :-/
The program should replace "the book" to "boka".

Here is the code so far:

import re
words = {'i':'jeg','read':'leste','the book':'boka'}
while True:
    sentence =input('Write something:')
    if sentence == 'quit':
        break
    sentence = sentence.split()
    result = []

    for word in sentence:
        word_mod = re.sub('[^a-z0-9]', '', word.lower())
        punctuation = word[-1] if word[-1].lower() != word_mod[-1] else ''
        if word_mod in words:
            result.append(words[word_mod] + punctuation)
        else:
            result.append(word)

    result = ' '.join(result).split('. ')
    print('. '.join(s.capitalize() for s in result))
print('See you!')

I appreciate all the help I can get very much. :icon_smile:
Thank you in advance!

You can try this

# python 3
import re
words = {'i':'jeg','read':'leste','book':'boka'}
articles = set(["the", "a"])
while True:
    sentence =input('Write something: ')
    if sentence == 'quit':
        break
    sentence = sentence.split()
    result = []
    art = []
    for word in sentence:
        word_mod = re.sub('[^a-z0-9]', '', word.lower())
        punctuation = word[-1] if word[-1].lower() != word_mod[-1] else ''
        if word_mod in articles:
            art.append(word_mod)
        elif word_mod in words:
            result.append(words[word_mod] + punctuation)
        else:
            result.append(word)

    result = ' '.join(result).split('. ')
    print('. '.join(s.capitalize() for s in result))
    if art:
        print("It seems that there are no articles in Norwegian, so I removed '{0}'.".format(", ".join(art)))
print('See you!')

Hi again!
Thank you very much for your quick reply.
The program is getting better thanks to you.
But I still need some improvements...

Let me use the same sentence as an example, and add a new one.
Eng:"I read the book"
Nor:"Jeg leste boka"
Sentence no.2
Eng:"I am reading a book"
Nor:" Jeg leser ei bok"

So the program should replace
"the book" to "boka"
"I am reading" to "Jeg leser"
"a book" to "ei bok"
I want the grammar to be right...

What I actually want is to replace "idiomatic expressions" and what
I described above into good norwegian...
Sometimes 2 english words could be 1 norwegian word, 3 english words could be 4 norwegians (idiomatic expr.) and so on.
I hope I made myself clear enough...

Is it possible to make several dictionarys?

dic01 = {'idiomatic expressions':'translation','english words':'norwegian transl.'}
dic02 = {'one word':'transl.','another word':'new transl.'}

First the program check dic01 to see if there is some words to translate.
Now the sentence could be a mixture of eng. and nor. words.
Then the program could check dic02 and replace the rest of the words.
Is it possible to do it like that?

I would appreciate a reply very much.
Thanks in advance!

I think you should base your traduction on words, but this allows you to write idiomatic expressions, because you can use python tuples for that. For example

dic01 = { ("i", "am", "reading") : ("jeg", "leser") }

then you can traverse the sentence and the dicts in different ways to check if the sentence contains an idiomatic expression.
You must know that there is a famous python software called nltk , or the "natural language toolkit" which contains probably many tools that you could use later. Since there is a learning curve, you can start with your dictionaries, and study nltk once you have obtained some results with your program.

Hi again!
Thank you very much for your quick reply!
I will try to implement your suggestion.
I guess I have to change some of the code then.

You will hear from me if I still have problems... :icon_smile:

Thanks again!

Hei fra norge.
This is an almost impossible task without a good libary.
I did a translation program a while ago where i use google translate API and wxpython as gui.
Its work fine,still only in norwegian.
Shall write gui fronend in englishs to.
Did this a fun projects.you can try it out Py-trans

Hei fra norge.
This is an almost impossible task without a good libary.
I did a translation program a while ago where i use google translate API and wxpython as gui.
Its work fine,still only in norwegian.
Shall write gui fronend in english,not so much work.
Did this a fun projects.you can try it out Py-trans

It's too bad that you don't distribute the source code, because I can't run your program in linux.
Google translations are often terrible, don't you think ?

I agree with you that writing a general translation tool starting from zero is almost an impossible task, but if you want to translate only certain sentences and expressions, it can be a good exercise to understand both python and the problems that arise in translation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.