def word_Rep(text,Dic_Word):
    rc = re.compile('|'.join(map(re.escape, Dic_Word)))
    def translate(match):
                return Dic_Word[match.group()]
        return rc.sub(translate, text)
Dic_Word= {
    #A Words
    'a':'ein',
    'an':'eine',
    'able':'KOmmen',
    'about':'gegen',
    'above':'Uber',
    'absence':'Abwesenheit',
    'absent':'abwesend',
    'accent':'Betonung',
    'accept':'akzeptieren',
    'according':'nach',
    'acquainted':'kennen',
    'across':'uber'
}
Text_Trans= raw_input("[*]ENTER THE TEXT TO TRANSLATE:")
Trans=word_Rep(Text_Trans,Dic_Word)
print Trans

please refer the above code. What happens here is that ,if a user gives the input in the command prompt as Input: a an able aunt. The output should be ein eine kommen Tante. But what I am getting is ein einn einble einunt and so on , the 'a' is getting replaced where ever the 'a' is occuring, Any help ?

It is replacing every 'a" with "ein". A tutorial on using a dictionary to map from English to Spanish

## CamelCase is reserved for classes in Python
dic_word= {
    'a':'ein',
    'an':'eine',
    'able':'KOmmen',
    'about':'gegen',
    'above':'Uber',
    'absence':'Abwesenheit',
    'absent':'abwesend',
    'accent':'Betonung',
    'accept':'akzeptieren',
    'according':'nach',
    'acquainted':'kennen',
    'across':'uber'
}

for word in ["a", "able", "acquainted"]:
    print word, "-->", dic_word[word]

@woooee
I am trying to create a translator which translates text from "English" to "German" , so i'd used a dictionary and coded all the words from a English-German Dictionary. Not only for the word 'a' for some other words like 'good' is replaced with the meaning of go, that is gegen+od,'go+od'. Simillarly for the word 'teacher' it gets replaced with meaning of'tea',and 'cher' is added to the end ((i.e) 'tee+cher'). Is there anyway to fix it? any ideas?

You can try to sort the words by decreasing length in the regex

L = sorted(Dic_Word, key = len, reverse = True)
rc = re.compile('|'.join(map(re.escape, L)))

@Gribouillis
Thanks for your help I'd tried it but no luck getting the following error message from my python compiler

TypeError : 'Key' is an invalid keyword argument for this function.

What could be possibly wrong with this function?

key should be in lower case.

Another approach ...

''' re_sub_translate2.py
a simple and rough English to German translator

specifically replace whole words only
will replace I but not change Italy
note that this replacement is case sensitive
attached quotes and punctuation marks are neutral
'''

import re

def re_replacer(match_obj):
    '''
    this function will be called for each key_word
    in the text during substitution/translation
    '''
    word = match_obj.group(0)
    return replace_dict.get(word, word)

eng_text = "Karl and I will travel by car to Italy."

# create a dictionary of key_word:replace_with pairs
replace_dict = {
'and' : 'und',
'by' : 'mit',
'car' : 'Auto',
'I' : 'ich',
'Italy' : 'Italien',
'to' : 'nach',
'travel' : 'reisen',
'will' : 'werden'
}

# a regular expression matching all identifiers
pattern = re.compile(r"[A-Za-z_]\w*")

ger_text = pattern.sub(re_replacer, eng_text)

# show result
print("English text ...")
print(eng_text)
print('-'*50)
print("German text ...")
print(ger_text)

''' result -->
English text ...
Karl and I will travel by car to Italy.
--------------------------------------------------
German text ...
Karl und ich werden reisen mit Auto nach Italien.
'''
def word_replace(Match_obj):
    word= Match_obj.group(0)
    return dic_word.get(word,word)
pattern = re.compile(r"[A-Za-z_]\w*")
Trans = pattern.sub(dic_word,c)
con.write(Trans)
f.close
con.close

I had tried the above code and when in run full code I am getting the following error message

Traceback (most recent call last):
  File "file2.py", line 2120, in <module>
    Trans = pattern.sub(dic_word,c)
  File "c:\python27\lib\re.py", line 270, in _subx
    template = _compile_repl(template, pattern)
  File "c:\python27\lib\re.py", line 250, in _compile_repl
    p = _cache_repl.get(key)
TypeError: unhashable type: 'dict'

Can anyone please help me here?

Please refer to the line no. 35, I had made the following change to that line something like the following, but could not translate capital words for example,the English word write means schreiben in German. Assume that my document contains Write it is not translating it. So for that i'd used re.IGNORECASE to translate, but not working.

pattern = re.compile(r"[A-Za-z_]\w*",re.IGNORECASE)

Where am I going wrong please help me guys.

This might work for you ...

''' re_sub_translate3.py
a simple and rough English to German translator

specifically replace whole words only
will replace I but not change Italy
attached quotes and punctuation marks are neutral
modified to handle capitalized words
'''

import re

def re_replacer(match_obj):
    '''
    this function will be called for each key_word
    in the text during substitution/translation
    tries to handle capitalized words
    '''
    word = match_obj.group(0)
    try:
        return replace_dict[word]
    except KeyError:
        try:
            print(word.lower())  #test
            return replace_dict[word.lower()]
        except KeyError:
            return replace_dict.get(word, word)

# note capitalized word 'BY'
eng_text = "Karl and I will travel BY car to Italy."

# create a dictionary of key_word:replace_with pairs
replace_dict = {
'and' : 'und',
'by' : 'mit',
'car' : 'Auto',
'I' : 'ich',
'Italy' : 'Italien',
'to' : 'nach',
'travel' : 'reisen',
'will' : 'werden'
}

# a regular expression matching all identifiers
pattern = re.compile(r"[A-Za-z]\w*")

# optional re.IGNORECASE does not work
#pattern = re.compile(r"[A-Za-z]\w*", re.IGNORECASE)

ger_text = pattern.sub(re_replacer, eng_text)

# show result
print("English text ...")
print(eng_text)
print('-'*50)
print("German text ...")
print(ger_text)

''' result -->
English text ...
Karl and I will travel BY car to Italy.
--------------------------------------------------
German text ...
Karl und ich werden reisen mit Auto nach Italien.
'''

@vegaseat
It's working.

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.