Skip to content

Instantly share code, notes, and snippets.

@benclark
Last active December 17, 2015 23:28
Show Gist options
  • Save benclark/5688984 to your computer and use it in GitHub Desktop.
Save benclark/5688984 to your computer and use it in GitHub Desktop.
Find out which words are most commonly used in song lyrics.
<?php
/**
* @file
* Find out which words are most commonly used in song lyrics.
*/
$wordcounts = array();
$lines = file('album/song.txt');
foreach ($lines as $line_num => $line) {
$line = strtolower($line);
$words = split(' ', $line);
foreach ($words as $word) {
$word = preg_replace('/[^\w]*/', '', $word);
$wordcounts[$word]++;
}
}
asort($wordcounts);
foreach ($wordcounts as $word => $wordcount) {
echo "$word, $wordcount\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment