[LON-CAPA-cvs] cvs: modules /gerd/harvesting recommender.pl

www www at source.lon-capa.org
Thu Oct 6 18:13:04 EDT 2011


www		Thu Oct  6 22:13:04 2011 EDT

  Added files:                 
    /modules/gerd/harvesting	recommender.pl 
  Log:
  Toy recommender system
  
  

Index: modules/gerd/harvesting/recommender.pl
+++ modules/gerd/harvesting/recommender.pl
#!/usr/bin/perl
use strict;
use HTML::Entities;

my $datapath='/home/www/loncapa/modules/gerd/harvesting/';
my $datahost='http://s10.lite.msu.edu';

$|=1;   # Autoflush after each print/writepath='/home/www/loncapa/modules/gerd/harvesting/';
print("Content-type: text/html\n\n");

my %env;
foreach (split(/&/,$ENV{'QUERY_STRING'})) {
    my ($name, $value) = split(/=/,$_);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    $env{'form.'.$name}=$value;
}

if ($env{'form.preview'}) {
   print("<html><frameset rows='50%,50%'>\n");
   print("<frame src='$datahost$env{'form.preview'}' />\n");
   print("<frame src='/cgi-bin/recommender.pl?focus=".&escape($env{'form.preview'})."' />\n");
   print("</frameset></html>\n");
   exit;
}


print("<html><body>");
print("Initializing ... \n");

# Read current basket

my %basket=();

open(IN,$datapath.'mycart.data') || print("Could not find basket ...\n");
while (my $line=<IN>) {
   chomp($line);
   $basket{$line}=1;
}
close(IN);

print("Resource data ...\n");
my @url=();

# Get resource URL, etc

open(IN,$datapath.'resource.dat') || print("Could not read resource ...\n");
while (my $line=<IN>) {
   my @entries=split(/\t/,$line);
   $url[$entries[0]]=$entries[4];
}
close(IN);

# Get keywords

print("Keywords ...\n");
my @keywords=();

my $i=0;

my $keystr='';
my ($id,$key)=(1,'');
open(IN,$datapath.'keywords.dat') || print("Could not read keywords ...\n");
while (my $line=<IN>) {
   my $oid=$id;
   chomp($line);
   ($id,$key)=split(/\t/,$line);
   if ($id!=$oid) {
      $keystr=~s/^\,//;
      $keywords[$oid]=$keystr;
      $keystr='';
   }
   $keystr.=','.lc($key);
}
close(IN);

# Commands
if ($env{'form.focus'}) {
  print "<h3>Focus: <tt>$env{'form.focus'}</tt></h3>\n";
} else {
# List basket

   print("\n<h3>Current Basket</h3>\n");
   foreach my $key (sort(keys(%basket))) {
      &outputlink($key,$basket{$key});
   }
}

print("</body></html>");
exit;

sub outputlink {
   my ($key,$url)=@_;
   print("<br /><tt>$key &nbsp<a href='$datahost/$url' target='preview'>$url</a>");
} 

sub escape {
   my ($arg)=@_;
   return &HTML::Entities::encode($arg,'"<>&')
}




More information about the LON-CAPA-cvs mailing list