File size: 1,431 Bytes
a6f7c7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
example.py

Minimal usage example for hv-temporal-context.
"""

from hv_temporal_context import HVTemporalContext


def main():
    model = HVTemporalContext.load("temporal_context_model")

    queries = [
        ("what is the forecast for rome", "weather"),
        ("how do i debug a python function", "coding"),
        ("how much did i spend on groceries", "finance"),
        ("how do i cook soup", "cooking"),
    ]

    print("Pure similarity retrieval:")
    print()
    for q, _ in queries:
        idx, scores = model.retrieve(q, k=3)
        print(f"  {q}")
        for i, (j, s) in enumerate(zip(idx, scores)):
            print(f"    top{i+1} [{model.topics[j]:<10}] sim={s:.3f}")
        print()

    print("Position-conditional retrieval (with topic hint):")
    print()
    for q, topic in queries:
        idx, scores = model.retrieve_temporal(q, topic, k=3)
        print(f"  {q}  (hint: {topic})")
        for i, (j, s) in enumerate(zip(idx, scores)):
            print(f"    top{i+1} [{model.topics[j]:<10}] sim={s:.3f}")
        print()

    print("Topic-shift detection:")
    print()
    boundaries = model.topic_boundaries()
    print(f"  detected boundaries at positions: {boundaries[:10]}")
    print(f"  total boundaries: {len(boundaries)}")
    print(f"  median surprise magnitude: "
          f"{float(__import__('numpy').median(model.surprise_mags)):.3f}")


if __name__ == "__main__":
    main()