Showing posts with label translation. Show all posts
Showing posts with label translation. Show all posts

Sunday, August 31, 2008

Computer Humor... Truth In Multi-National Advertising?

Hey again,

You may recall, just a few days ago (I think...), we put out a script do language translation using Yahoo/AltaVista's Babelfish interface. I mentioned, briefly, in that post that, when translating something into a foreign language, I always like to do a "reverse translation" using the translated output from my original query, and see how close the back-translation comes to what I originally wrote. It can make for hilarious results. In fact, unless you're entering something like "hello," it's almost guaranteed to come back goofy when translated back to English (or your native language).

For instance, given a phrase, and randomly picking Italian for the language to translate to-and-from, we would get this:

Original Phrase: Can I get you a drink, officer?

Translated to from English to Italian: Posso ottenergli una bevanda, ufficiale?

Translated back to English: I can ottenergli a drink, official?

What??? Apparently, The Italian "ottenergli" has no literal or idiomatic translation in English. And, if the back translation proves anything, it's that direct translation between languages is fairly weak. The translator (preferably a human ;) needs to be able to translate idiomatic phrases and cultural nuances and adapt them accordingly.

The good news is that this can be even more hilarious for you :) At the Lost In Translation language-bouncing site, it'll put anything you want through multiple language filters and give you back something that more closely resembles gibberish than your original expression.

Even "Hello" is not immune!!! Here is the output (only the final output, although, on the Lost In Translation page it takes you through each step in the translation/back-translation process so you get a play by play on how your original meaning is mashed up and beaten into a sticky pulp of utter nonsense ;)

English Text Entered: Hello
After Final Back-Translation: Today

This makes no sense to me whatsoever (since I don't have a thorough understanding of all of the languages the site uses to "babelize" your original word or sentence). But it does make for some interesting reading. I've found, also, that if you choose not to include "Chinese, Japanese and Korean," your phrase has a better chance at surviving. For instance, "hello there," without the CJK, comes back as "hello there." With CJK, it seems to come back as either "highly there" or "today here" ;)

I, personally, find it very interesting to put my company's catch-phrase in. Any advertising slogan is generally good fodder, as well :)

I've tried to inline the initial "textarea" box here (you will be taken off of blogspot to tashian.com to get your results). If it doesn't work in your browser, you can get to the original Babel page here.

Cheers,



Enter English Text and "Babelize" it:




Include Chinese, Japanese, and Korean


, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Monday, August 25, 2008

Unix and Linux Online Language Translation Script

Hey There,

This week's start-off Linux/Unix bash shell script is a follow up to our previous online dictionary shell script and Thesaurus shell script. I was thinking of doing the online Encyclopedia today, but, for some reason, I got sidetracked on The BabelFish Language Translation Site.

This script offers the full functionality of the original website's text translation, except I left out the HTML page translation part. I figure if you're going to translate an entire webpage, it would look a lot nicer using a regular web browser ;)

Like our previous scripts, this script uses wget and sed. This script was also written using "brute force" scripting (that's my term for writing while I think it through). I don't know (at this point) where it could be made better, but I'm sure that it can :) I'll give it some thought later, and any recommendations are always welcome!

BTW, a huge "shout out" to the folks at AutoPOST for help getting all the POST elements I needed to pass in the POST request to the online translator. (I fuddled around for hours before I went looking for help... ). Good stuff. Check it out :)

Below is a screen shot of the output spewed when you throw the script the "-h" flag:

Click the picture below to see it in full size:

language preference abbreviations

Below is a screen shot of the script's output translating from one language to another, then back again (to see how literal the translation was - this can be very funny sometimes ;) and a long convoluted statement showing one limitation of the script with regards to the handling of "special" international characters:

Click the picture below to see it in full size:

language translations

To get started, either do:

host # ./trans.sh <-- for a straight up help screen

or:

host # ./trans.sh -h <-- to list out the required short names needed to do the translations BabelFish is able to do, as well as the regular help.

Hope you enjoy this bash script, and can find some use for it. Any suggestions for improvement would be greatly appreciated, of course :)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

