-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.py
More file actions
66 lines (44 loc) · 1.46 KB
/
Copy pathscoring.py
File metadata and controls
66 lines (44 loc) · 1.46 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
import pandas as pd
# Load artifact once
df = pd.read_csv("comics_with_clusters_v1.csv")
df.columns = df.columns.str.strip()
CLUSTER_NAMES = {
0: "Mass-market / disposable-ish",
2: "Legacy-dense / collectible-ish",
}
def normalize_query(query: str) -> str:
return (query or "").strip()
def label_from_cluster(cluster_id: int) -> str:
if cluster_id == 2:
return "High (proxy)"
if cluster_id == 0:
return "Low (proxy)"
return "Medium (proxy)"
def find_matches(query: str):
hits = df[df["comic_name"].astype(str).str.contains(query, case=False, na=False)]
if len(hits) == 0:
hits = df[df["issue_title"].astype(str).str.contains(query, case=False, na=False)]
return hits
def choose_best_match(hits):
if len(hits) == 0:
return None
return hits.iloc[0].to_dict()
def build_result(row: dict) -> dict:
cluster_id = int(row.get("cluster", -1))
return {
"comic_name": row.get("comic_name", ""),
"issue_title": row.get("issue_title", ""),
"publish_date": row.get("publish_date", ""),
"cluster": cluster_id,
"cluster_name": CLUSTER_NAMES.get(cluster_id, "Unlabeled cluster"),
"label": label_from_cluster(cluster_id),
}
def score_comic(query: str):
query = normalize_query(query)
if not query:
return None
hits = find_matches(query)
row = choose_best_match(hits)
if row is None:
return None
return build_result(row)