Exemple #1
0
def test_init():
    space = Space('tests/example.n3')
    assert_equal(space._path_to_rdf, 'file:tests/example.n3')
    assert_equal(space._format, 'ntriples')
    assert_equal(space._property,
                 'http://www.w3.org/2004/02/skos/core#broader')
    assert_equal(
        space._direct_parents, {
            'http://dbpedia.org/resource/Category:Categories_named_after_television_series':
            ['http://dbpedia.org/resource/Category:Foo'],
            'http://dbpedia.org/resource/Category:Star_Trek': [
                'http://dbpedia.org/resource/Category:Categories_named_after_television_series',
            ],
            'http://dbpedia.org/resource/Category:Futurama': [
                'http://dbpedia.org/resource/Category:Categories_named_after_television_series',
                'http://dbpedia.org/resource/Category:New_York_City_in_fiction',
            ],
        })
    assert_equal(
        space._index, {
            'http://dbpedia.org/resource/Category:Categories_named_after_television_series':
            0,
            'http://dbpedia.org/resource/Category:New_York_City_in_fiction':
            1,
            'http://dbpedia.org/resource/Category:Foo':
            2,
        })
    assert_equal(space._size, 3)
Exemple #2
0
def test_sum_weighted_uris():
    space = Space('tests/example.n3')
    s = space.sum_weighted_uris([
        ('http://dbpedia.org/resource/Category:Futurama', 2),
        ('http://dbpedia.org/resource/Category:Star_Trek', 1)
    ])
    np.testing.assert_allclose(np.asarray(s.todense()), [[
        2 / np.sqrt(2 + 0.9**2) + 1 / np.sqrt(1 + 0.9**2),
        2 / np.sqrt(2 + 0.9**2),
        2 * 0.9 / np.sqrt(2 + 0.9**2) + 0.9 / np.sqrt(1 + 0.9**2)
    ]])
Exemple #3
0
def test_parents():
    space = Space('tests/example.n3')
    assert_equal(
        space.parents('http://dbpedia.org/resource/Category:Futurama'), [
            ('http://dbpedia.org/resource/Category:Categories_named_after_television_series',
             1),
            ('http://dbpedia.org/resource/Category:New_York_City_in_fiction',
             1),
            ('http://dbpedia.org/resource/Category:Foo', 0.9),
        ])
    assert_equal(
        space.parents('http://dbpedia.org/resource/Category:Star_Trek'), [
            ('http://dbpedia.org/resource/Category:Categories_named_after_television_series',
             1),
            ('http://dbpedia.org/resource/Category:Foo', 0.9),
        ])
    assert_equal(space.parents('http://dbpedia.org/resource/Category:Foo'), [])

    space = Space('tests/london.n3')
    Space.max_depth = 0
    assert_equal(space.parents(
        'http://dbpedia.org/resource/Category:London'
    ), [(
        'http://dbpedia.org/resource/Category:NUTS_1_statistical_regions_of_England',
        1), ('http://dbpedia.org/resource/Category:M4_corridor', 1)])
    Space.max_depth = 1
    print space.parents('http://dbpedia.org/resource/Category:London')
    assert_equal(space.parents(
        'http://dbpedia.org/resource/Category:London'
    ), [('http://dbpedia.org/resource/Category:Regional_planning_in_England',
         0.9),
        ('http://dbpedia.org/resource/Category:Regions_of_England', 0.9),
        ('http://dbpedia.org/resource/Category:NUTS_1_statistical_regions_of_the_European_Union',
         0.9), ('http://dbpedia.org/resource/Category:England', 0.9),
        ('http://dbpedia.org/resource/Category:NUTS_1_statistical_regions_of_the_United_Kingdom',
         0.9), ('http://dbpedia.org/resource/Category:Regions_of_Wales', 0.9),
        ('http://dbpedia.org/resource/Category:NUTS_1_statistical_regions_of_England',
         1),
        ('http://dbpedia.org/resource/Category:Local_government_in_England',
         0.9), ('http://dbpedia.org/resource/Category:M4_corridor', 1)])
    Space.max_depth = 10
Exemple #4
0
def test_centroid_weighted_uris():
    space = Space('tests/example.n3')
    centroid = space.centroid_weighted_uris([
        ('http://dbpedia.org/resource/Category:Futurama', 2),
        ('http://dbpedia.org/resource/Category:Star_Trek', 1)
    ])
    np.testing.assert_allclose(
        centroid.todense(),
        np.array([[
            (2 / np.sqrt(2 + 0.9**2) + 1 / np.sqrt(1 + 0.9**2)) / 2,
            (1 / np.sqrt(2 + 0.9**2)),
            (2 * 0.9 / np.sqrt(2 + 0.9**2) + 0.9 / np.sqrt(1 + 0.9**2)) / 2
        ]]))
