• Guys/gals,

    I’m trying to figure out how to shape a query that will do this:

    1. For all of author A’s posts over the last 1 year:
    2. Take the word count of each post, W.
    3. Take the square root of that word count sqrt(W).
    4. Sum all of those values together sum(sqrt(W)).

    This should result in one number for each author. Does anyone have any ideas?

    (This is part of an algorithm we’ll be using to compensate our writers.)

Viewing 3 replies - 1 through 3 (of 3 total)
  • Put this in your functions.php file and then call it from wherever.

    function word_count($text) {
        # There's a lot of things you could do here.
        # This just strips out the HTML,
        # splits on spaces and punctuation,
        # and returns the size of the array. Ie. the number of words.
        $tmp = strip_tags($text);
        $arr = preg_split("/[\s.?!\"']+/", $tmp);
        return count($arr);
    }
    
    function compute_author_score($name) {
        global $wpdb;
        $user = get_userdatabylogin($name);
        $posts = $wpdb->get_results("SELECT * from $wpdb->posts WHERE post_author = $user->ID AND post_type = 'post'");
        $total = 0;
        foreach ($posts as $post) {
            $total += sqrt(word_count($post->post_content));
        }
        # for testing
        # print "$name posts: " . count($posts) . " score: $total\n"; # or whatever
        return $total;
    }
    Thread Starter mgroup

    (@mgroup)

    Hedronist,

    Maybe I’m just tired, but doesn’t this sqrt the total word count, instead of totaling the sqrts of each respective post?

    Uhm, I don’t think so … but then again I am suffering from a killer sinus condition right now.

    Let’s take a look. Get the user (check). Get the user’s posts (check). Set the total to 0 (check). For each post (check) get the content (check) get it’s word count (check) take the sqrt() of the word count (check) and add it to the total (check).

    Looks good to me. Why not try it with a few test cases you do by hand?

    (Note bene: I am 2/3 of the way through a very alcoholic hot toddy, so all bets are off! (check!) ?? )

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Having some fun with word counts’ is closed to new replies.