Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_to_idx():
X = np.array([['a', 'x', 'b'], ['c', 'y', 'd']])
X_idx_expected = [[0, 0, 1], [2, 1, 3]]
rel_to_idx, ent_to_idx = create_mappings(X)
X_idx = to_idx(X, ent_to_idx=ent_to_idx, rel_to_idx=rel_to_idx)
np.testing.assert_array_equal(X_idx, X_idx_expected)
def test_generate_corruptions_for_eval():
X = np.array([['a', 'x', 'b'],
['c', 'x', 'd'],
['e', 'x', 'f'],
['b', 'y', 'h'],
['a', 'y', 'l']])
rel_to_idx, ent_to_idx = create_mappings(X)
X = to_idx(X, ent_to_idx=ent_to_idx, rel_to_idx=rel_to_idx)
with tf.Session() as sess:
all_ent = tf.constant(list(ent_to_idx.values()), dtype=tf.int64)
x = tf.constant(np.array([X[0]]), dtype=tf.int64)
x_n_actual = sess.run(generate_corruptions_for_eval(x, all_ent))
x_n_expected = np.array([[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 0, 4],
[0, 0, 5],
[0, 0, 6],
[0, 0, 7],
[0, 0, 1],
[1, 0, 1],
Returns
-------
rel_to_idx : dictionary
Relation to idx mapping dictionary
ent_to_idx : dictionary
entity to idx mapping dictionary
"""
from ..evaluation import create_mappings
if use_all:
complete_dataset = []
for key in self.dataset.keys():
complete_dataset.append(self.dataset[key])
self.rel_to_idx, self.ent_to_idx = create_mappings(np.concatenate(complete_dataset, axis=0))
else:
self.rel_to_idx, self.ent_to_idx = create_mappings(self.dataset["train"])
return self.rel_to_idx, self.ent_to_idx
Relation to idx mapping dictionary
ent_to_idx : dictionary
entity to idx mapping dictionary
"""
if (len(self.rel_to_idx) == 0 or len(self.ent_to_idx) == 0 or (regenerate is True)) \
and (not self.using_existing_db):
from ..evaluation import create_mappings
self._create_schema()
if use_all:
complete_dataset = []
for key in self.dataset.keys():
complete_dataset.append(self.dataset[key])
self.rel_to_idx, self.ent_to_idx = create_mappings(np.concatenate(complete_dataset, axis=0))
else:
self.rel_to_idx, self.ent_to_idx = create_mappings(self.dataset["train"])
self._insert_entities_in_db()
return self.rel_to_idx, self.ent_to_idx
use_all : boolean
If True, it generates mapping from all the data. If False, it only uses training set to generate mappings
Returns
-------
rel_to_idx : dictionary
Relation to idx mapping dictionary
ent_to_idx : dictionary
entity to idx mapping dictionary
"""
from ..evaluation import create_mappings
if use_all:
complete_dataset = []
for key in self.dataset.keys():
complete_dataset.append(self.dataset[key])
self.rel_to_idx, self.ent_to_idx = create_mappings(np.concatenate(complete_dataset, axis=0))
else:
self.rel_to_idx, self.ent_to_idx = create_mappings(self.dataset["train"])
return self.rel_to_idx, self.ent_to_idx
Returns
-------
rel_to_idx : dictionary
Relation to idx mapping dictionary
ent_to_idx : dictionary
entity to idx mapping dictionary
"""
if (len(self.rel_to_idx) == 0 or len(self.ent_to_idx) == 0 or (regenerate is True)) \
and (not self.using_existing_db):
from ..evaluation import create_mappings
self._create_schema()
if use_all:
complete_dataset = []
for key in self.dataset.keys():
complete_dataset.append(self.dataset[key])
self.rel_to_idx, self.ent_to_idx = create_mappings(np.concatenate(complete_dataset, axis=0))
else:
self.rel_to_idx, self.ent_to_idx = create_mappings(self.dataset["train"])
self._insert_entities_in_db()
return self.rel_to_idx, self.ent_to_idx