|
-
Sep 18th, 2004, 01:10 PM
#1
Thread Starter
New Member
Letter Replacement Translation Code Help
I need help in creating the code for a program that I need to create. Put simply, I need the program to replace each letter/combination of letters with another letter/combination of letters that I specify in the coding, and be able to reverse the translation. I have a text box to input the text, a command button to activate the translation process, a Label to put the translation into, and two option buttons to specify which direction the translation is going (the translations won't be exactly the same due to exceptions in the translation).
The concept is the same for another translation program, found here:
Uryuomoco Translator
And below is the code for the same type of program in Python (not for the translator above, but still a Uryuomoco Translator). I don't understand Python or its coding, but it may help you in figuring out what I'm trying to do exactly.
Code:
#!/usr/bin/python
import sys
import getopt
def starts_with(dict, match, prefix):
result=[]
for k in dict:
if k.startswith(prefix):
result.append(k)
if k==prefix:
match=k
return (result, match)
# 0: u2e, 1: e2u
def doend(line, mode):
if mode==0:
if line.lower().endswith('eg'):
line=line[0:-2]+'(ing|eg)'
else:
if line.lower().endswith('of'):
line=line[0:-1]+'(f)'
else:
if mode==1:
if line.lower().endswith('yumt'):
line=line[0:-4]+'ot'
else:
if line.lower().endswith('e'):
line=line+'h'
return line
u2e= {'a':'u',
'b':'v',
'c':'s',
'd':'z',
'e':'o',
'f':'h',
'g':'t',
'h':'f',
'i':'y',
'j':'x',
'k':'p',
'l':'r',
'm':'n',
'n':'m',
'o':'e',
'p':'k',
'qu':'w',
'r':'l',
's':'c',
't':'g',
'u':'a',
'v':'b',
'w':'qu',
'x':'j',
'yu':'i',
'z':'d',
'ch':'(th|sf)',
'se':'(ch|co)',
'quo':'(wh|we)',
'tul':'(gr|gar)',
'ais':'ss',
'us':'(sh|ac)',
'ra':'(ll|lu)'}
e2u= {'a':'u',
'b':'v',
'c':'s',
'd':'z',
'e':'o',
'f':'h',
'g':'t',
'h':'f',
'i':'yu',
'j':'x',
'k':'p',
'l':'r',
'm':'n',
'n':'m',
'o':'e',
'p':'k',
'qu':'w',
'r':'l',
's':'c',
't':'g',
'u':'a',
'v':'b',
'w':'qu',
'x':'j',
'y':'i',
'z':'d',
'th':'ch',
'ch':'se',
'wh':'quo',
'gr':'tul',
'ss':'ais',
'll':'ra',
'sh':'us'}
try:
opts, args = getopt.getopt(sys.argv[1:],'',['u2e', 'e2u'])
if len(opts)>1:
raise getopt.GetoptError
if len(opts)<1:
mode=1
else:
if opts[0][0]=='--u2e':
mode=0
else:
mode=1
except getopt.GetoptError:
# print help information and exit:
usage()
sys.exit(2)
cipher=(u2e, e2u)[mode]
for line in sys.stdin:
outline=''
i=0;
while i<len(line):
cap=line[i].isupper()
c=line[i].lower()
if not c.isalpha():
outline=doend(outline, mode)
if not c=="\n":
outline+=c
i+=1
continue
dict=cipher.keys()
j=i
largest_match=''
while len(dict)>1 and len(line)>=j:
j+=1
old_dict=dict
(dict,largest_match)=starts_with(dict, largest_match, line[i:j].lower())
if len(dict)==1:
if len(dict[0])>0:
if line[i:i+len(dict[0])].lower() == dict[0]:
largest_match=dict[0]
if(len(largest_match)<1):
print "Invalid input ("+line[i:j]+") translation so far ("+outline+")"
sys.exit()
j=len(largest_match)+i
if cap:
outline += cipher[largest_match].capitalize()
else:
outline += cipher[largest_match]
i=j
doend(outline, mode)
print outline
As stated before, the direction of the translation would make a difference in how it comes out, so I need to be able to specify the direction with the option buttons.
Using Uryuomoco as an example, the English word "super" would be translated to "cakol", and translating it back will make it "super" again. Just basic letter replacement for that. It gets more complicated because there are single letters that are translated into multiple letters (I --> YU, QU --> W, etc.), and there are exceptions to the replacements. For instance, a direct translation of "LL" from english would make it "RR" in Uryuomoco, but it is actually translated to "RA". However, "RA" in Uryuomoco could be translated back to English in two different ways; following the normal translation, it would be "LU", but following the exceptions it would be "LL"; obviously, both would be possible.
My program would work in the same way (this is where I got the idea from, after all). The code would be very similar, besides the specific translations (which I would input myself).
I need help to do this program. I need it done before I can continue with my work; it would take too long to translate everything manually.
And no, I don't care about security, it's not for that purpose.
There is no emotion; there is peace.
There is no ignorance; there is knowledge.
There is no passion; there is serenity.
There is no death; there is the Force.
Peace over anger.
Honor over hate.
Strength over fear.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|