forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgap_cluster.py
More file actions
137 lines (111 loc) · 4.37 KB
/
Copy pathgap_cluster.py
File metadata and controls
137 lines (111 loc) · 4.37 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
#!/usr/bin/env python3
"""Cluster zero-result search queries to identify content gaps.
Reads search_telemetry.jsonl and groups similar queries together.
Outputs top clusters ranked by frequency.
Usage:
python scripts/gap_cluster.py --top 20
python scripts/gap_cluster.py --input ~/.misakanet/search_telemetry.jsonl --output data/gap_clusters.json
"""
import argparse
import json
import re
from collections import Counter
from pathlib import Path
def normalize_query(query: str) -> str:
"""Normalize query for clustering."""
q = query.lower().strip()
q = re.sub(r'[^\w\s]', ' ', q) # Remove punctuation
q = re.sub(r'\s+', ' ', q) # Collapse whitespace
return q.strip()
def ngram_similarity(a: str, b: str, n: int = 2) -> float:
"""Simple n-gram similarity between two strings."""
if not a or not b:
return 0.0
def ngrams(s, n):
return set(s[i:i+n] for i in range(len(s) - n + 1))
a_ng = ngrams(a, n)
b_ng = ngrams(b, n)
if not a_ng or not b_ng:
return 0.0
return len(a_ng & b_ng) / len(a_ng | b_ng)
def cluster_queries(queries: list[str], threshold: float = 0.3) -> list[dict]:
"""Cluster similar queries together."""
normalized = [(q, normalize_query(q)) for q in queries]
clusters = []
used = set()
for i, (orig_i, norm_i) in enumerate(normalized):
if i in used:
continue
cluster = [orig_i]
used.add(i)
for j, (orig_j, norm_j) in enumerate(normalized):
if j in used:
continue
if ngram_similarity(norm_i, norm_j) >= threshold:
cluster.append(orig_j)
used.add(j)
clusters.append({
"queries": cluster,
"count": len(cluster),
"representative": Counter(cluster).most_common(1)[0][0],
})
return sorted(clusters, key=lambda c: c["count"], reverse=True)
def main():
parser = argparse.ArgumentParser(description="Cluster zero-result search queries")
parser.add_argument("--input", type=str, default="~/.misakanet/search_telemetry.jsonl",
help="Path to search_telemetry.jsonl")
parser.add_argument("--output", type=str, default="data/gap_clusters.json",
help="Output path for clusters")
parser.add_argument("--top", type=int, default=20,
help="Number of top clusters to show")
parser.add_argument("--threshold", type=float, default=0.3,
help="Similarity threshold for clustering (0-1)")
args = parser.parse_args()
input_path = Path(args.input).expanduser()
if not input_path.exists():
print(f"❌ Input file not found: {input_path}")
print(" Run some searches first to generate telemetry data.")
return 1
# Read queries
queries = []
with open(input_path, "r", encoding="utf-8") as f:
for line in f:
try:
entry = json.loads(line.strip())
if entry.get("result") == "zero":
queries.append(entry["query"])
except json.JSONDecodeError:
continue
if not queries:
print("✅ No zero-result queries found.")
return 0
print(f"📊 Found {len(queries)} zero-result queries")
print()
# Cluster
clusters = cluster_queries(queries, threshold=args.threshold)
# Show top clusters
print(f"Top {min(args.top, len(clusters))} content gaps:")
print("-" * 60)
for i, cluster in enumerate(clusters[:args.top], 1):
print(f"{i:2d}. [{cluster['count']:3d} queries] {cluster['representative']}")
if cluster['count'] > 1:
# Show a few example queries
examples = list(set(cluster['queries']))[:3]
for ex in examples:
if ex != cluster['representative']:
print(f" → {ex}")
# Save to file
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump({
"total_queries": len(queries),
"total_clusters": len(clusters),
"threshold": args.threshold,
"clusters": clusters[:args.top],
}, f, indent=2, ensure_ascii=False)
print()
print(f"💾 Saved to {output_path}")
return 0
if __name__ == "__main__":
exit(main())