# trans.sh - Ich verstehe nicht ;)
# Translate between languages using Yahoo's BabelFish
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ $1 == "-h" ]
then
echo "Usage: $0 TranslationPref YourQuotedPhrase"
echo
echo "TranslationPref Values:"
echo
echo "zh_en => Chinese-simple to English"
echo "zh_zt => Chinese-simple to Chinese-traditional"
echo "zt_en => Chinese-traditional to English"
echo "zt_zh => Chinese-traditional to Chinese-simple"
echo "en_zh => English to Chinese-simple"
echo "en_zt => English to Chinese-trad"
echo "en_nl => English to Dutch"
echo "en_fr => English to French"
echo "en_de => English to German"
echo "en_el => English to Greek"
echo "en_it => English to Italian"
echo "en_ja => English to Japanese"
echo "en_ko => English to Korean"
echo "en_pt => English to Portuguese"
echo "en_ru => English to Russian"
echo "en_es => English to Spanish"
echo "nl_en => Dutch to English"
echo "nl_fr => Dutch to French"
echo "fr_nl => French to Dutch"
echo "fr_en => French to English"
echo "fr_de => French to German"
echo "fr_el => French to Greek"
echo "fr_it => French to Italian"
echo "fr_pt => French to Portuguese"
echo "fr_es => French to Spanish"
echo "de_en => German to English"
echo "de_fr => German to French"
echo "el_en => Greek to English"
echo "el_fr => Greek to French"
echo "it_en => Italian to English"
echo "it_fr => Italian to French"
echo "ja_en => Japanese to English"
echo "ko_en => Korean to English"
echo "pt_en => Portuguese to English"
echo "pt_fr => Portuguese to French"
echo "ru_en => Russian to English"
echo "es_en => Spanish to English"
echo "es_fr => Spanish to French"
exit 0
elif [ $# -ne 2 ]
then
echo "Usage: $0 TranslationPref YourQuotedPhrase"
echo "For Tranlation Options: $0 -h"
echo "Ex: $0 en_de \"hi there. How are you?\""
echo "Ex: $0 en_it \"hi there. How are you?\""
exit 1
fi

langtran=$1
oargs=$2
args=`echo $oargs|sed -e 's/?/\\?/' -e 's/&/and/' -e 's/ /+/g'`
fromlang=`echo $langtran|awk -F"_" '{print $1}'`
tolang=`echo $langtran|awk -F"_" '{print $2}'`
echo

case $fromlang in
zh) longlang=Chinese-simple;fromlang=$longlang;;
zt) longlang=Chinese-traditional;fromlang=$longlang;;
en) longlang=English;fromlang=$longlang;;
nl) longlang=Dutch;fromlang=$longlang;;
fr) longlang=French;fromlang=$longlang;;
de) longlang=German;fromlang=$longlang;;
el) longlang=Greek;fromlang=$longlang;;
it) longlang=Italian;fromlang=$longlang;;
ja) longlang=Japanese;fromlang=$longlang;;
ko) longlang=Korean;fromlang=$longlang;;
pt) longlang=Portuguese;fromlang=$longlang;;
ru) longlang=Russian;fromlang=$longlang;;
es) longlang=Spanish;fromlang=$longlang;;
*) longlang="Unrecognized Language!";fromlang=$longlang;;
esac

case $tolang in
zh) longlang=Chinese-simple;tolang=$longlang;;
zt) longlang=Chinese-traditional;tolang=$longlang;;
en) longlang=English;tolang=$longlang;;
nl) longlang=Dutch;tolang=$longlang;;
fr) longlang=French;tolang=$longlang;;
de) longlang=German;tolang=$longlang;;
el) longlang=Greek;tolang=$longlang;;
it) longlang=Italian;tolang=$longlang;;
ja) longlang=Japanese;tolang=$longlang;;
ko) longlang=Korean;tolang=$longlang;;
pt) longlang=Portuguese;tolang=$longlang;;
ru) longlang=Russian;tolang=$longlang;;
es) longlang=Spanish;tolang=$longlang;;
*) longlang="Unrecognized Language!";tolang=$longlang;;
esac

wget=/usr/bin/wget

echo
echo "Original $fromlang: $2"
echo
echo -n "Translated $tolang: "
$wget -nv -O - http://babelfish.yahoo.com/translate_txt?ei=UTF-8\&doit=done\&fr=bf-res\&intl=1\&tt=urltext\&trtext=${args}\&lp=${langtran}\&btnTrTxt=Translate 2>&1|grep -i 'result'|egrep -iv 'txt-form|tips 1'|sed -e 's/$/\n/' -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/\&[A-Za-z]*\;/ /g' -e 's/ / /g' -e 's/\(.\)$/\1\n/' -e '/^ *\t*$/d'
exit 0


, Mike




Pete had this interesting comment to share. It brings up a very interesting way of attacking the script and you might find it useful in other situations as well! Our Thanks for the great observation!


Hi! When I copied in the script and ran, I got "unary operator expected at line 11." (or something simlar). I think it was just a slip up ( I do it often ), using the "C" syntax, instead of the shell syntax. In any case, changing to a single "=" corrected that. I usually, also, use the following test construct to avoid problems with shell scripts, when the tested variable may happen to have null contents.

If [ "x$1" = "x-h" ] ... that way, test never has to deal with a null argument.

Pete


Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.

Monday, April 14, 2008

Converting Solaris Pkg's Into RPM's on Linux

Greetings,

Today, we're going to wrap up our series of posts on rpm/pkg mixing and matching, as this is about the last thing that's, technically, really worthwhile doing (I think). So far we've created the following scripts based on Linux RPM and/or Solaris Unix pkg dissection, translation and derivation:

How to unpack Solaris datastream packages without using Sun's pkg tools

How to build pkg's using spec files, like in Linux, with pkg-build

How to create RPM's from already installed RPM packages

How to create Solaris datastream pkg's from already installed pkg's

How to convert Linux RPM's into Solaris datastream pkg's

How to build Linux RPM's part 1- Initial points

How to build Linux RPM's part 2 - Starting your spec file

How to build Linux RPM's part 3 - Finalizing the spec file and building the RPM

How to build your own Solaris datastream pkg's quickly

How to build your own Solaris datastream pkg's (more theory than practice)


And, now, whether or not it will ever be practical, we've put together a script to convert Solaris pkg's to RPM's on a Linux system. If nothing else, it rounds everything out :)

The script is easy to run, and only requires one argument of the pkg file that you'll be converting. For instance, to convert the Solaris 9 m4 pkg from SunFreeware.com, you'd just type this (after unzipping the package with gunzip):

host # ./pkg2rpm m4-1.4.10-sol9-sparc-local

One special note about this Perl script: When the pkg gets converted to an RPM, it's done so with the files (listed under the "%files" specification in the "spec file") saved in the RPM with the full path to the temporary build root. This is done because "%files" won't accept relative pathnames and also won't allow you to add files that don't exist (if you, say, populate "%files" with %{prefix}/bin/whatever, etc). Instead, the "Prefix:" value is set to the "BASEDIR" value in the pkg file (e.g. /usr/local), but the "%files" listed are in their actual location during the build (otherwise we'd be forced to overwrite good Linux binaries with potentially unexecutable Solaris binaries just to build the RPM). Therefore, you'll need to install the newly created RPM with the "--relocate" switch.

So, in our example, assuming we ran the above command from "/home/user" (with the pkg file being in that directory), we would want to install the resulting RPM file by first determining where it's supposed to be relocated and then running the "rpm" command, like so:

host # ls
m4-1.4.10-sol9-sparc-local m4-1.4.10-1.x86_64.rpm
host # rpm -qif m4-1.4.10-1.x86_64.rpm|grep -i reloc
Name : m4-1.4.10-1 Relocations: /usr/local
<--- The "Relocations:" specification is where we'll be relocating "to."
host # rpm -qlf m4-1.4.10-1|head -1 <--- The trunk of this output shows us where we will be relocating "from." In this case "/home/user/SMCm4"
/home/user/SMCm4/bin/m4

Now we know where we're relocating "from" and "to," so we're ready to install (Note that you can install this RPM in your home directory, or the build directory, if you want to. The only downside is the inconvenience ;)

host # rpm --force --nodeps --relocate=/home/user/SMCm4=/usr/local -ivh m4-1.4.10-1.x86_64.rpm
Preparing... ########################################### [100%]
1:m4 ########################################### [100%]


Now, all we have to do is a quick verify and we should see that we're all set:

host # rpm -q m4-1.4.10-1
m4-1.4.10-1
host # rpm -ql m4-1.4.10-1.x86_64.rpm|head -5
/usr/local/bin/m4
/usr/local/doc/m4/AUTHORS
/usr/local/doc/m4/BACKLOG
/usr/local/doc/m4/COPYING
/usr/local/doc/m4/ChangeLog


Here's hoping we've covered every useful angle of Solaris Unix datastream pkg to Linux RPM conversion and manipulation that we possibly can. At least for the here and now ;)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

