-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.py
More file actions
81 lines (71 loc) · 2.5 KB
/
Worker.py
File metadata and controls
81 lines (71 loc) · 2.5 KB
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
# Node Worker menggunakan ZeroMQ untuk memproses query
import zmq
import pymysql
from sklearn.feature_extraction.text import TfidfVectorizer
# Koneksi database lokal untuk worker
def get_db_connection():
return pymysql.connect(
host="localhost",
user="root",
password="",
database="db_se",
cursorclass=pymysql.cursors.DictCursor
)
def process_query(query):
connection = get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute("SELECT document_id, judul, isi_berita FROM documents")
documents = cursor.fetchall()
# TF-IDF Processing
texts = [doc['isi_berita'] for doc in documents]
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(texts)
query_vector = vectorizer.transform([query])
# Calculate cosine similarity
cosine_similarities = (tfidf_matrix * query_vector.T).toarray().flatten()
results = [
{
"document_id": doc["document_id"],
"judul": doc["judul"],
"content": doc["isi_berita"],
"device": "bagas",
"score": float(score),
}
for doc, score in zip(documents, cosine_similarities) if score > 0
]
return results
finally:
connection.close()
def main():
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555") # Worker mendengarkan di port 5555
print("Worker berjalan dan siap menerima query...")
while True:
message = socket.recv_json()
query = message.get("query")
if "query" in message:
query = message.get("query")
results = process_query(query)
socket.send_json({"results": results})
elif "document_id" in message:
document_id = message.get("document_id")
document = get_document_by_id(document_id)
socket.send_json({"document": document})
else:
socket.send_json({"error": "Invalid request"})
def get_document_by_id(document_id):
connection = get_db_connection()
try:
with connection.cursor() as cursor:
cursor.execute(
"SELECT document_id, judul, isi_berita FROM documents WHERE document_id = %s",
(document_id,)
)
document = cursor.fetchone()
return document
finally:
connection.close()
if __name__ == "__main__":
main()