LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2014, 05:05 AM   #1
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Rep: Reputation: 1
Script to translate comments to English


Hi!

I need to troubleshoot a CNC lathe. The program is written in assembly and the comments are in Czech.
Found a script that connects to google translate. It is used like this:
Code:
#translate 'kryt vretena otevren' cz en
I was hoping there was a way to use it with sed to find comments in the code, running them through the translate script and outputting the translate code to stdout.

Something like this:
cat program | sed blabla translate > tans_program

Here is a code snippet:
Code:
;chyba krytu vretena

        LDN     IX_COVER                ;kryt vretena otevren
        ST      MX_E17                  ;(chyba krytu vretena)

        LD      QX_SPEED_CW             ;odblokovany reg. vretena
        ANDN    WIX_S_LESS_NMIN         ;nenulove otacky vretena
        ANDN    IX_COVER                ;kryt vretena otevren
        SET     MX_SPDL_BREAK           ;zastaveni vretena
        SET     MX_M5_REQ               ;pozadavek M5
Can someone help me out?

//John

Last edited by john_erlandsson; 03-27-2014 at 02:27 PM.
 
Old 03-27-2014, 05:22 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,203

Rep: Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675
yes, it looks possible. What have you tried so far? I would suggest you to use sed 'command' programfile | trans_program (there is no need to use cat).
But you can even try awk:
awk -F\; ' <filter $2 thru your translator>; print ' input_file > output_file
 
Old 03-27-2014, 12:46 PM   #3
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Original Poster
Rep: Reputation: 1
I don't think it is possible to pipe to the translate command since my output needs to be the code with translated comments.

I was thinking something like:
Code:
sed 's/;.*$/;translate cmd/g' MAIN.PRG
Guess I need to match the comment part, put it in a variable, execute translate command with the variable as argument and finally substitute the match with command output.
Is that possible?

This is where I found the translate script.

Last edited by john_erlandsson; 03-27-2014 at 12:49 PM.
 
Old 03-27-2014, 01:12 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,203

Rep: Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675Reputation: 2675
using sed it will not be so simple, but with awk or perl you can surely do it.
I would try to write a shell script first which will use args as input text and prints result to stdout. In your case, something like this:
Code:
#!bin/bash
translate "$@" cz en
will do that job line by line. Next, I will try to call that small script like here:
Code:
#!/bin/perl -wn
my ($a, $b) = split (/;/);
my $l = `system("your_tranlsate_script $b")`;
print "$a;$l\n";
and you need to invoke this perl script by scriptname.pl inputfile > outputfile
inputfile is your original source and outputfile will contain the same assembly code with translated comments

(this code is not tested, just a plan)
 
Old 03-27-2014, 01:17 PM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,848
Blog Entries: 36

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
The question is... do you want to simply translate the strings and write them to stdout? Do you want to preserve the code and translate the comments with the code inline and have basically a translated asm file?

The first question would be easy...

Code:
sed 's/.*;\(.*\)/\1/' code.asm | grep -v '^$' | while read x;do translate "$x" cz en;done
However, that approach only works for translating the comments alone. If you want to translate the comments and leave the code intact then you'll need to use a more powerful scripting language which would translate the code and preserve the whitespace indentation of the code. Using bash+sed would not preserve leading whitespace in that scenario.

Here's a full conversion using python. It makes use of the google translate python module.

Code:
#!/usr/bin/env python
#Sam Gleske
#Thu Mar 27 14:48:54 EDT 2014
#Ubuntu 12.04.4 LTS \n \l
#Linux 3.8.0-37-generic x86_64
#Python 2.7.3
#Translates assembly comments from cz to en.
#Requires google translate python module - https://github.com/terryyin/google-translate-python

from os.path import isfile
from sys import argv
from sys import exit
from sys import stdin
from translate import Translator

