-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
149 lines (118 loc) · 3.14 KB
/
app.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* Bayes classifier
* @author: Alouit Alex <alex@alouit.fr>
*/
$minimum_length = 3;
$useless_words = array(
'le',
'la',
'et',
'par',
'pour',
'ton',
'ta',
'vos',
'votre',
'il',
'qui',
'quoi',
'un',
'une'
);
// look for learn
foreach (array('spam', 'ham') as $type) {
$director = new RecursiveDirectoryIterator(__DIR__ . "/learn/{$type}");
$iterator = new RecursiveIteratorIterator($director, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $fileInfo) {
if (! $fileInfo->isFile()) continue;
$words = preg_split(
'/\s+/',
strtolower(
file_get_contents(
$fileInfo->getPathname()
)
)
);
$words = array_filter($words, function ($word) use ($minimum_length, $useless_words) {
if (strlen($word) < $minimum_length)
return false;
if (in_array($word, $useless_words))
return false;
if (! preg_match("/^([a-z]+)$/i", $word))
return false;
return true;
});
foreach ($words as $word) {
$counter = 1;
if (file_exists(__DIR__ . "/corpus/{$type}/{$word}")) {
// increment counter
$counter += (int) file_get_contents(__DIR__ . "/corpus/{$type}/{$word}");
}
file_put_contents(__DIR__ . "/corpus/{$type}/{$word}", (string) $counter);
}
rename(
$fileInfo->getPathname(),
str_replace(
'/learn/',
'/learned/',
$fileInfo->getPathname()
)
);
}
}
// decide what to do with stdin
// count documents
$data = array(
'documents' => 0,
'words' => 0
);
$director = new RecursiveDirectoryIterator(__DIR__ . "/corpus");
$iterator = new RecursiveIteratorIterator($director, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $fileInfo) {
if (! $fileInfo->isFile()) continue;
$data['words']++;
}
foreach (array('spam', 'ham') as $type) {
$data[$type]['total'] = 0;
$data[$type]['score'] = 0;
$director = new RecursiveDirectoryIterator(__DIR__ . "/learned/{$type}");
$iterator = new RecursiveIteratorIterator($director, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $fileInfo) {
if (! $fileInfo->isFile()) continue;
$data[$type]['total']++;
$data['documents']++;
}
}
foreach (array('spam', 'ham') as $type) {
$data[$type]['score'] = log($data[$type]['total'] / $data['documents']);
}
$words = preg_split(
'/\s+/',
strtolower(
file_get_contents(
'php://stdin'
)
)
);
$words = array_filter($words, function ($word) use ($minimum_length, $useless_words) {
if (strlen($word) < $minimum_length)
return false;
if (in_array($word, $useless_words))
return false;
if (! preg_match("/^([a-z]+)$/i", $word))
return false;
return true;
});
foreach ($words as $word) {
foreach (array('spam', 'ham') as $type) {
$count = 0;
if (file_exists(__DIR__ . "/corpus/{$type}/{$word}")) {
$count += (int) file_get_contents(__DIR__ . "/corpus/{$type}/{$word}");
}
$data[$type]['score'] += log(($count + 1) / ($data[$type]['total'] + $data['words']));
}
}
if ($data['ham']['score'] >= $data['spam']['score'])
return print "pass";
return print "fail";