Results 1 to 9 of 9

Thread: Letter Replacement Translation Code Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    2

    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.

  2. #2
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068
    Use the Replace function.
    VB Code:
    1. Label1.Caption = Text1.Text
    2.   Label1.Caption = Replace(Label1.Caption, "a", "u")
    3.   Label1.Caption = Replace(Label1.Caption, "b", "v")
    And so on.....

  3. #3
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Copy string to byte array, then make another byte array in which you copy the new data as you read the other byte string.

    Basically:

    VB Code:
    1. Dim InBuffer() As Byte, OutBuffer() As Byte
    2. Dim KeyFrom() As Byte, KeyTo() As Byte
    3. Dim A As Long
    4. Const TESTSTRING As String = "I LIKE PEOPLE"
    5. Const TESTFROM As String = "ILKEPO"
    6. Const TESTTO As String = "ABCDEF"
    7.  
    8. KeyFrom = TESTFROM
    9. KeyTo = TESTTO
    10. InBuffer = TESTSTRING
    11. 'resize OutBuffer to match InBuffer
    12. ReDim OutBuffer(UBound(InBuffer))
    13. 'strings are unicode, every other byte is zero in byte array
    14. 'thus jump every other byte
    15. For A = 0 To UBound(InBuffer) Step 2
    16.     For B = 0 To UBound(KeyFrom) Step 2
    17.         'look for a character to replace
    18.         If InBuffer(A) = KeyFrom(B) Then OutBuffer(A) = KeyTo(B): Exit For
    19.     Next B
    20.     'check if the loop was exited
    21.     If Not (B < UBound(KeyFrom)) Then
    22.         'the loop was not exited, just copy the character
    23.         OutBuffer(A) = InBuffer(A)
    24.     End If
    25. Next A
    26.  
    27. MsgBox OutBuffer

    Not optimized for speed all that much, but is sure faster than using Mid$ and Asc to do it! Hope it helps.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Merri, does all that fall apart when you switch to unicode? Ascii has one byte/char, but unicode has two, and they aren't all that similar as far as I can see.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    that doesn't take multiple characters into effect.

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    dglienna: It wasn't even meant to do so, it is an example how to use byte arrays, because not all that many seem to know about them.

    Shaggy Hiker: it doesn't fell apart while you stay within characters 0 - 255, VB strings are internally always stored as two byte per character. If you want to do unicode, then you'd still do Step 2 as you'd want to check for both bytes the same time

    Developing that to work for longer character combinations needs some thinking and it is the reason I didn't want to start doing it ready this time (as I'm busy on IRC talking to my girlfriend ).

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    just pointing it out to him, he may have assumed differently.

    add [RESOLVED] to the subject of the first post to mark it as resolved.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    2
    *trying to comprehend Merri's code*

    Uh oh... Mental processes breaking down... Mainframe failure imminent... Reactor core at critical... Danger, Will Robinson! Danger!



    Seriously, though, I never reached that level of coding in my class. It was only an introductory class... I understand that "TESTTO" and "TESTFROM" are the conversions, and "TESTSTRING" is what's being converted, but not much more than that. (The basic concept of the code I can understand, but exactly how it works eludes me.) The guides I'm finding online aren't helping much either.

    And that Replace function doesn't work in this case because of the fact that it doesn't differentiate between the original characters and the replaced characters; if a "th" had already been changed to a "ch", and then encounters the line to replace any c's with an "s", then the "ch" will become "sh". It won't work like that without some SERIOUS changing of the translations, which may not even work in the end.

    In any case, there's no way I'm going to be able to get this program working on my own. No way in heck. I just don't have enough knowledge of the program to have a chance at it. So unless someone around here is willing to seriously help me out, it looks like I'm going to be doing this manually.
    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.

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    I'll first explain how the whole thing works


    To simplify, a string has a set of characters. Each character takes up two bytes. Thus, in memory, string looks like this:

    A B C D E F G

    A space represents byte zero. When you deal with strings, you're initially forced to processing the data as string: not as the actual numbers in memory. You can use Asc or AscW to get the character number, but it isn't very efficient. This is where byte arrays come in to play.

    It is possible to copy string data to byte array so that you get a more direct access to the actual numeric data of a string. Thus the string above in byte code would be:

    65 00 66 00 67 00 68 00 69 00 70 00 71 00
    (I used space to separate the bytes from each other)


    Thus, the first For A ... Next loop goes through the InBuffer array, if you give the string sample from above to it, it would first get 65, then 66 and so on (InBuffer(A)). Then, the For B ... Next loop goes through the comparison array. If it finds something to replace, it replaces it (= sets to OutBuffer the replacing byte) and exists the loop.

    Then the code check if the loop has been exited (it has been if B is smaller than the upper bound of the comparison array). If the code has not been exited, the OutBuffer is filled with the same byte that is in the InBuffer.

    In the end, the code shows the result with the help of a msgbox (byte arrays are automatically converted to strings when required).


    Now, to get going with your little project... it is probably much easier for you to just get going with Mid$. You could have a string array 1 with the data to replace from and then a string array 2 to replace to. Then you loop through the string comparing to the string array and filling a new string with new data as you go on. It is very slow though, due to the fact strings in VB are "intelligent".

    I'm in the middle if I bother to provide actual code as I think completing this kind of project will do real good for your mental processes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width