Repository navigation
Expand file tree
/
Copy pathanalyze.py
More file actions
61 lines (54 loc) · 2.14 KB
/
Copy pathanalyze.py
File metadata and controls
61 lines (54 loc) · 2.14 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
"""Lightweight analysis on the stored corpus.
Surfaces candidate threads per taxonomy bucket via LIKE-based keyword hits.
This is NOT Pakka's detector — it's a retriever for human review.
"""
from __future__ import annotations
from collections import Counter
from typing import Optional
from store import Store
def keyword_hits(
store: Store,
taxonomy: dict[str, list[str]],
subreddit: Optional[str] = None,
per_term_limit: int = 5000,
top_n: int = 20,
) -> dict:
results: dict[str, dict] = {}
for bucket, terms in taxonomy.items():
hits_by_thread: Counter[str] = Counter()
seen_comment_ids: set[str] = set()
for term in terms:
rows = store.search_comments_like(term, subreddit=subreddit, limit=per_term_limit)
for r in rows:
cid = r["id"]
if cid in seen_comment_ids:
continue
seen_comment_ids.add(cid)
pid = r.get("post_id") or ""
if pid:
hits_by_thread[pid] += 1
results[bucket] = {
"total_hits": len(seen_comment_ids),
"top_threads": hits_by_thread.most_common(top_n),
}
return results
def format_surface(results: dict, store: Store) -> str:
lines: list[str] = []
for bucket, info in results.items():
lines.append(f"\n=== {bucket} (total comment hits: {info['total_hits']}) ===")
if not info["top_threads"]:
lines.append(" (no hits)")
continue
for post_id, count in info["top_threads"]:
row = store.conn.execute(
"SELECT subreddit, title, permalink FROM posts WHERE id = ?",
(post_id,),
).fetchone()
if row:
title = (row["title"] or "")[:90]
permalink = row["permalink"] or f"/r/?/comments/{post_id}/"
lines.append(f" [{count}x] r/{row['subreddit']} {title}")
lines.append(f" https://www.reddit.com{permalink}")
else:
lines.append(f" [{count}x] post_id={post_id} (post not in DB)")
return "\n".join(lines)