translator
Posted: Tue Aug 13, 2002 3:38 pm
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!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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>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.