-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsource_lookup.py
More file actions
155 lines (130 loc) · 5.1 KB
/
Copy pathsource_lookup.py
File metadata and controls
155 lines (130 loc) · 5.1 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
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
#!/usr/bin/env python3
"""Lookup source-backed benchmark claims from sources.json."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Any
REPO = Path(__file__).resolve().parents[1]
SOURCES = REPO / "sources.json"
OUTPUT_FIELDS = ("id", "claim", "evidence_class", "source", "date", "url", "scope", "status", "tags")
FIELD_WEIGHTS = {
"id": 5,
"claim": 4,
"tags": 4,
"source": 2,
"scope": 2,
"evidence_class": 1,
"date": 1,
}
ALIASES = {
"ab": ["a/b", "experiment", "test"],
"abtest": ["a/b", "experiment", "test"],
"apple": ["app store", "ios", "storekit"],
"complaint": ["complaints", "refund", "billing"],
"conversion": ["convert", "paid", "purchase"],
"design": ["paywall", "screen", "visual", "copy"],
"geo": ["region", "market", "localization", "pricing"],
"localize": ["localization", "locale", "market"],
"plan": ["plans", "product", "products"],
"plans": ["plan", "product", "products"],
"product": ["plan", "plans", "products"],
"products": ["plan", "plans", "product"],
"refund": ["billing", "failure", "failures", "cancellations", "complaint", "complaints"],
"rejection": ["reject", "rejected", "guideline", "3.1.2"],
"trial": ["free trial", "trial-to-paid", "trial paid"],
"weekly": ["week", "7-day"],
}
def load_sources(path: Path = SOURCES) -> list[dict[str, Any]]:
data = json.loads(path.read_text())
return list(data.get("benchmarks", []))
def normalize_entry(entry: dict[str, Any]) -> dict[str, Any]:
return {field: entry.get(field) for field in OUTPUT_FIELDS}
def expand_terms(query: str) -> list[str]:
raw_terms = [term.lower() for term in query.replace("-", " ").split() if term.strip()]
terms: list[str] = []
for term in raw_terms:
terms.append(term)
terms.extend(ALIASES.get(term, []))
if len(raw_terms) > 1:
terms.append(" ".join(raw_terms))
return list(dict.fromkeys(terms))
def score_entry(entry: dict[str, Any], terms: list[str]) -> int:
score = 0
for field, weight in FIELD_WEIGHTS.items():
value = entry.get(field, "")
if isinstance(value, list):
haystack = " ".join(str(item) for item in value).lower()
else:
haystack = str(value).lower()
for term in terms:
if term in haystack:
score += weight
return score
def lookup_sources(
*,
source_id: str | None = None,
query: str | None = None,
evidence_class: str | None = None,
status: str | None = "active",
limit: int = 10,
path: Path = SOURCES,
) -> list[dict[str, Any]]:
entries = load_sources(path)
if status:
entries = [entry for entry in entries if entry.get("status") == status]
if evidence_class:
entries = [entry for entry in entries if entry.get("evidence_class") == evidence_class]
if source_id:
entries = [entry for entry in entries if entry.get("id") == source_id]
if query:
terms = expand_terms(query)
scored = [(score_entry(entry, terms), entry) for entry in entries]
entries = [entry for score, entry in sorted(scored, key=lambda item: item[0], reverse=True) if score > 0]
return [normalize_entry(entry) for entry in entries[:limit]]
def render_text(results: list[dict[str, Any]]) -> str:
if not results:
return "No matching sources found."
blocks = []
for entry in results:
blocks.append(
"\n".join(
[
f"id: {entry['id']}",
f"claim: {entry['claim']}",
f"evidence_class: {entry['evidence_class']}",
f"source: {entry['source']}",
f"date: {entry['date']}",
f"url: {entry['url']}",
f"scope: {entry['scope']}",
]
)
)
return "\n\n".join(blocks)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Lookup source-backed benchmark claims.")
parser.add_argument("--id", dest="source_id", help="Exact source id.")
parser.add_argument("--query", help="Search terms for claim/source/scope/id.")
parser.add_argument("--evidence-class", help="Filter by evidence_class.")
parser.add_argument("--status", default="active", help="Filter by status. Use empty string for all.")
parser.add_argument("--limit", type=int, default=10, help="Maximum results.")
parser.add_argument("--json", action="store_true", help="Emit JSON.")
args = parser.parse_args(argv)
if not args.source_id and not args.query and not args.evidence_class:
parser.error("provide --id, --query, or --evidence-class")
status = args.status or None
results = lookup_sources(
source_id=args.source_id,
query=args.query,
evidence_class=args.evidence_class,
status=status,
limit=args.limit,
)
if args.json:
print(json.dumps(results, indent=2, sort_keys=True))
else:
print(render_text(results))
return 0
if __name__ == "__main__":
sys.exit(main())