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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write a language translator? 1

Status
Not open for further replies.

LadyClaire

Technical User
Dec 10, 2002
26
0
0
US
I am presently trying to write a language translator for the elvish language based on Lord of the Rings. Many in my area wish to learn the language. I have a full dictionary of translations in doc and txt format. I want to be able to search that file for a word or phrase and the proper tanslation appear on the screen after the submit button is clicked. Please help.
 
And you want to do this in Javascript, that is you do not plan to use CGI or ASP?

The Javascript way

Make a new text file named elvish_english.js . This will be used to look up the english meaning of the elvish word. And another text file named english_elvish.js to look up the elvish meaning of english words.

The elvish_english.js file will have this code.

Code:
var elvishWords = new Array();
var englishWords = new Array();
var englishDefinitions = new Array();

//The code for each word has this structure.
elvishWords[0] = "wlsh";
englishWords[0] = "money";
englishDefinitions[0] = "Medium for exchanging value for goods."

elvishWords[1] = "glmmr";
englishWords[1] = "glamor";
englishDefinitions[1] = "Appearance of bright richness in dress and grooming."

etc.

In your translation page you will have two forms, one for each direction of translation.

The forms don't actually submit, instead they run a function to display the results. One form has three fields, one for the word and two for the results.

Code:
<form name=&quot;elen&quot;>
  <input type=&quot;text&quot; name=&quot;elvish&quot;>
  <input type=&quot;text&quot; name=&quot;english&quot;>
  <textarea name=&quot;definition&quot;></textarea>
  <input type=&quot;button&quot; value=&quot;Please Translate&quot; onclick=&quot;trElvishEnglish()&quot;>
</form>

Four scripts will be needed. You have already created two of them, they are the dictionary files. Incorporate them into your page in the <HEAD> section like this. (This is just a convention, they can go anywhere.)

Code:
<html>
<head>
  <title>Elvish English Translator</title>
  <script src=&quot;elvish_english.js&quot;></script>
  <script src=&quot;english_elvish.js&quot;></script>
</head>
<body>
 . . .
</body>
</html>

Then the two translation functions could also go in the <HEAD>.

Code:
<script>
function trElvishEnglish(){
  var found = false;
  var target = document.elen.elvish.value;
  for(var i=0;i<elvishWords.length;i++){
    if( target == elvishWords[i] ){
      document.elen.english.value = englishWords[i];
      document.elen.definition.value = englishDefinitions[i];
      found = true;
      break;
    }
  }
  if( found == false ){
  document.elen.english.value = &quot;Sorry, that word is not in the dictionary&quot;;
  }
}
</script>

I leave the english_elvish translation to you.
 
this is cool, my question is more just something for the future:
is the sentence structure the same for english and for the elvish or is it different? and so will you want to match whole frases and not just single words?
this is where it gets interesting

alex
 
Ok I have my two .js files made. What I am wanting is to also be able to type in a whole sentence or phasre and it picks all the patching words out of one or the other of the two js files and bring up the sentence. And the sentence structure is the same for the english. And rac2, your giving me an example of the code just for translating from elvish to english, so I wil have to do the english to elvish translation just like this you gave me right?
 
Ok I have both .js files made for the single words. The sentence structure is the same, basically if you typed in &quot;You smell like a human&quot; in english it would give you back &quot;Lle holma ve’ edan&quot; in elvish, where Lle = You, Holma = smell, ve' = like, edan = a human. Would I have to make up two more files for the phrases like I did the individual words or is there some code I can type where it would search for all words you type in and it would give you back the phrase?
 
&quot;The sentence structure is the same ...&quot;

This phrase is ripe with significance. Translation is so much more than substituting words from one language for words in another. I'm sure you appreciate this. What we are about here I would call recoding, mapping codes from one set into codes in another. Since elvish grammar has the same syntax as english, recoding will yield satisfying results.

In a word yes you will search the dictionaries for each word and make the substitution. A phrase dictionary won't be needed until you get around to recoding idioms. The code you need to write will pick individual words out of phrases, look up each one and build a new phrase with the words from the other language.

The essential Javascript method is split(). With this method you can break a phrase into an array of words.

Suppose the phrase was entered into a textarea in a form; for example the phrase might be &quot;Men's faults do seldom to themselves appear.&quot;

A function to break apart the words might look like this-

Code:
function wordsFromPhrase(){
   //Put the phrase into a variable.
   //Here the phrase comes from a field named phrase
   //  in a form named myForm.
   var aPhrase = document.myForm.phrase.value;
   
   //Break the phrase into an array of words.
   var words = aPhrase.split( &quot; &quot; );
   
   return words;
}

You refer to a particular word with a subscript. For example words[0] is the word &quot;Men's&quot;, words[1] is &quot;faults&quot;, words[6] is &quot;appear&quot;.

Now you write a loop to recode each word.

Code:
...
var englishWords = wordsFromPhrase();
var elvishWords = new Array();

for(var i=0; i<englishWords.length; i++){
   elvishWords[i] = trElvishEnglish( englishWords[i] );
}
...

The function I used here, trElvishEnglish(), would be a variation on the one in my original post. This new one would take an elvish word as an argument and return the english word. It would return a code number if the word is not in the dictionary; and the program would need to check this code and take some alternative action for that word.

I wonder whether you are combining an interest in elves with an interest in learning to program?
 
Wow, You are a great help rac2. I know anyway I do this it is going to take, lots of time, work and patience. I am also looking into creating it with Macromedis Flash MX as well.

I have been interestied in both programming and elves for about 4 years now. In college we just programmed in VB, C, and C++. They just recently started teaching Javascript where I went to college at. I learned html on my own through the internet. And in the local community college they only hit the basics of programming.

Anyway I play RPG's online alot and I have an elvish character that I have had for about 4 years now and I was tired of going in and looking in my dictionary everytime I wanted to say someting in elvish and find the individual words. I have found one place on the net that had a Drow translator and no Sindarian translator, but if you know anything about elves the Drow are evil vile creatures and my character is a Sindarian or High Elf, depending on if you look to AD&D or J.R.R. Tolkien for your elvish literature. So yes I suppose you could say I am combineing my interest in elves with my interest in programming. I know others who can write up this program for me and create a translator real quick but I want to do it myself. That I way I learn something in the process of playing my game.

Thanks again for your help rac2, and if there is anyway I can contact you individually, please let me know. You have been the best help on this so far.
 
So far this is the script I have setup on my page. I think it would be easier to do this in Frames which I can do.

<html>
<head>
<title>English-Elvish Translator</title>
<script src=&quot;elvish_english.js&quot;></script>
<script src=&quot;english_elvish.js&quot;></script>
<script>
function trElvishEnglish(){
var found = false;
var target = document.elveng.elvish.value;
for(var i=0;i<elvishWords.length;i++){
if( target == elvishWords ){
document.elveng.english.value = englishWords;
found = true;
break;
}
}
if( found == false ){
document.elveng.english.value = &quot;Sorry, that word is not in the dictionary&quot;;
}
}
</script>

<script>
function trEnglishElvish(){
var found = false;
var target = document.engelv.english.value;
for(var i=0;i<englishWords.length;i++){
if( target == englishWords ){
document.engelv.elvish.value = englishWords;
found = true;
break;
}
}
if( found == false ){
document.engelv.elvish.value = &quot;Sorry, that word is not in the dictionary&quot;;
}
}
</script>
</head>
<body background=&quot;stonewall.jpg&quot;>

<center><font color=&quot;#00CC00&quot;><font size=+3>Eniglish-Elvish Translator</font></font></center>

<form name=&quot;engelv&quot;>
<input type=&quot;text&quot; name=&quot;english&quot;>
<input type=&quot;text&quot; name=&quot;elvish&quot;>
<input type=&quot;button&quot; value=&quot;Please Translate&quot; onclick=&quot;trEnglishElvish()&quot;>
</form>
<br><br>
<form name=&quot;elveng&quot;>
<input type=&quot;text&quot; name=&quot;elvish&quot;>
<input type=&quot;text&quot; name=&quot;english&quot;>
<input type=&quot;button&quot; value=&quot;Please Translate&quot; onclick=&quot;trElvishEnglish()&quot;>
</form>
<p><br>
<br>
</body>
</html>

My questions are as follows:
1. Get error messages on my page when I run the script and I dont know what they mean or how to fix them.
a. Line 82
Char 29
Error: Unterminated String Constant
The above Error is on line 83 also
b. Line 27
Char 15
Error 'englishWords'is undefined
c. Line 10
Char 15
Error 'elvishWords' is undefined

