2019-04-10 17:06:09 +03:00
|
|
|
# cython: infer_types=True
|
2019-03-18 19:27:51 +03:00
|
|
|
# cython: profile=True
|
|
|
|
# coding: utf8
|
2019-04-23 17:33:40 +03:00
|
|
|
from collections import OrderedDict
|
|
|
|
from cpython.exc cimport PyErr_CheckSignals
|
|
|
|
|
|
|
|
from spacy import util
|
2019-03-22 18:55:05 +03:00
|
|
|
from spacy.errors import Errors, Warnings, user_warning
|
2019-03-15 18:05:23 +03:00
|
|
|
|
2019-04-24 12:26:38 +03:00
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
from preshed.maps cimport PreshMap
|
|
|
|
|
2019-04-23 17:33:40 +03:00
|
|
|
from cpython.mem cimport PyMem_Malloc
|
|
|
|
from cpython.exc cimport PyErr_SetFromErrno
|
|
|
|
|
|
|
|
from libc.stdio cimport FILE, fopen, fclose, fread, fwrite, feof, fseek
|
|
|
|
from libc.stdint cimport int32_t, int64_t
|
|
|
|
from libc.stdlib cimport qsort
|
|
|
|
|
|
|
|
from .typedefs cimport hash_t
|
|
|
|
|
|
|
|
from os import path
|
2019-04-24 12:26:38 +03:00
|
|
|
from libcpp.vector cimport vector
|
|
|
|
|
2019-04-23 17:33:40 +03:00
|
|
|
|
2019-03-21 02:04:06 +03:00
|
|
|
|
|
|
|
cdef class Candidate:
|
|
|
|
|
2019-03-25 20:10:41 +03:00
|
|
|
def __init__(self, KnowledgeBase kb, entity_hash, alias_hash, prior_prob):
|
2019-03-21 15:26:12 +03:00
|
|
|
self.kb = kb
|
2019-03-25 20:10:41 +03:00
|
|
|
self.entity_hash = entity_hash
|
2019-03-21 14:31:02 +03:00
|
|
|
self.alias_hash = alias_hash
|
|
|
|
self.prior_prob = prior_prob
|
2019-03-21 02:04:06 +03:00
|
|
|
|
2019-03-22 18:10:49 +03:00
|
|
|
@property
|
2019-03-25 20:10:41 +03:00
|
|
|
def entity(self):
|
|
|
|
"""RETURNS (uint64): hash of the entity's KB ID/name"""
|
|
|
|
return self.entity_hash
|
2019-03-21 20:20:57 +03:00
|
|
|
|
2019-03-22 18:10:49 +03:00
|
|
|
@property
|
2019-03-25 20:10:41 +03:00
|
|
|
def entity_(self):
|
|
|
|
"""RETURNS (unicode): ID/name of this entity in the KB"""
|
2019-04-10 17:06:09 +03:00
|
|
|
return self.kb.vocab.strings[self.entity_hash]
|
2019-03-21 02:04:06 +03:00
|
|
|
|
2019-03-22 18:10:49 +03:00
|
|
|
@property
|
|
|
|
def alias(self):
|
2019-03-21 15:26:12 +03:00
|
|
|
"""RETURNS (uint64): hash of the alias"""
|
2019-03-22 18:10:49 +03:00
|
|
|
return self.alias_hash
|
2019-03-21 02:04:06 +03:00
|
|
|
|
2019-03-22 18:10:49 +03:00
|
|
|
@property
|
|
|
|
def alias_(self):
|
2019-03-21 20:20:57 +03:00
|
|
|
"""RETURNS (unicode): ID of the original alias"""
|
2019-04-10 17:06:09 +03:00
|
|
|
return self.kb.vocab.strings[self.alias_hash]
|
2019-03-21 20:20:57 +03:00
|
|
|
|
2019-03-22 18:10:49 +03:00
|
|
|
@property
|
|
|
|
def prior_prob(self):
|
|
|
|
return self.prior_prob
|
2019-03-21 02:04:06 +03:00
|
|
|
|
|
|
|
|
2019-03-15 18:05:23 +03:00
|
|
|
cdef class KnowledgeBase:
|
2019-03-22 01:17:25 +03:00
|
|
|
def __init__(self, Vocab vocab):
|
|
|
|
self.vocab = vocab
|
2019-03-18 19:27:51 +03:00
|
|
|
self.mem = Pool()
|
2019-04-10 18:25:10 +03:00
|
|
|
self._entry_index = PreshMap()
|
|
|
|
self._alias_index = PreshMap()
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
# TODO initialize self._entries and self._aliases_table ?
|
|
|
|
|
2019-04-10 18:25:10 +03:00
|
|
|
self.vocab.strings.add("")
|
|
|
|
self._create_empty_vectors(dummy_hash=self.vocab.strings[""])
|
2019-03-18 19:27:51 +03:00
|
|
|
|
2019-03-15 18:05:23 +03:00
|
|
|
def __len__(self):
|
2019-03-19 17:51:56 +03:00
|
|
|
return self.get_size_entities()
|
|
|
|
|
|
|
|
def get_size_entities(self):
|
2019-04-24 12:26:38 +03:00
|
|
|
return len(self._entry_index)
|
2019-03-15 18:05:23 +03:00
|
|
|
|
2019-04-18 15:12:17 +03:00
|
|
|
def get_entity_strings(self):
|
|
|
|
return [self.vocab.strings[x] for x in self._entry_index][1:] # removing the dummy element on index 0
|
|
|
|
|
2019-03-19 17:51:56 +03:00
|
|
|
def get_size_aliases(self):
|
2019-04-24 12:26:38 +03:00
|
|
|
return len(self._alias_index)
|
2019-04-18 15:12:17 +03:00
|
|
|
|
|
|
|
def get_alias_strings(self):
|
|
|
|
return [self.vocab.strings[x] for x in self._alias_index][1:] # removing the dummy element on index 0
|
2019-03-19 17:51:56 +03:00
|
|
|
|
2019-03-25 20:10:41 +03:00
|
|
|
def add_entity(self, unicode entity, float prob=0.5, vectors=None, features=None):
|
2019-03-21 15:26:12 +03:00
|
|
|
"""
|
2019-04-10 17:06:09 +03:00
|
|
|
Add an entity to the KB, optionally specifying its log probability based on corpus frequency
|
|
|
|
Return the hash of the entity ID/name at the end
|
2019-03-21 15:26:12 +03:00
|
|
|
"""
|
2019-03-25 20:10:41 +03:00
|
|
|
cdef hash_t entity_hash = self.vocab.strings.add(entity)
|
2019-03-18 19:27:51 +03:00
|
|
|
|
2019-03-19 23:35:24 +03:00
|
|
|
# Return if this entity was added before
|
2019-03-25 20:10:41 +03:00
|
|
|
if entity_hash in self._entry_index:
|
|
|
|
user_warning(Warnings.W018.format(entity=entity))
|
2019-03-15 18:05:23 +03:00
|
|
|
return
|
|
|
|
|
2019-03-18 12:31:01 +03:00
|
|
|
cdef int32_t dummy_value = 342
|
2019-04-10 18:25:10 +03:00
|
|
|
new_index = self.c_add_entity(entity_hash=entity_hash, prob=prob,
|
|
|
|
vector_rows=&dummy_value, feats_row=dummy_value)
|
|
|
|
self._entry_index[entity_hash] = new_index
|
|
|
|
|
2019-03-18 12:31:01 +03:00
|
|
|
# TODO self._vectors_table.get_pointer(vectors),
|
2019-03-18 14:38:40 +03:00
|
|
|
# self._features_table.get(features))
|
2019-03-15 18:05:23 +03:00
|
|
|
|
2019-03-25 20:10:41 +03:00
|
|
|
return entity_hash
|
2019-03-21 15:26:12 +03:00
|
|
|
|
2019-03-18 19:27:51 +03:00
|
|
|
def add_alias(self, unicode alias, entities, probabilities):
|
2019-03-21 15:26:12 +03:00
|
|
|
"""
|
|
|
|
For a given alias, add its potential entities and prior probabilies to the KB.
|
|
|
|
Return the alias_hash at the end
|
|
|
|
"""
|
2019-03-19 23:43:48 +03:00
|
|
|
|
2019-03-19 23:55:10 +03:00
|
|
|
# Throw an error if the length of entities and probabilities are not the same
|
|
|
|
if not len(entities) == len(probabilities):
|
2019-03-22 18:55:05 +03:00
|
|
|
raise ValueError(Errors.E132.format(alias=alias,
|
|
|
|
entities_length=len(entities),
|
|
|
|
probabilities_length=len(probabilities)))
|
2019-03-19 23:55:10 +03:00
|
|
|
|
2019-03-19 23:43:48 +03:00
|
|
|
# Throw an error if the probabilities sum up to more than 1
|
|
|
|
prob_sum = sum(probabilities)
|
|
|
|
if prob_sum > 1:
|
2019-03-22 18:55:05 +03:00
|
|
|
raise ValueError(Errors.E133.format(alias=alias, sum=prob_sum))
|
2019-03-19 23:43:48 +03:00
|
|
|
|
2019-03-22 01:17:25 +03:00
|
|
|
cdef hash_t alias_hash = self.vocab.strings.add(alias)
|
2019-03-19 23:35:24 +03:00
|
|
|
|
|
|
|
# Return if this alias was added before
|
|
|
|
if alias_hash in self._alias_index:
|
2019-03-22 18:55:05 +03:00
|
|
|
user_warning(Warnings.W017.format(alias=alias))
|
2019-03-19 23:35:24 +03:00
|
|
|
return
|
|
|
|
|
2019-03-19 18:15:38 +03:00
|
|
|
cdef vector[int64_t] entry_indices
|
|
|
|
cdef vector[float] probs
|
|
|
|
|
|
|
|
for entity, prob in zip(entities, probabilities):
|
2019-03-25 20:10:41 +03:00
|
|
|
entity_hash = self.vocab.strings[entity]
|
|
|
|
if not entity_hash in self._entry_index:
|
2019-03-22 18:55:05 +03:00
|
|
|
raise ValueError(Errors.E134.format(alias=alias, entity=entity))
|
2019-03-19 19:39:35 +03:00
|
|
|
|
2019-03-25 20:10:41 +03:00
|
|
|
entry_index = <int64_t>self._entry_index.get(entity_hash)
|
2019-03-19 18:15:38 +03:00
|
|
|
entry_indices.push_back(int(entry_index))
|
|
|
|
probs.push_back(float(prob))
|
2019-03-18 14:38:40 +03:00
|
|
|
|
2019-04-10 18:25:10 +03:00
|
|
|
new_index = self.c_add_aliases(alias_hash=alias_hash, entry_indices=entry_indices, probs=probs)
|
|
|
|
self._alias_index[alias_hash] = new_index
|
2019-03-18 19:27:51 +03:00
|
|
|
|
2019-03-21 15:26:12 +03:00
|
|
|
return alias_hash
|
|
|
|
|
2019-03-19 23:35:24 +03:00
|
|
|
|
2019-03-18 19:50:01 +03:00
|
|
|
def get_candidates(self, unicode alias):
|
2019-03-21 20:55:01 +03:00
|
|
|
""" TODO: where to put this functionality ?"""
|
2019-03-22 01:17:25 +03:00
|
|
|
cdef hash_t alias_hash = self.vocab.strings[alias]
|
2019-03-19 17:51:56 +03:00
|
|
|
alias_index = <int64_t>self._alias_index.get(alias_hash)
|
2019-03-21 02:04:06 +03:00
|
|
|
alias_entry = self._aliases_table[alias_index]
|
|
|
|
|
2019-03-21 15:26:12 +03:00
|
|
|
return [Candidate(kb=self,
|
2019-03-25 20:10:41 +03:00
|
|
|
entity_hash=self._entries[entry_index].entity_hash,
|
2019-03-21 14:31:02 +03:00
|
|
|
alias_hash=alias_hash,
|
|
|
|
prior_prob=prob)
|
2019-03-21 17:24:40 +03:00
|
|
|
for (entry_index, prob) in zip(alias_entry.entry_indices, alias_entry.probs)
|
|
|
|
if entry_index != 0]
|
2019-04-23 17:33:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
def dump(self, loc):
|
2019-04-23 19:36:50 +03:00
|
|
|
cdef Writer writer = Writer(loc)
|
2019-04-24 16:31:44 +03:00
|
|
|
writer.write_header(self.get_size_entities())
|
2019-04-23 17:33:40 +03:00
|
|
|
|
2019-04-24 12:26:38 +03:00
|
|
|
# dumping the entry records in the order in which they are in the _entries vector.
|
|
|
|
# index 0 is a dummy object not stored in the _entry_index and can be ignored.
|
|
|
|
i = 1
|
|
|
|
for entry_hash, entry_index in sorted(self._entry_index.items(), key=lambda x: x[1]):
|
2019-04-23 19:36:50 +03:00
|
|
|
entry = self._entries[entry_index]
|
2019-04-24 12:26:38 +03:00
|
|
|
assert entry.entity_hash == entry_hash
|
|
|
|
assert entry_index == i
|
2019-04-24 16:31:44 +03:00
|
|
|
writer.write_entry(entry_index, entry.entity_hash, entry.prob)
|
2019-04-24 12:26:38 +03:00
|
|
|
i = i+1
|
2019-04-23 17:33:40 +03:00
|
|
|
|
|
|
|
writer.close()
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cpdef load_bulk(self, loc):
|
2019-04-23 17:33:40 +03:00
|
|
|
cdef int64_t entry_id
|
|
|
|
cdef hash_t entity_hash
|
|
|
|
cdef float prob
|
2019-04-24 12:26:38 +03:00
|
|
|
cdef EntryC entry
|
2019-04-23 17:33:40 +03:00
|
|
|
cdef int32_t dummy_value = 342
|
2019-04-23 19:36:50 +03:00
|
|
|
|
|
|
|
cdef Reader reader = Reader(loc)
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int64_t nr_entities
|
|
|
|
reader.read_header(&nr_entities)
|
2019-04-24 12:26:38 +03:00
|
|
|
|
|
|
|
self._entry_index = PreshMap(nr_entities+1)
|
|
|
|
self._entries = entry_vec(nr_entities+1)
|
|
|
|
|
|
|
|
# we assume the data was written in sequence
|
|
|
|
# index 0 is a dummy object not stored in the _entry_index and can be ignored.
|
|
|
|
# TODO: should we initialize the dummy objects ?
|
|
|
|
cdef int i = 1
|
2019-04-24 16:31:44 +03:00
|
|
|
while reader.read_entry(&entry_id, &entity_hash, &prob) and i <= nr_entities:
|
2019-04-24 12:26:38 +03:00
|
|
|
assert i == entry_id
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
# TODO features and vectors
|
2019-04-23 19:36:50 +03:00
|
|
|
entry.entity_hash = entity_hash
|
|
|
|
entry.prob = prob
|
|
|
|
entry.vector_rows = &dummy_value
|
|
|
|
entry.feats_row = dummy_value
|
|
|
|
|
2019-04-24 12:26:38 +03:00
|
|
|
self._entries[i] = entry
|
|
|
|
self._entry_index[entity_hash] = i
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
2019-04-23 17:33:40 +03:00
|
|
|
|
|
|
|
cdef class Writer:
|
|
|
|
def __init__(self, object loc):
|
|
|
|
if path.exists(loc):
|
|
|
|
assert not path.isdir(loc), "%s is directory." % loc
|
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
|
|
|
self._fp = fopen(<char*>bytes_loc, 'wb')
|
|
|
|
assert self._fp != NULL
|
|
|
|
fseek(self._fp, 0, 0)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
cdef size_t status = fclose(self._fp)
|
|
|
|
assert status == 0
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int write_header(self, int64_t nr_entries) except -1:
|
|
|
|
self._write(&nr_entries, sizeof(nr_entries))
|
2019-04-23 17:33:40 +03:00
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int write_entry(self, int64_t entry_id, hash_t entry_hash, float entry_prob) except -1:
|
|
|
|
# TODO: feats_rows and vector rows
|
|
|
|
self._write(&entry_id, sizeof(entry_id))
|
|
|
|
self._write(&entry_hash, sizeof(entry_hash))
|
|
|
|
self._write(&entry_prob, sizeof(entry_prob))
|
2019-04-23 17:33:40 +03:00
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int _write(self, void* value, size_t size) except -1:
|
|
|
|
status = fwrite(value, size, 1, self._fp)
|
|
|
|
assert status == 1, status
|
2019-04-23 17:33:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
cdef class Reader:
|
|
|
|
def __init__(self, object loc):
|
|
|
|
assert path.exists(loc)
|
|
|
|
assert not path.isdir(loc)
|
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
|
|
|
self._fp = fopen(<char*>bytes_loc, 'rb')
|
|
|
|
if not self._fp:
|
|
|
|
PyErr_SetFromErrno(IOError)
|
|
|
|
status = fseek(self._fp, 0, 0) # this can be 0 if there is no header
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
fclose(self._fp)
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int read_header(self, int64_t* nr_entries) except -1:
|
|
|
|
status = self._read(nr_entries, sizeof(int64_t))
|
|
|
|
if status < 1:
|
|
|
|
if feof(self._fp):
|
|
|
|
return 0 # end of file
|
|
|
|
raise IOError("error reading header from input file")
|
|
|
|
|
|
|
|
cdef int read_entry(self, int64_t* entry_id, hash_t* entity_hash, float* prob) except -1:
|
|
|
|
status = self._read(entry_id, sizeof(int64_t))
|
2019-04-23 17:33:40 +03:00
|
|
|
if status < 1:
|
|
|
|
if feof(self._fp):
|
|
|
|
return 0 # end of file
|
|
|
|
raise IOError("error reading entry ID from input file")
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
status = self._read(entity_hash, sizeof(hash_t))
|
2019-04-23 17:33:40 +03:00
|
|
|
if status < 1:
|
|
|
|
if feof(self._fp):
|
|
|
|
return 0 # end of file
|
|
|
|
raise IOError("error reading entity hash from input file")
|
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
status = self._read(prob, sizeof(float))
|
2019-04-23 17:33:40 +03:00
|
|
|
if status < 1:
|
|
|
|
if feof(self._fp):
|
|
|
|
return 0 # end of file
|
|
|
|
raise IOError("error reading entity prob from input file")
|
2019-04-23 19:36:50 +03:00
|
|
|
|
|
|
|
if feof(self._fp):
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return 1
|
2019-04-24 12:26:38 +03:00
|
|
|
|
2019-04-24 16:31:44 +03:00
|
|
|
cdef int _read(self, void* value, size_t size) except -1:
|
|
|
|
status = fread(value, size, 1, self._fp)
|
|
|
|
return status
|
|
|
|
|
2019-04-24 12:26:38 +03:00
|
|
|
|