-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarize.py
More file actions
45 lines (39 loc) · 1.5 KB
/
summarize.py
File metadata and controls
45 lines (39 loc) · 1.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
import argparse, csv, json, datetime
from collections import defaultdict, Counter
def load_schema(p):
with open(p,'r',encoding='utf-8') as f: return json.load(f)
def tag_text(text, schema):
text_l = text.lower(); preds=set()
for tag,kws in schema.items():
for kw in kws:
if kw in text_l: preds.add(tag); break
return preds
def main():
ap=argparse.ArgumentParser()
ap.add_argument('--notes', required=True)
ap.add_argument('--schema', default='tag_schema.json')
ap.add_argument('--out', required=True)
args=ap.parse_args()
schema = load_schema(args.schema)
rows = list(csv.DictReader(open(args.notes,encoding='utf-8')))
weeks = defaultdict(list)
for r in rows:
d = datetime.date.fromisoformat(r['date'])
year,week,_ = d.isocalendar()
weeks[(year,week)].append(r)
lines=[]
for (year,week), items in sorted(weeks.items()):
lines.append(f"# Week {year}-W{week}\n\n")
tag_counts = Counter()
for it in items:
tag_counts.update(tag_text(it['note'], schema))
lines.append("## Tag highlights\n")
for t,c in tag_counts.most_common():
lines.append(f"- {t}: {c}\n")
lines.append("\n## Notes\n")
for it in items:
lines.append(f"- {it['date']} ({it['child']}): {it['note']}\n")
lines.append("\n---\n\n")
with open(args.out,'w',encoding='utf-8') as f: f.write(''.join(lines))
print(f"Wrote {args.out}")
if __name__=='__main__': main()