Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php create word pairs from sentence

/*this will get all word pairs next to eachother */
$sentence = "the cat sat down outside";
$sentenceArr = explode(" ",$sentence);
$wordPairs = array();
for($i=0;$i<count($sentenceArr)-1;$i++) {
      $wordPairs[] =  $sentenceArr[$i].'_'.$sentenceArr[$i+1];
}


/*This will get all pairs (ie: all culmintions of two words)*/
$sentence = "the cat sat down outside";                                               
$sentenceArr = explode(" ",$sentence);                                                
$wordPairs = array();                                                                 
foreach($sentenceArr as $kw1=>$word1){                                                
    for($j=($kw1+1);$j<count($sentenceArr);$j++) {                                    
        $wordPairs[]=$word1."_".$sentenceArr[$j];                                     
    }                                                                                 
} 
 
PREVIOUS NEXT
Tagged: #php #create #word #pairs #sentence
ADD COMMENT
Topic
Name
2+9 =