translator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

translator

Post by m3mn0n »

Does anyone know any samples or templates for creating a translator type of tool in php?

links would be much apreciated! :D
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

The difficulty in what you propose isn't the PHP, it's the dictionary. You better start out with a good dictionary if you want to make a translation tool. After that, it's not complicated, just search and replace. Problem is, those dictionaries aren't readily available (as far as I know - are there any GNU dictionaries maybe?), hence a (perceived?) lack of available scripts.
In any case, you can search evilwalrus.com and hotscripts.com - or even better, yours and my favorite place in the whole world, google.com. Good luck.
daemorhedron
Forum Commoner
Posts: 52
Joined: Tue Jul 23, 2002 11:03 am

Post by daemorhedron »

You can look into gettext, gdbm or dict.org to aid you in your quest. I wrote a translation tool using gdbm for language files, but someone still has to translate them from the english language files to their own language (though I wrote an interface to help them do that). So far there are english, japanese and chinese language files for that project, which I am sad to say is not open source at this time. IIRC correctly dict.org has language files freely available.

I examined the (very dated) code of a piece called STPHP to help me get started originally I think.

HTH.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

isn't there some way to make a database table with 2 colums

1 is in english and 1 is in the other language i want to translate

and then make a GET form and take whatever $Word = in colum 1 and show up on the page what $Word's equivilant in colum 2 is?

it is as simple as that, thats my first plan in this project, then i will work for more of an advanced database and translation tool something like altavista.com has where i can type setences and get it to translate.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

right, but the trick is, where do you get the column with the translations? are you going to write them manually?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

i have no idea really :P

i'm a total newbie to this php stuff but i do have theories to what i can and want to do and i see if it's possible with php. :D

I'm going to be experiementing with this idea for the next lil while, if anyone cares to lend a hand to a newbie with a mission please send me a messege on any of the messengers in my profile :)
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi, your post reminded me of something I tried doing when I first started with php. Didn't have much luck then, so I thought I'd try again now. Anyway, this is what I've come up with so far.

It uses language files available from http://www.june29.com/IDP/IDPfiles.html.

If you create a mysql database called dictionary, and run the following script with the correct db username / password, and the language files mentioned above in the same directory, it should create the tables you need and populate them with words.

Code: Select all

<?php

// 1) connect to the database (named dictionary)

$host = "";
$user = "";
$password = "";

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("dictionary") or die(mysql_error());

// 2) create an array of languages to use

$languages = array("German", "Spanish", "Italian", "Portuguese", "French");

// 3) for each language in the array, create the database table

for($i = 0; $i < count($languages); $i++)&#123;
//	mysql_query("DROP TABLE ".$languages&#1111;$i]) or die(mysql_error());
	$sql = "CREATE TABLE ".$languages&#1111;$i]." (";
	$sql .="english VARCHAR(60) NOT NULL, ";
	$sql .=$languages&#1111;$i]." VARCHAR(60) NOT NULL, ";
	$sql .="INDEX english_index(english))";
	$res = mysql_query($sql) or die(mysql_error()."<br>$sql");
&#125;

// 4) for each language, read in the dictionary file and process it for mysql

for( $i = 0; $i < count($languages); $i++)&#123;

	// load the file into an array of lines (which are in the format english word &#1111;tab] foreign word)

	$lines = file("./".$languages&#1111;$i].".txt") or die("Couldn't read file for ".$languages&#1111;$i]);

	// for each line in the array, 

	$numlines = count($lines);
	$sqls = array();
	$numwords = 0;

	for( $j = 0;  $j < $numlines; $j++)&#123;

		// if the line is not a comment (ie first character is not #)

		if( $lines&#1111;$j]&#1111;0] != "#" )&#123;

			// split the line into the english / translation pair

			$words = split("\t", trim($lines&#1111;$j]));

			// put the words into the sqls array if an english and foreign word exist

			if( count($words) == 2)&#123;
				$sqls&#1111;$numwords++] = "('".addslashes($words&#1111;0])."', '".addslashes(preg_replace('/\&#1111;.*?\]/',"",$words&#1111;1]))."') ";
			&#125;
		&#125;

		// if we have 500 words to insert, then insert them and continue

		if( $numwords >= 500 )&#123;
			$sql = "INSERT INTO ".$languages&#1111;$i]." (english, ".$languages&#1111;$i].") VALUES ".join(",", $sqls);
			mysql_query($sql) or die( mysql_error()."<br>$sql");
			$numwords = 0;
			$sqls = array();
		&#125;

	&#125;

	// if any words left over to insert, then add them to db

	if( $numwords != 0 )&#123;
		$sql = "INSERT INTO ".$languages&#1111;$i]." (english, ".$languages&#1111;$i].") VALUES ".join(",", $sqls);
		mysql_query($sql) or die( mysql_error()."<br>$sql");
	&#125;

&#125;

// close the mysql connection

mysql_close($conn);

?>
<h3>Finished!</h3>
Now <b>please</b> delete this file.
You can then use this script to convert from english to other languages:

Code: Select all

<html>
<head>
<title>The English Translator</title>
</head>
<body>
<?php

$language = 0;
$englishpara = "";
$englishword = "";

// create an array of languages to use

$languages = array("German", "Spanish", "Italian", "Portuguese", "French");

// if a submission has been made...

if( isset($HTTP_POST_VARS&#1111;"doword"]) || isset($HTTP_POST_VARS&#1111;"dopara"]) )&#123;

	// connect to the database (named dictionary)

$host = "";
$user = "";
$password = "";

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("dictionary") or die(mysql_error());

	$language = $HTTP_POST_VARS&#1111;"language"];

	if( $language >= 0 && $language < count($languages) )&#123;

		if( isset($HTTP_POST_VARS&#1111;"doword"]) )&#123;
			$english = trim($HTTP_POST_VARS&#1111;"englishword"]);
			$englishword = $english;
			if( strlen($english) > 0 )&#123;

				$sql = "SELECT ".$languages&#1111;$language]." AS translation FROM ".$languages&#1111;$language];
				$sql .=" WHERE english='$english'";

				$result = mysql_query($sql) or die(mysql_error()."<br>$sql");

				$english = stripslashes($english);
				if( mysql_num_rows($result) == 0 )&#123;
					echo "<h4>Sorry, no ".$languages&#1111;$language]." translation was found for $english.</h4>";
				&#125;else&#123;
					echo "<h4>".mysql_num_rows($result)." possible translation(s) found for $english.</h4><ol>";
					while( list($translation) = mysql_fetch_row($result))&#123;
						echo "<li>$translation</li>\n";
					&#125;
					echo "</ol>\n";
				&#125;
			&#125;
		&#125;else&#123;
			$words = preg_split("/&#1111;\s,]+/", stripslashes($HTTP_POST_VARS&#1111;"englishpara"]),-1, PREG_SPLIT_NO_EMPTY);
			if( count($words) > 0 && count($words) < 100)&#123;
				$words = array_unique($words);
				$sqls = array();
				$cnt = 0;
				while( list(, $word) = each($words))&#123;
					if( trim($word) != "" )
						$sqls&#1111;$cnt++] = "'".addslashes(trim($word))."'";
				&#125;
				$sql = "SELECT DISTINCT english, ".$languages&#1111;$language]." AS translation FROM ".$languages&#1111;$language];
				$sql .=" WHERE english IN (".join(",", $sqls).")";
				$result = mysql_query($sql) or die(mysql_error()."<br>$sql");
				$search = array();
				$replace = array();
				$cnt = 0;
				while( list($e, $t) = mysql_fetch_row($result) )&#123;
					$search&#1111;$cnt] = '/\b'.$e.'\b/i';
					$replace&#1111;$cnt] = '$1'.$t.'$2';
					$cnt++;
				&#125;

				$english = stripslashes($HTTP_POST_VARS&#1111;"englishpara"]);
				$translation = preg_replace($search, $replace, $english);

				echo "<h4>The (appallingly bad) translation is as follows:</h4>";
				echo "<b>Original English:</b><br><br>\n".nl2br(htmlspecialchars($english));
				echo "<br><br><b>".$languages&#1111;$language].":</b><br><br>\n".nl2br(htmlspecialchars($translation));
				echo "<h4>Does that make any sense at all?</h4>";
				$englishpara = htmlspecialchars(str_replace('"', '"',$english));
			&#125;
		&#125;
	&#125;
&#125;
?>
<form action="index.php" method="post">
<h4>Choose the language to translate to:</h4>
<select name="language">
<?php
for( $i = 0; $i < count($languages); $i++)&#123;
	echo "<option value="$i"";
	if( $i == $language ) echo ' SELECTED';
	echo '>'.$languages&#1111;$i].'</option>';
&#125;
?>
</select>
<h4>Translate an English Word:</h4>
<input type="text" name="englishword" value="<?php echo $englishword;?>">&nbsp;<input type="submit" name="doword" value="Translate Word">
<h4>Translate a paragraph:</h4>
<textarea name="englishpara" cols="60" rows="4"><?php echo $englishpara;?></textarea>
<br><input type="submit" name="dopara" value="Translate Paragraph">
</form>
</body>
</html>
Plenty of room for improvement in this, but hope it helps you. :lol:

Sam
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Just searching and replacing isn't going to produce perfect code, many languages have different word orders to others so it maybe bad grammer produce will.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

speaking of translator innaccuracy, if you want to have fun, use babelfish to translate to a language and then translate the result back.

This:
PHP Rocks! you can do so many different loops and database queries with it.
becomes, after english -> spanish -> english
Rocks of PHP! you can do so many diverse bows and questions of the data base with him.
there are much better ones too, but it's too early for me to be creative.
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

On Radio 1 over here they have a game like that they convert a song to a random language and then convert it back and people have to guess the origional song, it can be quite humourous :wink:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

lmao llimllib :lol:

thanks alot sam!!! 8)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

i got

"Rocks of Php! you can do so many diverse curls and interrogations of the data base with him."
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

hey i just got around to testing out that translator Sam and it didnt work, i got an error while instaling the tables into the DB, i was using phpMyAdmin if it helps...
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi Oromian,

I just ran the scripts on my server to install the db and they worked ok.
What are you doing with phpmyadmin? Creating the database or the tables themselves? To get the install script to work, you'll need the correct host, username and password.

Can you give me more info about what you are doing with phpmyadmin and what error message it gives you (if any), and I'll try to help.

cheers, Sam :D
Post Reply