Exemple #5
0
def test_cache_vectors():
    space = Space('tests/example.n3')
    space.cache_vectors()
    assert space._uri_to_vector.has_key(
        'http://dbpedia.org/resource/Category:Futurama'
    )  # Checking that we cached the vectors
    assert space._uri_to_vector.has_key(
        'http://dbpedia.org/resource/Category:Star_Trek')
    np.testing.assert_array_equal(
        space._uri_to_vector['http://dbpedia.org/resource/Category:Futurama'],
        space.to_vector('http://dbpedia.org/resource/Category:Futurama'))
    np.testing.assert_array_equal(
        space._uri_to_vector['http://dbpedia.org/resource/Category:Star_Trek'],
        space.to_vector('http://dbpedia.org/resource/Category:Star_Trek'))
Exemple #6
0
def test_similarity_all():
    space = Space('tests/example.n3')
    m = lil_matrix((2, 3))
    m[0, 0] = 1 / np.sqrt(1 + 2 * 2 + 3 * 3)
    m[0, 1] = 2 / np.sqrt(1 + 2 * 2 + 3 * 3)
    m[0, 2] = 3 / np.sqrt(1 + 2 * 2 + 3 * 3)
    m[1, 0] = 4 / np.sqrt(4 * 4 + 5 * 5 + 6 * 6)
    m[1, 1] = 5 / np.sqrt(4 * 4 + 5 * 5 + 6 * 6)
    m[1, 2] = 6 / np.sqrt(4 * 4 + 5 * 5 + 6 * 6)
    v = m[0, :]
    m = m.tocsr()
    similarities = space.similarity_all(m, v)
    assert_equal(similarities[0], 1)
    assert_equal(
        similarities[1],
        ((1 * 4 + 2 * 5 + 3 * 6) /
         (np.sqrt(1 + 2 * 2 + 3 * 3) * np.sqrt(4 * 4 + 5 * 5 + 6 * 6))))
Exemple #7
0
def test_to_vector():
    space = Space('tests/example.n3')
    np.testing.assert_array_equal(
        space.to_vector(
            'http://dbpedia.org/resource/Category:Futurama').todense(), [[
                1 / np.sqrt(2 + 0.9**2), 1 / np.sqrt(2 + 0.9**2),
                0.9 / np.sqrt(2 + 0.9**2)
            ]])
    np.testing.assert_array_equal(
        space.to_vector(
            'http://dbpedia.org/resource/Category:Star_Trek').todense(),
        [[1 / np.sqrt(1 + 0.9**2), 0, 0.9 / np.sqrt(1 + 0.9**2)]])
    assert space._uri_to_vector.has_key(
        'http://dbpedia.org/resource/Category:Futurama'
    )  # Checking that we cached the vectors when generating them
    assert space._uri_to_vector.has_key(
        'http://dbpedia.org/resource/Category:Star_Trek')
    np.testing.assert_array_equal(
        space._uri_to_vector['http://dbpedia.org/resource/Category:Futurama'],
        space.to_vector('http://dbpedia.org/resource/Category:Futurama'))
    np.testing.assert_array_equal(
        space._uri_to_vector['http://dbpedia.org/resource/Category:Star_Trek'],
        space.to_vector('http://dbpedia.org/resource/Category:Star_Trek'))
Exemple #8
0
import rdfsim
from rdfsim.space import Space

space = Space('skos_categories_1000.nt',
              property='http://www.w3.org/2004/02/skos/core#broader')

print "Distance betwen Dwarves and Elves:"
print space.similarity_uri(
    'http://dbpedia.org/resource/Category:Middle-earth_Dwarves',
    'http://dbpedia.org/resource/Category:Middle-earth_Elves')

print "Distance between Dwarves and Futurama:"
print space.similarity_uri(
    'http://dbpedia.org/resource/Category:Middle-earth_Dwarves',
    'http://dbpedia.org/resource/Category:Futurama')
Exemple #9
0
def test_similarity_uri():
    space = Space('tests/example.n3')
    np.testing.assert_allclose(
        space.similarity_uri('http://dbpedia.org/resource/Category:Futurama',
                             'http://dbpedia.org/resource/Category:Star_Trek'),
        (1 + 0.9 * 0.9) / (np.sqrt(2 + 0.9**2) * np.sqrt(1 + 0.9**2)))