-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
227 lines (182 loc) · 7.23 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
error_reporting(0);
require_once 'vendor/autoload.php';
use Sastrawi\Stemmer\StemmerFactory;
class DocumentIndexer
{
private $conn;
private $stemmer;
public function __construct($servername, $username, $password, $dbname)
{
$this->conn = new mysqli($servername, $username, $password, $dbname);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
$stemmerFactory = new StemmerFactory();
$this->stemmer = $stemmerFactory->createStemmer();
}
public function indexDocuments($documents)
{
foreach ($documents as $content) {
$processedContent = $this->preprocessText($content);
$processedContentEscaped = mysqli_real_escape_string($this->conn, $processedContent);
if ($this->getExistingDocument($processedContentEscaped)) {
continue;
}
$document_id = $this->insertDocument($processedContentEscaped);
$terms = array_unique(array_filter(array_map('strtolower', preg_split("/[^a-zA-Z0-9]+/", $processedContent))));
foreach ($terms as $term) {
$termEscaped = mysqli_real_escape_string($this->conn, $term);
if (!$term_id = $this->getTermId($termEscaped)) {
$term_id = $this->insertTerm($termEscaped);
}
$tfidf = $this->calculateTfidf($processedContent, $term, count($documents));
$this->saveToDocumentTerms($document_id, $term_id, $tfidf);
}
}
}
private function preprocessText($content) {
$content = strtolower($content);
$content = preg_replace("/[^a-zA-Z0-9\s]/", "", $content);
$content = $this->removeStopWords($content);
$content = $this->stemText($content);
return $content;
}
private function removeStopWords($content) {
$stopWords = ["dan", "di", "yang", "untuk", "pada", "ke", "dengan"]; // Tambahkan stop words yang relevan
$words = explode(" ", $content);
$filteredWords = array_diff($words, $stopWords);
return implode(" ", $filteredWords);
}
private function stemText($content) {
$words = explode(" ", $content);
$stemmedWords = array_map(function($word) {
return $this->stemmer->stem($word);
}, $words);
return implode(" ", $stemmedWords);
}
private function getExistingDocument($content)
{
$stmt = $this->conn->prepare("SELECT id FROM documents WHERE content = ?");
$stmt->bind_param("s", $content);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
private function insertDocument($content)
{
$stmt = $this->conn->prepare("INSERT INTO documents (content) VALUES (?)");
$stmt->bind_param("s", $content);
$stmt->execute();
return $this->conn->insert_id;
}
private function getTermId($term)
{
$stmt = $this->conn->prepare("SELECT id FROM terms WHERE term = ?");
$stmt->bind_param("s", $term);
$stmt->execute();
$existingTerm = $stmt->get_result()->fetch_assoc();
if ($existingTerm) {
return $existingTerm['id'];
} else {
return false;
}
}
private function insertTerm($term)
{
$stmt = $this->conn->prepare("INSERT INTO terms (term) VALUES (?)");
$stmt->bind_param("s", $term);
$stmt->execute();
return $this->conn->insert_id;
}
private function calculateTfidf($content, $term, $totalDocuments)
{
$tf = substr_count($content, $term) / str_word_count($content);
$documentCount = $this->getDocumentCountForTerm($term);
$idf = log($totalDocuments / ($documentCount + 1));
return $tf * $idf;
}
private function saveToDocumentTerms($document_id, $term_id, $tfidf)
{
$stmt = $this->conn->prepare("INSERT INTO document_terms (document_id, term_id, tfidf) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE tfidf = VALUES(tfidf)");
$stmt->bind_param("iid", $document_id, $term_id, $tfidf);
$stmt->execute();
}
private function getDocumentCountForTerm($term)
{
$stmt = $this->conn->prepare("SELECT COUNT(*) as document_count FROM document_terms JOIN terms ON document_terms.term_id = terms.id WHERE terms.term = ?");
$stmt->bind_param("s", $term);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
return $result['document_count'];
}
public function searchDocuments($searchTerm)
{
$searchTerm = $this->preprocessText($searchTerm);
$searchTermEscaped = mysqli_real_escape_string($this->conn, $searchTerm);
$terms = array_unique(array_filter(array_map('strtolower', preg_split("/[^a-zA-Z0-9]+/", $searchTermEscaped))));
$documentScores = $this->calculateDocumentScores($terms);
arsort($documentScores);
return $this->fetchDocumentResults($documentScores);
}
private function calculateDocumentScores($terms)
{
$documentScores = array();
foreach ($terms as $term) {
$termEscaped = mysqli_real_escape_string($this->conn, $term);
$stmt = $this->conn->prepare("SELECT document_id, SUM(tfidf) AS score FROM document_terms JOIN terms ON document_terms.term_id = terms.id WHERE terms.term = ? GROUP BY document_id");
$stmt->bind_param("s", $termEscaped);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$document_id = $row['document_id'];
$score = $row['score'];
if (!isset($documentScores[$document_id])) {
$documentScores[$document_id] = 0;
}
$documentScores[$document_id] += $score;
}
}
return $documentScores;
}
private function fetchDocumentResults($documentScores)
{
$results = array();
foreach (array_keys($documentScores) as $document_id) {
$stmt = $this->conn->prepare("SELECT * FROM documents WHERE id = ?");
$stmt->bind_param("i", $document_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
if ($result) {
$result['score'] = $documentScores[$document_id];
$results[] = $result;
}
}
return $results;
}
public function closeConnection()
{
$this->conn->close();
}
}
// Example usage
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "p";
$documentIndexer = new DocumentIndexer($servername, $username, $password, $dbname);
$documents = array(
"Kucing suka bermain.",
"Anjing suka berlari di taman.",
"Burung suka terbang tinggi.",
"memberi makan anak kambing yang lapar"
);
$documentIndexer->indexDocuments($documents);
$searchTerm = "suka bermain";
$results = $documentIndexer->searchDocuments($searchTerm);
foreach ($results as $result) {
echo "Document ID: " . $result['id'] . "<br>";
echo "Content: " . $result['content'] . "<br>";
echo "Score: " . $result['score'] . "<br><br>";
}
$documentIndexer->closeConnection();
?>