Universität Ulm, Fakultät für Mathematik und Wirtschaftswissenschaften, SAI

# Lösung zu Blatt 4 --- Software Engineering Praxis (SS 2002)

## Was mag ich denn?

```#!/usr/local/bin/perl -w

use strict;

sub vq {
my \$res = 0;
return 0 if scalar(@_) == 0;
foreach (@_) {
\$res += \$_;
}
return \$res / scalar(@_);
}

sub p {
my (\$user, \$film, \$votes) = @_;
my (\$k, \$sum, \$w) = (0, 0, 0);
next unless defined \$cvotes->{\$film};        # Film bewertet?
\$k += abs(\$w);
}
return 0 if \$k == 0;
return vq(values %{\$votes->{\$user}}) + \$sum / \$k;
}

sub w {  # Pearson
my (\$user, \$cuser, \$votes) = @_;
return 0 unless (\$vq1 and \$vq2);
my (\$num, \$denum1, \$denum2) = (0, 0, 0);
foreach my \$films (keys %{\$votes1}) {
\$denum1 += (\$votes1->{\$films} - \$vq1) ** 2;
\$denum2 += (\$votes2->{\$films} - \$vq2) ** 2;
}
return 0 if (\$denum1 * \$denum2) == 0;
return \$num * (\$denum1 * \$denum2) ** (-0.5);
}

my (\$file) = @_;
open(FH, "\$file") or die "Cannot open \$file\n";
my %user = ();
while (<FH>) {
chomp;
my (\$name, \$film, \$vote) = split /\|/;
\$user{\$name}->{\$film} = \$vote;
}
close FH;
return \%user;
}