Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'word for word' translation - easy? 2

Status
Not open for further replies.

ivanmcp

Technical User
Apr 25, 2003
14
GB
Hi,

I am complete newbie, as some of you know already...

I have a file which contains two columns, separated by space. First column has english words and second column has words in french, like:

house maison
meal repas
sea mer
water eau
etc...

I use this code to run the translation:

echo "Enter the word in english:"
read string
echo "Same word in french is:"
awk ' { if ($1 == word)
print $2 }' word="$string" lang_words.txt

How can I modify this to read the whole sentence (made up of words in lang_words.txt file only)

For example, I would be prompted with:

echo "Enter the sentence in english:"

followed by:

echo "Same sentence in french is:"

This is "word for word" translation, so it should be fairly easy, but it is still beyond me.

Any ideas on how this would work?
 
Just add a for loop around the awk statements,
eg:
echo "Enter the sentence in english:"
read line
SENTENCE=$line
for WORD in $SENTENCE
do
awk......
done
 
Create a file called words.txt, with the following contents
Code:
house maison
meal repas
sea mer
water eau

Create a file called translate.awk, like so
Code:
#!/usr/bin/awk -f
#
BEGIN {
  while ( getline < &quot;words.txt&quot; ) {
    map[$1] = $2
  }
  close( &quot;words.txt&quot; )
}

{
  for ( i = 1 ; i <= NF ; i++ ) {
    if ( $i in map ) {
      printf( &quot;%s &quot;, map[$i] )
    } else {
      printf( &quot;%s &quot;, $i )  # no translation 
    }
  } 
  print &quot;\n&quot;
}

When you run the program, it looks like this
$ awk -f translate.awk
the house by the sea
the maison by the mer
 
lhurwitz, thanks for your response, this gives me an idea of how it all works.

Salem, nice and rich code you have posted :).

I have created all the files above, but only problem is that I am not sure as to how do I make it all work. How do I make a call to your code, so that I am prompted for a sentence in english, execute your code and get translation in french.

I feel like I was given this great car engine, I have my own rusty frame, but do not know how to install this engine in a car. Which cables go where type thing, hehe.

Thanks for your effort.
 
Taking your

echo &quot;Enter the word in english:&quot;
read string

Add this
echo $string | awk -f translate.awk
 
Cool, but I get error message after typing sh translate.txt:

Enter the word in english:
sea house

awk: translate.awk:4 aawhile (getline < &quot;lang_french&quot; ) {
awk: translate.awk:4: ^ invalid char 'a' in expression

note that small aa in aawhile and 'a' have little angled line above them. Never saw those before.

and this is my translate.awk (yours :))

#!/usr/bin/awk -f
#
BEGIN {
  while ( getline < &quot;lang_french&quot; ) {
    map[$1] = $2
  }
  close( &quot;lang_french&quot; )
}

{
  for ( i = 1 ; i <= NF ; i++ ) {
    if ( $i in map ) {
      printf( &quot;%s &quot;, map[$i] )
    } else {
      printf( &quot;%s &quot;, $i )  # no translation
    }
  }
  print &quot;\n&quot;
}

note: lang_french is my dictionary file

and this is my (yours, hehe) translate.txt file:

echo &quot;Enter the word in english:&quot;
read string

echo $string | awk -f translate.awk

I feel that something very minor is at play here, and has decided to spoil this effort.
 
> note that small aa in aawhile and 'a' have little angled line above them. Never saw those before.
I'm guessing it's some HTML 'space' which isn't really a space, but causes problems when you copy/paste it.

 
Your advice did help in your last post. After typing your code manually, everything went smoothly and worked first time. Awesome job. Thank you again, as I feel like I did learn a great deal. Star has been awarded :).

Ivan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top