Page 1 of 1

How To Rearrange Words In a Sentence ?

Posted: Sun Nov 16, 2008 8:31 pm
by phpbaby2009
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.

Re: How To Rearrange Words In a Sentence ?

Posted: Sun Nov 16, 2008 8:57 pm
by Syntac
Try explode()-ing it with a space as the delimiter, switching stuff around, and implode()-ing it back?

Re: How To Rearrange Words In a Sentence ?

Posted: Sun Nov 16, 2008 9:01 pm
by phpbaby2009
Syntac wrote:Try explode()-ing it with a space as the delimiter, switching stuff around, and implode()-ing it back?
OMG!!! This is the first time i have got such a fast response .Thank you Syntac and this forum. AWESOME.

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 ?

Posted: Sun Nov 16, 2008 9:16 pm
by Syntac
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 ?

Posted: Sun Nov 16, 2008 9:20 pm
by Eran
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:

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"
 
Of course you can add as many verbs to that array as you would like.

Re: How To Rearrange Words In a Sentence ?

Posted: Sun Nov 16, 2008 9:34 pm
by phpbaby2009
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.

Re: How To Rearrange Words In a Sentence ?

Posted: Sun Nov 16, 2008 9:41 pm
by phpbaby2009
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:

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"
 
Of course you can add as many verbs to that array as you would like.

Re: How To Rearrange Words In a Sentence ?

Posted: Mon Nov 17, 2008 2:56 am
by Mark Baker
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

Re: How To Rearrange Words In a Sentence ?

Posted: Sat Nov 22, 2008 12:57 pm
by phpbaby2009
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.

Re: How To Rearrange Words In a Sentence ?

Posted: Sat Nov 22, 2008 6:43 pm
by phpbaby2009
come on you all, what's stopping you from helping me. do you want me to do this ...

:banghead: :banghead: :banghead: :banghead: :banghead: :banghead:

p l e a s e h e l p !

Re: How To Rearrange Words In a Sentence ?

Posted: Sun Nov 23, 2008 12:06 am
by novice4eva

Code: Select all

 
foreach($wordsToMatch as $val)
{
   if(!(strpos($sentence,$val)===false))
   {
       $isMale=true; 
       break;
   }
}
 

Re: How To Rearrange Words In a Sentence ?

Posted: Tue Nov 25, 2008 6:07 pm
by phpbaby2009
novice4eva wrote:

Code: Select all

 
foreach($wordsToMatch as $val)
{
   if(!(strpos($sentence,$val)===false))
   {
       $isMale=true; 
       break;
   }
}
 
novice4eva rocks! OMG. Thank you. Thank you. Thank you. :drunk:

Split String Into An Array of Small Sentences by and or so c

Posted: Wed Dec 03, 2008 8:23 pm
by phpbaby2009
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.... :banghead:

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 !! :oops: :oops: :oops: :oops: :oops: :oops: