Created
March 10, 2011 11:26
-
-
Save uppfinnarjohnny/863966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function word_pairs($string, $min_matches = 1) { | |
$pair_counts = array(); | |
$words = explode(' ', $string); | |
$previous_word = FALSE; | |
foreach($words as $word) { | |
if($previous_word) | |
$pair_counts[$previous_word.' '.$word]++; | |
$previous_word = $word; | |
} | |
$pair_counts = array_filter($pair_counts, function($count) use ($min_matches) { | |
return $count >= $min_matches; | |
}); | |
return $pair_counts; | |
} | |
$string = 'hej hopp hej hopp hej hej hej jag heter johnny vem mer heter johnny'; | |
print_r(word_pairs($string, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment