Skip to content

Instantly share code, notes, and snippets.

@uppfinnarjohnny
Created March 10, 2011 11:26
Show Gist options
  • Save uppfinnarjohnny/863966 to your computer and use it in GitHub Desktop.
Save uppfinnarjohnny/863966 to your computer and use it in GitHub Desktop.
<?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