File size: 2,265 Bytes
1da7ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# tests/test_encode.py
import numpy as np

from fractus_vorax.compiler.atoms import Atom
from fractus_vorax.compiler.encode import (
    char_ngrams,
    encode_atom,
    normalize,
    slot_hv,
    text_hv,
    tokenize,
)
from fractus_vorax.hv import sim


def test_normalize():
    assert normalize("  What   IS The   Capital?  ") == "what is the capital"


def test_tokenize():
    assert tokenize("What is the capital of France?") == ["what", "is", "the", "capital", "of", "france"]


def test_text_hv_identical_sentences():
    a = text_hv("what is the capital of france")
    b = text_hv("What IS the capital of France?")
    assert np.array_equal(a, b)


def test_related_more_similar_than_unrelated():
    q1 = text_hv("what is the capital of france")
    q2 = text_hv("what is the capital of spain")
    far = text_hv("quantum entanglement violates locality")
    assert sim(q1, q2) > 0.3          # 5 tokens partagés sur 6
    assert sim(q1, far) < 0.2         # quasi-orthogonal
    assert sim(q1, q2) > sim(q1, far)


def test_encode_atom_deterministic():
    atom = Atom("what is the capital of france", "paris", "cap.csv:2")
    a1, v1 = encode_atom(atom)
    a2, v2 = encode_atom(atom)
    assert np.array_equal(a1, a2) and np.array_equal(v1, v2)


def test_encode_atom_addr_is_statement():
    atom = Atom("who wrote hamlet", "william shakespeare", "s:1")
    addr, value = encode_atom(atom)
    assert np.array_equal(addr, text_hv("who wrote hamlet"))
    assert not np.array_equal(value, addr)  # le contexte change la valeur


def test_encode_atom_no_context_value_equals_addr():
    atom = Atom("un fait nu", "", "s:1")
    addr, value = encode_atom(atom)
    assert np.array_equal(value, addr)


def test_char_ngrams_padded():
    assert char_ngrams("ab") == ["#ab", "ab#"]


def test_char_ngrams_length_three():
    assert char_ngrams("abcd") == ["#ab", "abc", "bcd", "cd#"]


def test_slot_hv_typo_similarity():
    a = slot_hv("france", D=4096)
    b = slot_hv("franca", D=4096)
    c = slot_hv("japan", D=4096)
    assert sim(a, b) > 0.4          # 3-grammes partagés
    assert sim(a, c) < 0.2          # quasi-orthogonal


def test_slot_hv_deterministic():
    assert np.array_equal(slot_hv("spain", D=2048), slot_hv("spain", D=2048))