-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scorer.php
executable file
·80 lines (71 loc) · 2.75 KB
/
Scorer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
require_once('Verb.php');
require_once('UserQiki.php');
// TODO: a simple scorer that just totalled up all the values in Sentence that matched the criteria
// TODO: another that would just count the number of such sentences
// TODO: another that did so normalized by some broader total
// e.g. so a user who rated lots of likes would find each one less potent
class Scorer {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function score($opts=array()) {
$opts += array(
/* Verb::classname() => $verb_id, */ //
/* UserQiki::classname() => $user_id, */ // pick one of these to specify the object, or none of these to sum over all objects
/* Sentence::classname() => $comment_id, */ //
'client_id' => NULL, // e.g. UserQiki::client()->id()
);
switch ($this->name) {
case 'spammy':
$verb = Verb::selectInfo('spam');
// TODO: MergeMorphMeld with Verb::associations() or something
$wheretests = array();
$queryparameters = array();
foreach (allNounClasses() as $nounclass) {
if (isset($opts[$nounclass])) {
$wheretests[] = "s.object_class = ? AND s.object_id = ?";
$queryparameters[] = $nounclass;
$queryparameters[] = $opts[$nounclass];
break;
}
}
$wheretests[] = "s.verb_id = ?";
$queryparameters[] = $verb->id();
$wheretests[] = "s.subject_class = ?";
$queryparameters[] = UserQiki::classname(); // Ignores ratings from anonymous users -- but still shows the icon!
$WHEREclause = empty($wheretests) ? '' : "WHERE " . join(' AND ', $wheretests);
$pdo = Verb::pdo();
$uservotes = $pdo->column("
SELECT
s.subject_id,
s.value
FROM ".Sentence::tablename()." AS s
$WHEREclause
", $queryparameters);
$isClientSpecified = !is_null($opts['client_id']);
$isClientLoggedIn = $isClientSpecified && $opts['client_id'] != User::NOUSERID;
$didClientScore = $isClientLoggedIn && isset($uservotes[$opts['client_id']]);
$scoreOfClient = $didClientScore ? $uservotes[$opts['client_id']] : 0.0;
// So, $scoreOfClient == 0.0 could mean:
// client says I dunno, or
// client never scored, or
// anonymous client, or
// calling function never specified a client
if ($scoreOfClient == 0.0) {
$countPositive = 0;
foreach ($uservotes as $uservote) {
if ($uservote > 0.0) {
$countPositive++;
}
}
$spamscore = $countPositive / 2.0; // if client has no opinion, it only matters how many others had a positive score, and half as much
} else {
$spamscore = $scoreOfClient; // to each person, their own (nonzero) rating is godlike
}
return $spamscore;
}
return 0.0;
}
}