Cheers,

#!/usr/bin/perl

#
# pkg2rpm - convert Solaris datastream pkg files to RPM's for Linux
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if ( $#ARGV != 0 ) {
print "Usage: $0 pkg_file\n";
exit(1);
}

$pkg_name = $ARGV[0];

chomp($simple_header_check=`grep -i "package datastream" $pkg_name >/dev/null 2>&1;echo \$?`);

if ( $simple_header_check != 0 ) {
print "This doesn't appear to be a valid SVR4 DataStream pkg file. Exiting...\n";
exit;
}

open(PKG, "<$pkg_name");
@pkg_file = <PKG>;
close(PKG);

$header_end = 0;
$cpio_start = 0;
open (P_HEADERS, ">$pkg_name.headers");
foreach $line (@pkg_file) {
if ( $header_end == 0 && $line =~ /TRAILER/ && $line =~ /pkginfo/ ) {
print P_HEADERS $line;
$header_end = 1;
} elsif ( $header_end == 1 && $line =~ /pkginfo/ ) {
print P_HEADERS $line;
close(P_HEADERS);
$cpio_start = 1;
open(P_CPIO, ">$pkg_name.cpio");
} elsif ( $cpio_start == 0 ) {
print P_HEADERS $line;
} elsif ( $cpio_start == 1 ) {
print P_CPIO $line;
} else {
print STDERR "WTF?\n";
}
}
close(P_CPIO);

open(P_HEADERS, "<$pkg_name.headers");
@p_headers = <P_HEADERS>;
close(P_HEADERS);

$output_dir = 0;
$pkginfo_start = 0;
$pkginfo_seek = 0;
$pkgmap_start = 0;
foreach $p_head (@p_headers) {
if ( $output_dir eq 1 && $pkginfo_seek == 0 ) {
$output_dir = $p_head;
$output_dir =~ s/^(\w+)\W*.*$/$1/;
chomp($output_dir);
$pkginfo_seek = 1;
} elsif ( $output_dir eq 0 && $p_head =~ /datastream/i ) {
$output_dir = 1;
} elsif ( $output_dir eq 0 && $pkginfo_start == 0 ) {
next;
} elsif ( $output_dir ne 0 && $p_head =~ /pkgmap/ ) {
$pkgmap_start = 1;
$pkginfo_start = 0;
} elsif ( $pkgmap_start == 1 && $p_head =~ /pkginfo/ ) {
chomp($p_head);
push(@pkgmap, $p_head);
last;
} elsif ( $output_dir ne 0 && $p_head =~ /pkginfo.*PKG=/ ) {
$pkginfo_start = 1;
} elsif ( $output_dir ne 0 && $pkgmap_start == 1 ) {
chomp($p_head);
push(@pkgmap, $p_head);
} elsif ( $output_dir ne 0 && $pkginfo_start == 1 ) {
chomp($p_head);
push(@pkginfo, $p_head);
}
}

mkdir("$output_dir");
chdir("$output_dir");
open(PKGINFO_OUT, ">>pkginfo");
foreach $info (@pkginfo) {
print PKGINFO_OUT "$info\n";
}
close(PKGINFO_OUT);
open(PKGMAP_OUT, ">>pkgmap");
foreach $map (@pkgmap) {
print PKGMAP_OUT "$map\n";
}
close(PKGMAP_OUT);
open(RPM_SPEC_FILES, ">>${pkg_name}.specfiles");
foreach $specfile (@pkgmap) {
@specfile = split(" ", $specfile);
if ( $specfile[1] !~ /^d$/ && $specfile[2] =~ /^none$/ ) {
print RPM_SPEC_FILES "$specfile[3]\n";
}
}
close(RPM_SPEC_FILES);
system("cpio -iv <../$pkg_name.cpio >/dev/null 2>&1");
chdir("reloc");
system("tar cpf - *|(cd ../;tar xpf -)");
chdir("../");

$rpm_name=$pkg_name;
chomp($starting_point=`pwd`);
$temp_var=$$;

system("mkdir ${starting_point}/BUILD");
system("mkdir ${starting_point}/SOURCES");
system("mkdir ${starting_point}/SPECS");
system("mkdir ${starting_point}/SRPMS");
system("mkdir ${starting_point}/RPMS");
system("mkdir ${starting_point}/RPMS/noarch");
system("mkdir ${starting_point}/RPMS/`uname -i`");
system("mkdir $temp_var");
system("cd $temp_var");
open(PKGINFO_RPM, "<${starting_point}/pkginfo");
@pkginfo_rpm = <PKGINFO_RPM>;
close(PKGINFO_RPM);
($pkgrpm_name,$pkgrpm_arch,$pkgrpm_version,$pkgrpm_category,$pkgrpm_vendor,$pkgrpm_email,$pkgrpm_pstamp,$pkgrpm_basedir,$pkgrpm_classes) = @pkginfo_rpm;
$pkgrpm_name =~ s/.*=(.*)\n$/$1/;
$pkgrpm_arch =~ s/.*=(.*)\n$/$1/;
$pkgrpm_version =~ s/.*=(.*)\n$/$1/;
$pkgrpm_category =~ s/.*=(.*)\n$/$1/;
$pkgrpm_vendor =~ s/.*=(.*)\n$/$1/;
$pkgrpm_email =~ s/.*=(.*)\n$/$1/;
$pkgrpm_pstamp =~ s/.*=(.*)\n$/$1/;
$pkgrpm_basedir =~ s/.*=(.*)\n$/$1/;
$pkgrpm_classes =~ s/.*=(.*)\n$/$1/;
open(RPM_SPECIFICS, ">>$rpm_name.spec");
print RPM_SPECIFICS "%define _topdir $starting_point\n";
print RPM_SPECIFICS "Summary: $pkgrpm_name $pkgrpm_version\n";
print RPM_SPECIFICS "Name: $pkgrpm_name\n";
print RPM_SPECIFICS "Version: $pkgrpm_version\n";
print RPM_SPECIFICS "Release: 1\n";
print RPM_SPECIFICS "Copyright: $pkgrpm_pstamp\n";
print RPM_SPECIFICS "Group: $pkgrpm_category\n";
print RPM_SPECIFICS "Prefix: $pkgrpm_basedir\n";
print RPM_SPECIFICS "%description\n";
print RPM_SPECIFICS "Vendor = $pkgrpm_vendor - Email = $pkgrpm_email - Arch = $pkgrpm_arch - Classes = $pkgrpm_classes\n";
print RPM_SPECIFICS "%files\n";
open(RPM_SPEC_FILES, "<${starting_point}/$pkg_name.specfiles");
@specks = <RPM_SPEC_FILES>;
close(SPECKS);
foreach $speck (@specks) {
print RPM_SPECIFICS "${starting_point}/$speck\n";
}
close(RPM_SPECIFICS);

system("rpmbuild -bb $rpm_name.spec 2>&1|grep -v twice");
chdir("../");
system("mv -f ${starting_point}/RPMS/*/* .");
system("rm -rf $output_dir");
unlink "$pkg_name.headers";
unlink "$pkg_name.cpio";


, Mike