php create word pairs from sentence

Solutions on MaxInterview for php create word pairs from sentence by the best coders in the world

showing results for - "php create word pairs from sentence"
Valentina
22 Jun 2018
1/*this will get all word pairs next to eachother */
2$sentence = "the cat sat down outside";
3$sentenceArr = explode(" ",$sentence);
4$wordPairs = array();
5for($i=0;$i<count($sentenceArr)-1;$i++) {
6      $wordPairs[] =  $sentenceArr[$i].'_'.$sentenceArr[$i+1];
7}
8
9
10/*This will get all pairs (ie: all culmintions of two words)*/
11$sentence = "the cat sat down outside";                                               
12$sentenceArr = explode(" ",$sentence);                                                
13$wordPairs = array();                                                                 
14foreach($sentenceArr as $kw1=>$word1){                                                
15    for($j=($kw1+1);$j<count($sentenceArr);$j++) {                                    
16        $wordPairs[]=$word1."_".$sentenceArr[$j];                                     
17    }                                                                                 
18}