Esempio n. 1
0
def test_similarity(rng):
    v = Vocabulary(64, max_similarity=0.1)
    v.populate('''
        A;
        B;
        C = .7 * A + .3 * B;
        D = C.normalized()
    ''')

    s = similarity(v['A'].v, v)
    assert_almost_equal(s[0], 1.)
    assert s[1] < 0.1
    assert_almost_equal(s[2], 0.7 + .3 * np.dot(v['A'].v, v['B'].v))

    sn = similarity(v['A'].v, v, normalize=True)
    assert_almost_equal(s[3], sn[2])

    data = np.array([v['A'].v, v['B'].v])
    s = similarity(data, v)
    assert_almost_equal(s[0, 0], 1.)
    assert_almost_equal(s[1, 1], 1.)
    assert s[0, 1] < 0.1
    assert s[1, 0] < 0.1

    s = similarity(data, v.parse_n('A', 'B'))
    assert_almost_equal(s[0, 0], 1.)
    assert_almost_equal(s[1, 1], 1.)
    assert s[0, 1] < 0.1
    assert s[1, 0] < 0.1
Esempio n. 2
0
def test_similarity(rng):
    v = Vocabulary(64, max_similarity=0.1)
    v.populate("""
        A;
        B;
        C = .7 * A + .3 * B;
        D = C.normalized()
    """)

    s = similarity(v["A"].v, v)
    assert_almost_equal(s[0], 1.0)
    assert s[1] < 0.1
    assert_almost_equal(s[2], 0.7 + 0.3 * np.dot(v["A"].v, v["B"].v))

    sn = similarity(v["A"].v, v, normalize=True)
    assert_almost_equal(s[3], sn[2])

    data = np.array([v["A"].v, v["B"].v])
    s = similarity(data, v)
    assert_almost_equal(s[0, 0], 1.0)
    assert_almost_equal(s[1, 1], 1.0)
    assert s[0, 1] < 0.1
    assert s[1, 0] < 0.1

    s = similarity(data, v.parse_n("A", "B"))
    assert_almost_equal(s[0, 0], 1.0)
    assert_almost_equal(s[1, 1], 1.0)
    assert s[0, 1] < 0.1
    assert s[1, 0] < 0.1
def test_non_equality():
    v = Vocabulary(16)
    tv1 = TVocabulary(v)
    tv2 = TVocabulary(v)
    tvx = TVocabulary(Vocabulary(16))
    assert tv1 == tv2
    assert not (tv1 != tv2)
    assert tv1 != tvx
    assert not (tv1 == tvx)
def test_coercion_errors():
    with pytest.raises(SpaTypeError) as err:
        coerce_types(TVocabulary(Vocabulary(16)), TVocabulary(Vocabulary(16)))
    assert "Different vocabularies" in str(err.value)

    with pytest.raises(SpaTypeError) as err:
        coerce_types(TAnyVocabOfDim(16), TAnyVocabOfDim(32))
    assert "Dimensionality mismatch" in str(err.value)

    with pytest.raises(SpaTypeError) as err:
        coerce_types(Type('x'), Type('y'))
    assert "Incompatible types" in str(err.value)
def test_coercion():
    v1 = TVocabulary(Vocabulary(16))

    assert coerce_types(TAnyVocab, TAnyVocab) is TAnyVocab
    assert coerce_types(TAnyVocab, TScalar) is TAnyVocab
    assert coerce_types(TAnyVocab, v1) == v1
    assert coerce_types(TScalar, TScalar) == TScalar
    assert coerce_types(TScalar, TScalar, TScalar) == TScalar
    assert coerce_types(TScalar, v1) == v1
    assert coerce_types(v1, v1) == v1
    assert coerce_types(TAnyVocab, v1, TScalar) == v1
    assert coerce_types(TScalar, TScalar, v1, TScalar, v1) == v1
Esempio n. 6
0
def test_text(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate('A; B; C; D; E; F')
    x = v.parse('A+B+C')
    y = v.parse('-D-E-F')
    ptr = r'-?[01]\.[0-9]{2}[A-F]'
    assert re.match(';'.join([ptr] * 3), text(x, v))
    assert re.match(';'.join([ptr] * 2), text(x, v, maximum_count=2))
    assert re.match(ptr, text(x, v, maximum_count=1))
    assert len(text(x, v, maximum_count=10).split(';')) <= 10
    assert re.match(';'.join([ptr] * 4), text(x, v, minimum_count=4))
    assert re.match(';'.join([ptr.replace('F', 'C')] * 3),
                    text(x, v, minimum_count=4, terms=['A', 'B', 'C']))

    assert re.match(ptr, text(y, v, threshold=0.6))
    assert text(y, v, minimum_count=None, threshold=0.6) == ''

    assert text(x, v, join=',') == text(x, v).replace(';', ',')
    assert re.match(';'.join([ptr] * 2), text(x, v, normalize=True))

    assert text([0]*64, v) == '0.00F'
    assert text(v['D'].v, v) == '1.00D'
Esempio n. 7
0
def test_text(rng):
    v = Vocabulary(64, pointer_gen=rng)
    v.populate("A; B; C; D; E; F")
    x = v.parse("A+B+C")
    y = v.parse("-D-E-F")
    ptr = r"-?[01]\.[0-9]{2}[A-F]"
    assert re.match(";".join([ptr] * 3), text(x, v))
    assert re.match(";".join([ptr] * 2), text(x, v, maximum_count=2))
    assert re.match(ptr, text(x, v, maximum_count=1))
    assert len(text(x, v, maximum_count=10).split(";")) <= 10
    assert re.match(";".join([ptr] * 4), text(x, v, minimum_count=4))
    assert re.match(
        ";".join([ptr.replace("F", "C")] * 3),
        text(x, v, minimum_count=4, terms=["A", "B", "C"]),
    )

    assert re.match(ptr, text(y, v, threshold=0.6))
    assert text(y, v, minimum_count=None, threshold=0.6) == ""

    assert text(x, v, join=",") == text(x, v).replace(";", ",")
    assert re.match(";".join([ptr] * 2), text(x, v, normalize=True))

    assert text([0] * 64, v) == "0.00F"
    assert text(v["D"].v, v) == "1.00D"
Esempio n. 8
0
def test_none_vocab_is_always_compatible(op):
    gen = UnitLengthVectors(50)
    v = Vocabulary(50)
    a = SemanticPointer(next(gen), vocab=v)  # noqa: F841
    b = SemanticPointer(next(gen), vocab=None)  # noqa: F841
    eval(op)  # no assertion, just checking that no exception is raised
Esempio n. 9
0
def test_ops_check_vocab_compatibility(op):
    gen = UnitLengthVectors(50)
    a = SemanticPointer(next(gen), vocab=Vocabulary(50))  # noqa: F841
    b = SemanticPointer(next(gen), vocab=Vocabulary(50))  # noqa: F841
    with pytest.raises(SpaTypeError):
        eval(op)
Esempio n. 10
0
def test_ops_preserve_vocab(op):
    v = Vocabulary(50)
    a = SemanticPointer(next(UnitLengthVectors(50)), vocab=v)  # noqa: F841
    x = eval(op)
    assert x.vocab is v
Esempio n. 11
0
def test_pairs():
    v = Vocabulary(64)
    v.populate('A; B; C')
    actual = pairs(v)
    expected = {'A*B', 'A*C', 'B*C'}
    assert actual == expected
Esempio n. 12
0
def test_pairs():
    v = Vocabulary(64)
    v.populate("A; B; C")
    actual = pairs(v)
    expected = {"A*B", "A*C", "B*C"}
    assert actual == expected