links would be much apreciated!
translator
Moderator: General Moderators
translator
Does anyone know any samples or templates for creating a translator type of tool in php?
links would be much apreciated!
links would be much apreciated!
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.
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
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.
I examined the (very dated) code of a piece called STPHP to help me get started originally I think.
HTH.
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.
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.
i have no idea really 
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.
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
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.
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
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.
You can then use this script to convert from english to other languages:
Plenty of room for improvement in this, but hope it helps you.
Sam
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++){
// mysql_query("DROP TABLE ".$languagesї$i]) or die(mysql_error());
$sql = "CREATE TABLE ".$languagesї$i]." (";
$sql .="english VARCHAR(60) NOT NULL, ";
$sql .=$languagesї$i]." VARCHAR(60) NOT NULL, ";
$sql .="INDEX english_index(english))";
$res = mysql_query($sql) or die(mysql_error()."<br>$sql");
}
// 4) for each language, read in the dictionary file and process it for mysql
for( $i = 0; $i < count($languages); $i++){
// load the file into an array of lines (which are in the format english word їtab] foreign word)
$lines = file("./".$languagesї$i].".txt") or die("Couldn't read file for ".$languagesї$i]);
// for each line in the array,
$numlines = count($lines);
$sqls = array();
$numwords = 0;
for( $j = 0; $j < $numlines; $j++){
// if the line is not a comment (ie first character is not #)
if( $linesї$j]ї0] != "#" ){
// split the line into the english / translation pair
$words = split("\t", trim($linesї$j]));
// put the words into the sqls array if an english and foreign word exist
if( count($words) == 2){
$sqlsї$numwords++] = "('".addslashes($wordsї0])."', '".addslashes(preg_replace('/\ї.*?\]/',"",$wordsї1]))."') ";
}
}
// if we have 500 words to insert, then insert them and continue
if( $numwords >= 500 ){
$sql = "INSERT INTO ".$languagesї$i]." (english, ".$languagesї$i].") VALUES ".join(",", $sqls);
mysql_query($sql) or die( mysql_error()."<br>$sql");
$numwords = 0;
$sqls = array();
}
}
// if any words left over to insert, then add them to db
if( $numwords != 0 ){
$sql = "INSERT INTO ".$languagesї$i]." (english, ".$languagesї$i].") VALUES ".join(",", $sqls);
mysql_query($sql) or die( mysql_error()."<br>$sql");
}
}
// close the mysql connection
mysql_close($conn);
?>
<h3>Finished!</h3>
Now <b>please</b> delete this file.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ї"doword"]) || isset($HTTP_POST_VARSї"dopara"]) ){
// 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ї"language"];
if( $language >= 0 && $language < count($languages) ){
if( isset($HTTP_POST_VARSї"doword"]) ){
$english = trim($HTTP_POST_VARSї"englishword"]);
$englishword = $english;
if( strlen($english) > 0 ){
$sql = "SELECT ".$languagesї$language]." AS translation FROM ".$languagesї$language];
$sql .=" WHERE english='$english'";
$result = mysql_query($sql) or die(mysql_error()."<br>$sql");
$english = stripslashes($english);
if( mysql_num_rows($result) == 0 ){
echo "<h4>Sorry, no ".$languagesї$language]." translation was found for $english.</h4>";
}else{
echo "<h4>".mysql_num_rows($result)." possible translation(s) found for $english.</h4><ol>";
while( list($translation) = mysql_fetch_row($result)){
echo "<li>$translation</li>\n";
}
echo "</ol>\n";
}
}
}else{
$words = preg_split("/ї\s,]+/", stripslashes($HTTP_POST_VARSї"englishpara"]),-1, PREG_SPLIT_NO_EMPTY);
if( count($words) > 0 && count($words) < 100){
$words = array_unique($words);
$sqls = array();
$cnt = 0;
while( list(, $word) = each($words)){
if( trim($word) != "" )
$sqlsї$cnt++] = "'".addslashes(trim($word))."'";
}
$sql = "SELECT DISTINCT english, ".$languagesї$language]." AS translation FROM ".$languagesї$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) ){
$searchї$cnt] = '/\b'.$e.'\b/i';
$replaceї$cnt] = '$1'.$t.'$2';
$cnt++;
}
$english = stripslashes($HTTP_POST_VARSї"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ї$language].":</b><br><br>\n".nl2br(htmlspecialchars($translation));
echo "<h4>Does that make any sense at all?</h4>";
$englishpara = htmlspecialchars(str_replace('"', '"',$english));
}
}
}
}
?>
<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++){
echo "<option value="$i"";
if( $i == $language ) echo ' SELECTED';
echo '>'.$languagesї$i].'</option>';
}
?>
</select>
<h4>Translate an English Word:</h4>
<input type="text" name="englishword" value="<?php echo $englishword;?>"> <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>Sam
speaking of translator innaccuracy, if you want to have fun, use babelfish to translate to a language and then translate the result back.
This:
This:
becomes, after english -> spanish -> englishPHP Rocks! you can do so many different loops and database queries with it.
there are much better ones too, but it's too early for me to be creative.Rocks of PHP! you can do so many diverse bows and questions of the data base with him.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
-
samscripts
- Forum Commoner
- Posts: 57
- Joined: Tue Apr 23, 2002 4:34 pm
- Location: London, UK
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
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