tags:

views:

33

answers:

3

I'd like to,

  1. Check the word count for a folder full of text files.
  2. Output a list of the files arranged by word count in the format - FILENAME is WORDCOUNT

I know str_word_count is used to get individual wordcounts for files but I'm not sure how to rearrange the output.

Thanks in advance.

A: 

I don't use php but I would

  1. create array to hold filename and wordcount
  2. read through the folder full of text files and for each save the filename and wordcount to the array
  3. sort the array by wordcount
  4. output the array

To store the information (#2) I would put the information into a 2D array. There is more information about 2D arrays here at Free PHP Tutorial. Thus array[0][0] would equal the name of the first file and array0 would be the wordcount. array1[0] and array1 would be the for the next file.

To sort the array (#3) you can use the tutorial firsttube.com.

The to output I would do a loop through the array and output the first and second location.

for ($i = 0; $i < sizeof($array); ++$i) {
    print the filename ($array[$i][0]) and wordcount ($array[$i][1])
}
Kyra
My problem is step 3, how do I store the name of the file and the count together for output later?
usertest
@user201140 - just added more information to the answer. If you have anymore questions feel free to ask.
Kyra
+1  A: 

Adapted from here.

<?php
    $files = array();
    $it = new DirectoryIterator("/tmp");
    $it->rewind();
    while ($it->valid()) { 
        $count = str_word_count(file_get_contents($it->getFilename()));
        $files[sprintf("%010d", $count) . $it->getFilename()] =
            array($count, $it->getFilename()); 
        $it->next();
    }

    ksort($files);
    foreach ($files as $tup) {
        echo sprintf("%s is %d\n", $tup[1], $tup[0]);
    }

EDIT It would be more elegant to have $file's key be the file name and $file's value be the word count and then sort by value.

Artefacto
It outputs the files, but doesn't arrange them by wordcount
usertest
@user Are you sure? The initial version had a few errors I hopefully corrected.
Artefacto
I think the probem is that ksort arranges by key, which in this case is filename?
usertest
@user No, the key is the count. The filename is appended to the count so that the key is unique.
Artefacto
My mistake, thats it thanks
usertest
A: 

If you would like to keep the iterator-style approach (yet still do essentially the same as Artefacto's answer) then something like the following would suffice.

$dir_it = new FilesystemIterator("/tmp");
// Build array iterator with word counts
$arr_it = new ArrayIterator();
foreach ($dir_it as $fileinfo) {
    // Skip non-files
    if ( ! $fileinfo->isFile()) continue;
    $fileinfo->word_count = str_word_count(file_get_contents($fileinfo->getPathname()));
    $arr_it->append($fileinfo);
}
// Sort by word count descending
$arr_it->uasort(function($a, $b){
    return $b->word_count - $a->word_count;
});

// Display sorted files and their word counts
foreach ($arr_it as $fileinfo) {
    printf("%10d %s\n", $fileinfo->word_count, $fileinfo->getFilename());
}

Aside: If the files are particularly large (read: loading each one entirely into memory just to count the words is too much) then you could loop over the file line-by-line (or byte-by-byte if you really wanted to) with the SplFileObject.

salathe