2. Would it be better to do this in Frames instead of on one page? That which we are we are, made weak by time and fate, but strong in mind to try, to seek, to find, but not to yield.
 
2. Regarding the scripts which I have suggested, no there is nothing to gain by using frames. You may have other considerations that would lead to frames, but nothing in the translation procedure would be easier in frames.

1.b and c. The errors lie in the .js files. That is where englishWords and elvishWords are defined. Those files must exist in the same folder as this page. They must have the statements . . . Oooops!

Today I think you need one dictionary file with both english and elvish words and definitions. Oh boy, that could be a costly mistake if you have typed in a lot of words already. Very sorry.

Here is the new approach.

Make a dictionary.js file like this.
Code:
var elvishWords = new Array();
var englishWords = new Array();
var englishDefinitions = new Array();
var elvishDefinitions = new Array();


//The code for each word has this structure.
elvishWords[0] = &quot;wlsh&quot;;
englishWords[0] = &quot;money&quot;;
englishDefinitions[0] = &quot;Medium for exchanging value for goods.&quot;
elvishDefinitions[0] = &quot;Fluvium par trado valor ala cosas.&quot;

elvishWords[1] = &quot;glmmr&quot;;
englishWords[1] = &quot;glamor&quot;;
englishDefinitions[1] = &quot;Appearance of bright richness in dress and grooming.&quot;
elvishDefinitions[1] = &quot;Anshu do brillig ricos par drou y marifos.&quot;

etc

Oh boy that elvish was tough (to invent).

Then in the HTML only include the dictionary.js like so-
Code:
<html>
<head>
  <title>Elvish English Translator</title>
  <script src=&quot;dictionary.js&quot;></script>
</head>
<body>
 . . .
</body>
</html>

It is possible that having englishElvish and elvishEnglish declared in both files would generate the undefined errors. I would like to know whether you had created the two .js files and whether they were in the same folder with the page.

1. I don't see any unterminated string constants in the code you posted. I also don't count 82 lines. The error probably is in the .js file, which has a multitude of string constants, therefore many opportunities to leave out a quotation mark. A quotation mark begins and terminates a string constant. A string constant is characters beginning and ending with quotation marks.

One last thing. You didn't mention it but there is another type of error in the javascript you posted. The undefined thingies, which we hope to define, elvishWords and englishWords are the names of arrays, that is lists. To refer to a particular item on the list you must use a subscript like so-
Code:
target == elvishWords
Code:
[i]
Code:
 ){
document.elveng.english.value = englishWords
Code:
[i]
Code:
;

Don't be discouraged. Programming is always a matter of trial-and-error. Well at least for me.

Again I apologize for the bad advice regarding the dictionary.
 
Dont apologize, when I get it finished I am giving most of the credit to you on the web site. If it werent for you I wouldnt have gotten this far. So basically I only need one .js file then. I'm not putting definitions on the page so that will cut down on alot of work. And then I just add

target == elvishWords ){
document.elveng.english.value = englishWords;

target == englishWords ){
document.engelv.elvish.value = eelvishWords;

to the Head section? That which we are we are, made weak by time and fate, but strong in mind to try, to seek, to find, but not to yield.
 
ok something isnt right if you want to see the script on the actual site its at :


Do the View Source and you can look and see what the problem is. it gives me the undefined errors when I hit the submit button on the page. Maybe you can look at it and find where the problem is. And I searched through the js file and couldnt find a missing &quot;;&quot; but I did find a missing quote mark so I added it. But there is still a unterminated string constant that I cant find on line 83. If you want the js file I can mail it to you. its too big to post here, there is about 1625 words in the dictionary so far. That which we are we are, made weak by time and fate, but strong in mind to try, to seek, to find, but not to yield.
 
Hello,

The dictionary.js file looks fine. And you know what, the Translator works for me, I don't get any errors. I used it several times with words from both ends of the list and translating in both directions, and in Netscape and IE.

Congratulations, I think you've got it.

Regards.
 
Thanks so much, I figured out my probs right after I posted that last post. I did start a new thread though. Called Translator Fixed rac2. There is where I will post any new questions about it because I still need the phrases part done which I totally got confused on, and eventually I will want to add a clear button that will clear all entries before your next translation. That which we are we are, made weak by time and fate, but strong in mind to try, to seek, to find, but not to yield.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top