How To Rearrange Words In a Sentence ?
Moderator: General Moderators
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
How To Rearrange Words In a Sentence ?
Hi you all. I came here because google told me for keywords "large forum php minus bb" and boy am i glad to be here. lots of posts. and awesome. So who is ready to take this challenge. Forgive me. I wish i wouldn't have put some trouble here.
given
$q="they are nice"
I need it like : they nice are.
if $q="they were eating"
I need output to be "they eating were"
if $q="you guys are great"
required output "you guys great are"
as you can see, there is a verb (are) that needs to be at the end of sentence.. I need to translate from English to a foreign language where verbs happens to be at the end.
Guys... I am counting on you. Could you please help me ? I will never ever forget you. Once again guys.... given $q="helping is good" required : $q="helping good is"
Thank you so very much.
given
$q="they are nice"
I need it like : they nice are.
if $q="they were eating"
I need output to be "they eating were"
if $q="you guys are great"
required output "you guys great are"
as you can see, there is a verb (are) that needs to be at the end of sentence.. I need to translate from English to a foreign language where verbs happens to be at the end.
Guys... I am counting on you. Could you please help me ? I will never ever forget you. Once again guys.... given $q="helping is good" required : $q="helping good is"
Thank you so very much.
Re: How To Rearrange Words In a Sentence ?
Try explode()-ing it with a space as the delimiter, switching stuff around, and implode()-ing it back?
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
OMG!!! This is the first time i have got such a fast response .Thank you Syntac and this forum. AWESOME.Syntac wrote:Try explode()-ing it with a space as the delimiter, switching stuff around, and implode()-ing it back?
Syntac, i have tried that. But i can't get it. The explode helped me break them into array. But i need a loop or sort that looks for "are" or whatever i put in and it takes that out and puts it at the end. isn't there a function or something that makes life easier, say i just want the function to put a particular word at the last. in my case, given "this is a great forum" would be "this a great forum is" (the function would take the word "is" and put it at the end"
please help me with a step by step guide. please !!!!! H E L P
Thank you for fast reply.
Re: How To Rearrange Words In a Sentence ?
I can't write you anything off the top of my head, but I'll be sure to give it some thought tomorrow. 
Re: How To Rearrange Words In a Sentence ?
Just out of curiousity, would that language be Japanese?
In any case, I really don't think English can be broken in such a simple manner - you just gave very simple examples. But for your needs maybe this simple solution would do the job:
Of course you can add as many verbs to that array as you would like.
In any case, I really don't think English can be broken in such a simple manner - you just gave very simple examples. But for your needs maybe this simple solution would do the job:
Code: Select all
function verbsInTheEnd($sentence,$verbs) {
$wordArray = explode(' ',$sentence);
foreach($wordArray as $key => $word) {
if(in_array($word,$verbs)) {
unset($wordArray[$key]);
$wordArray[] = $word;
}
}
return implode(' ',$wordArray);
}
$verbs = array(
'are',
'were'
);
$sentence = 'you guys are great';
echo verbsInTheEnd($sentence,$verbs);
//Outputs "you guys great are"
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
Thank you so much. I am gonna try that. I looked everyone on the Net but I seem to have found the solution. Thank looks awesome. That way i can add more verbs. Thank you so much.
this forum rocks and thank you "pytrin" for such a prompt reply.
this forum rocks and thank you "pytrin" for such a prompt reply.
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
pytrin wrote:Just out of curiousity, would that language be Japanese?
In any case, I really don't think English can be broken in such a simple manner - you just gave very simple examples. But for your needs maybe this simple solution would do the job:Of course you can add as many verbs to that array as you would like.Code: Select all
function verbsInTheEnd($sentence,$verbs) { $wordArray = explode(' ',$sentence); foreach($wordArray as $key => $word) { if(in_array($word,$verbs)) { unset($wordArray[$key]); $wordArray[] = $word; } } return implode(' ',$wordArray); } It worked wow! thank you. thank you. thank you so much. $verbs = array( 'are', 'were' ); $sentence = 'you guys are great'; echo verbsInTheEnd($sentence,$verbs); //Outputs "you guys great are"
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: How To Rearrange Words In a Sentence ?
If you simply want a random re-ordering of the words, then once you've got an array of words as Syntac says, you can just use the shuffle() function on the array and implode it back.
To get verbs at the end, you really need to parse the sentnce properly to identify the verbs
To get verbs at the end, you really need to parse the sentnce properly to identify the verbs
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
Thank you all.
I have used preg match in my script, does anyone know the shortest and fastest way to match one or two words in a sentence (exact but not like a pattern within a word) ? A function would be awesome for all those who want to test a sentence for male or female etc.
// male words.........
$wordsToMatch=Array(
"he",
"man",
"men",
"boy",
"boys",
"boyfriend",
"brother",
"uncle",
"prince"
"father",
"grandfather"
)
/// given sentence
$sentence="he was nice yesterday and he is horrible today"
$isMale=false;
I use theselines... I know I'm dumb.
****************************
if ((preg_match("/\bhe\b/i", $sentence))
{
$isMale=True;
}
if ((preg_match("/\bman\b/i", $sentence))
{
$isMale=True;
}
if ((preg_match("/\bmen\b/i", $sentence))
{
$isMale=True;
}
and so on..................
is there a short way to do guys ? LOL !!
Go ahead, tell me i am dumb in php. You are right and i am wrong. LOL !!
Sorry to ask again. Thanks friends.
I have used preg match in my script, does anyone know the shortest and fastest way to match one or two words in a sentence (exact but not like a pattern within a word) ? A function would be awesome for all those who want to test a sentence for male or female etc.
// male words.........
$wordsToMatch=Array(
"he",
"man",
"men",
"boy",
"boys",
"boyfriend",
"brother",
"uncle",
"prince"
"father",
"grandfather"
)
/// given sentence
$sentence="he was nice yesterday and he is horrible today"
$isMale=false;
I use theselines... I know I'm dumb.
****************************
if ((preg_match("/\bhe\b/i", $sentence))
{
$isMale=True;
}
if ((preg_match("/\bman\b/i", $sentence))
{
$isMale=True;
}
if ((preg_match("/\bmen\b/i", $sentence))
{
$isMale=True;
}
and so on..................
is there a short way to do guys ? LOL !!
Go ahead, tell me i am dumb in php. You are right and i am wrong. LOL !!
Sorry to ask again. Thanks friends.
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
come on you all, what's stopping you from helping me. do you want me to do this ...
p l e a s e h e l p !
p l e a s e h e l p !
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: How To Rearrange Words In a Sentence ?
Code: Select all
foreach($wordsToMatch as $val)
{
if(!(strpos($sentence,$val)===false))
{
$isMale=true;
break;
}
}
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Re: How To Rearrange Words In a Sentence ?
novice4eva rocks! OMG. Thank you. Thank you. Thank you.novice4eva wrote:Code: Select all
foreach($wordsToMatch as $val) { if(!(strpos($sentence,$val)===false)) { $isMale=true; break; } }
- phpbaby2009
- Forum Commoner
- Posts: 31
- Joined: Sun Nov 16, 2008 8:20 pm
Split String Into An Array of Small Sentences by and or so c
i wanna thank all of you for helping me out. i keep getting stumbled. the word to put verbs in the end seems to work just fine as long as the sentence is simple. but what if you made the GRAMMER RULES by yourself. wow, guys.... i think you guys are the real coders.... Cheers!!! I know how the google language must work, converting one language to another and the hardest part would be take the grammer....
I think this thread looks like a great tool for creating functions that might help many people translate or manipulate strings.
# $sentenceSeperators=Array (
# 'or',
# 'and',
# 'but',
# 'so',
# 'therefore'
# 'hence'
# )
#
# function verbsInTheEnd($sentence,$verbs) {
# $wordArray = explode(' ',$sentence);
# foreach($wordArray as $key => $word) {
# if(in_array($word,$verbs)) {
# unset($wordArray[$key]);
# $wordArray[] = $word;
# }
# }
# return implode(' ',$wordArray);
# }
#
# $verbs = array(
# 'are',
# 'were'
# 'love'
# );
#
# $sentence = 'you guys are great and i love you';
#
# echo verbsInTheEnd($sentence,$verbs);
how can the above function look for the sentence breaker and put the verbs at the end of each short sentence not at the end of the whole sentence ? I guess I could that function after separating the sentence. is there a short way to do this. I was thinking of using the explode manually like this.
# $q="you guys are great and i love you"
#
# foreach ($sentenceSeperators as $key => $value)
# {
# $sentences=explode($value,$q);
# }
# foreach($sentences as $key => $value)
# $sentenceToTranslate=$sentenceToTranslate . verbsInTheEnd($value,$verbs);
# }
#
#
#
I tried this with some dummy knowledge i have and you know i don't know about php. am i doing right ???? please help and i bless you already! == please help and i you bless already lol !!

I think this thread looks like a great tool for creating functions that might help many people translate or manipulate strings.
# $sentenceSeperators=Array (
# 'or',
# 'and',
# 'but',
# 'so',
# 'therefore'
# 'hence'
# )
#
# function verbsInTheEnd($sentence,$verbs) {
# $wordArray = explode(' ',$sentence);
# foreach($wordArray as $key => $word) {
# if(in_array($word,$verbs)) {
# unset($wordArray[$key]);
# $wordArray[] = $word;
# }
# }
# return implode(' ',$wordArray);
# }
#
# $verbs = array(
# 'are',
# 'were'
# 'love'
# );
#
# $sentence = 'you guys are great and i love you';
#
# echo verbsInTheEnd($sentence,$verbs);
how can the above function look for the sentence breaker and put the verbs at the end of each short sentence not at the end of the whole sentence ? I guess I could that function after separating the sentence. is there a short way to do this. I was thinking of using the explode manually like this.
# $q="you guys are great and i love you"
#
# foreach ($sentenceSeperators as $key => $value)
# {
# $sentences=explode($value,$q);
# }
# foreach($sentences as $key => $value)
# $sentenceToTranslate=$sentenceToTranslate . verbsInTheEnd($value,$verbs);
# }
#
#
#
I tried this with some dummy knowledge i have and you know i don't know about php. am i doing right ???? please help and i bless you already! == please help and i you bless already lol !!