PHP - Translation?

Penguin

Fledgling Freddie
Joined
May 11, 2005
Messages
375
Hey Guys,

I want to make a simple translator using PHP/MYSQL but i'm not too advanced with php at this point - so not sure exactly how to go about this.

Basically, i want the user to be able to input some text into a text field - Click "Translate" and have the input sliced up into seperate words and use this to check the database for words to replace it with. When no record is found, leaving the origional input.

I've tried searching for translator/replacing examples in php but i can't find many at all, atleast none which i can use or learn from.

I've got a decent ammount of experience and MYSQL but nothing like this before really.

Any help much appreciated, if this is really tricky please let me know and perhaps i'll leave it for a bit ;)

- Penguin
 

TheJkWhoSaysNi

One of Freddy's beloved
Joined
Dec 23, 2003
Messages
187
I dont know if this is the best way but...

I'd have your table like this:

Word (primary key) | Translation

then for each word you can do a query like this SELECT * FROM translations WHERE word = '$word'

You can get the individual words using explode(' ',$sentence);

To speed things up you'll probably want to use MySQLi and prepared queries. Because you'll be doing the same query over and over you'll want something like this:

Code:
$stmt = $mysqli->prepare('SELECT translation FROM translations WHERE word = ?');
for ($i = 0; $i <= count($words); $i++) {
$stmt->bind_param($words[i]);
$stmt->execute();
}

obviously you'd then need to get the result of each query and output it as a string.


While this would translate words, what about grammar? translations would make no sense without sorting out the grammar. I've no idea how you'd do this since I dont know the grammar rules for any language other than english.
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
You also need to identify the context for a word that has multiple meanings to get the right meaning when translated.. I'm curious though - why would you want to reproduce altavista's babelfish?
 

Penguin

Fledgling Freddie
Joined
May 11, 2005
Messages
375
I never said it was to translate text to an alternative language ;) Grammer is not important in this case, i just want to replace each word with an alternative which will be listed in the database. Thanks for the replys.

The thing is, if i seperate each word, i would then need to assign them to seperate variables? or could i have the query automatically find each word and then check?

I guess something like this? (In a text area)

SELECT replacement FROM database WHERE word = $word? LIMIT 1;

Then echo the found word, or use an else statement to just leave the origional if no word is found? Then move onto the next word?

My main problem is really seperating each individual word and how to use the seperated words, i've never done this before.

- Penguin

Edit: Perhaps i should have said "Converter" in place of translator - but i hope you get the idea now. :)
 

TheJkWhoSaysNi

One of Freddy's beloved
Joined
Dec 23, 2003
Messages
187
I told you how to do this. Once you have the array of words, you can use a prepared query and a loop to get the translation of each word.


Code:
$sentence = 'a sentence with a few words';
$words = explode(' ',$sentence);
then just plug $words into the code i gave you earlier.
of course you'll want to replace all the extra characters, !,. etc.

To do this effectivly the best way would be to use preg_match like this:

Code:
$sentence = 'a sentence with a few words';
preg_match_all('/[A-Za-z]+/', $sentence, $words);

This will put all the words in an array called $words. Then when you've got the replacement you can replace $words with $replacement in the original string and the punctuation, etc will be preserved.

edit: Sorry was thinking of perl. You want preg_match_all and no /g
 

TheJkWhoSaysNi

One of Freddy's beloved
Joined
Dec 23, 2003
Messages
187
You cant re-edit after a few mins?

Anyway, having a quick test, preg_match_all will (for some reason) put all the words in $words[0] so to access, for example 'few' from $sentence after setting $words using preg_match_all you'd use $words[0][4];
 

Penguin

Fledgling Freddie
Joined
May 11, 2005
Messages
375
Thanks for the help TheJkWhoSaysNi - It's much appreciated, i'm giving this a go now, may not be able to reply again untill tomorrow -
 

SheepCow

Bringer of Code
Joined
Dec 22, 2003
Messages
1,365
TheJkWhoSaysNi said:
preg_match_all will (for some reason) put all the words in $words[0] so to access

This is changed with the flags sent to the function to make it use pattern or set order.
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,046
you just loop through your regex return array with a for loop:
Code:
 for(i = 0; i < arraylength; i++) {
SQL BLAH BLAH array[i];
do stuff
whatever
}
 

Penguin

Fledgling Freddie
Joined
May 11, 2005
Messages
375
Thanks for the replys guys, i contacted TheJkWhoSaysNi via pm to get a little additional help, i've got it working now.

Much appreciated!

- Penguin
 

Chilly

Balls of steel
Joined
Dec 22, 2003
Messages
9,046
sorry, I forgot the $ signs for my variables in that snippet, I've been coding C++ all week and it looks very similar to php in places :)
 

Users who are viewing this thread

Top Bottom