def main(argv):
  if len(argv) > 1 and not isfile(argv[1]):
    print "%s is not a file!" % argv[1]
    exit(1)
  tl=Translator(from_lang="cz",to_lang="en")
  #read from stdin or a file
  if len(argv) < 2:
    data=stdin.read()
  else:
    with open(argv[1],'r') as f:
      data=f.read()
  #try translating comments otherwise simply output the line
  for x in data.split('\n'):
    parts=x.split(';',1)
    if len(parts) > 1:
      parts[1]=tl.translate(parts[1])
      print ';'.join(parts)
    else:
      print x

if __name__ == '__main__':
  try:
    main(argv=argv)
  except KeyboardInterrupt,e:
    print "User aborted."
    exit(1)
Note do not name the python file 'translate.py'. Name it something like asm_translate.py otherwise it will conflict with the translate module.

Then you can translate your code with the following command.

Code:
#take a file as an argument
python asm_translate.py code.asm > code.en.asm

#read from stdin
python asm_translate.py < code.asm > code.en.asm

#or do a whole bunch of asm files with the help of bash
for x in *.asm;do
  python asm_translate.py "${x}" > "${x%.asm}.en.asm"
done
You can add asm_translate.py to your $PATH and use it like any other standard cli command with pipes or with the first argument a file to be read.

That code can be improved by using optparse or argparse and allowing script user to set language arguments like --from-lang and --to-lang which would make it more generic. For now I'm lazy.

Last edited by sag47; 03-27-2014 at 02:29 PM.
 
1 members found this post helpful.
Old 03-27-2014, 02:26 PM   #6
john_erlandsson
Member
 
Registered: Jul 2009
Location: Sweden
Distribution: Fedora
Posts: 70

Original Poster
Rep: Reputation: 1
Damn. I solved it with awk just before you posted the python code.

I confused the translate script with another one that didn't work. This one should not take any parameters but the text to be translated.
Here is my awk solution:

Code:
#!/usr/bin/awk -f

BEGIN {
        FS=";"
}

{
        line = $0

        if( length($2) > 1 )
        {
                code = $1
                "translate \"" $2 "\"" | getline comment
                print code ";" comment 
        }
        else
        {
                print line
        }
}
Anyway. Thanks alot for the cool python solution. I will use that next time.
 
Old 03-27-2014, 02:58 PM   #7
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,848
Blog Entries: 36

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Heh, well here's a more robust python script which customizes arguments and can be used on the cli with your $PATH.

https://github.com/sag47/drexel-univ...m_translate.py

asm_translate.py --help

Code:
usage: asm_translate.py [-h] [--from-lang FROM_LANG] [--to-lang TO_LANG]
                        [asmfile]

Translate assembly code comments. From cz to en by default.

positional arguments:
  asmfile               Optional asm file to read. Otherwise read from stdin.

optional arguments:
  -h, --help            show this help message and exit
  --from-lang FROM_LANG
                        Translate from language.
  --to-lang TO_LANG     Translate to language.
Sample usage...

Code:
#take a file as an argument
asm_translate.py code.asm > code.en.asm

#read from stdin
asm_translate.py < code.asm > code.en.asm

#or do a whole bunch of asm files with the help of bash
for x in *.asm;do
  asm_translate.py "${x}" > "${x%.asm}.en.asm"
done

asm_translate.py --to-lang ja code.asm > code.ja.asm

cat code.asm | asm_translate.py --to-lang zh


SAM

Last edited by sag47; 03-27-2014 at 06:55 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please translate this PHP script to BASH protorox Programming 8 03-28-2014 12:09 PM
[SOLVED] hiphop on centos 6.3 - problems to translate a simple script eantoranz Programming 3 07-12-2012 02:19 PM
BASH/No X: Using google translate to convert TXT files (translate) frenchn00b Programming 10 09-13-2009 10:55 PM
LXer: Translate the Haskell into English Manually, Part II LXer Syndicated Linux News 0 08-05-2006 06:21 PM
LXer: Translate Haskell into English Manually LXer Syndicated Linux News 0 06-21-2006 11:12 PM


All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration