Favorites/ScoringCode

From Exalted - Unofficial Wiki
Revision as of 01:29, 11 February 2007 by Wordman (talk)
Jump to: navigation, search

This is the Perl code used to calculate the Favorites list. It assumes the existence of *.txt files containing the wiki text of the root favorites page and the pages to which it links. Creating such files is left as an exercise for the reader.

#!/usr/bin/perl
#Author: Wordman

use Time::Local;
use strict;

my %seen;

my $rootpath='/path/to/files';
my $contents="$rootpath/content";

my $rootfile="$contents/Favorites.txt";

my $now = localtime time;
print "Run begins: $now\n";

my @links;
get_bullet_links($rootfile,\@links);

my %results;
foreach my $link (@links)
{
  my $path = "$contents/$link.txt";
  my %favs;
  count_favorites($path,\%favs);
  
  foreach my $fav (keys %favs)
  {
    $results{$fav} ||= 0;
    $results{$fav} += 1;
    print "Counting vote from $link for $fav\n";
  }
}

# Now build an inverted result map
my %groupres;
foreach my $fav (keys %results)
{
  my $count = $results{$fav};
  $groupres{$count}{$fav} = '';
}

# Dump results
foreach my $count (reverse sort { $a <=> $b } keys %groupres)
{
  print ":'''Favorite of $count users:'''\n";
  my $list = $groupres{$count};
  foreach my $fav (sort keys %$list)
  {
    print "::[[$fav]]\n";
  }
}

sub get_bullet_links
{
  my ($path,$list) = @_;
  
  if (open(DAT, $path))
  {
    my @existing=<DAT>;
    close(DAT);

    foreach my $line (@existing)
    {
      my $link = extract_bullet_link($line);
      next if !$link;
      push(@$list,$link);
    }
  }  
}

sub extract_bullet_link
{
  my ($line) = @_;
  chomp $line;
  my $test = $line;
  $test =~ s/^\*+\s*(\[\[)?([\w\/-]+).*/$2/;
  return $line eq $test ? "" : $test;
}

sub count_favorites
{
  my ($path,$favs) = @_;
  my @links;
  get_bullet_links($path,\@links);
  
  # Count each link only once
  foreach my $link (@links)
  {
    $favs->{$link} = 1;
  }
}