Last active
December 17, 2015 23:28
-
-
Save benclark/5688984 to your computer and use it in GitHub Desktop.
Find out which words are most commonly used in song lyrics.
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 | |
/** | |
* @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