2016-10-24 14:49:03 +03:00
|
|
|
# cython: infer_types=True
|
2016-03-24 17:09:55 +03:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2016-03-24 17:09:55 +03:00
|
|
|
cimport cython
|
2014-12-19 22:42:01 +03:00
|
|
|
from libc.string cimport memcpy
|
2017-03-07 19:15:18 +03:00
|
|
|
from libc.stdint cimport uint64_t, uint32_t
|
2016-03-24 17:09:55 +03:00
|
|
|
|
2016-11-01 15:27:13 +03:00
|
|
|
from murmurhash.mrmr cimport hash64, hash32
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2016-09-30 21:20:22 +03:00
|
|
|
from preshed.maps cimport map_iter, key_t
|
2015-11-05 14:28:26 +03:00
|
|
|
|
2016-03-24 17:09:55 +03:00
|
|
|
from .typedefs cimport hash_t
|
2016-11-01 15:27:13 +03:00
|
|
|
from libc.stdint cimport uint32_t
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2017-03-07 19:15:18 +03:00
|
|
|
import ujson
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cpdef hash_t hash_string(unicode string) except 0:
|
2016-09-30 21:20:22 +03:00
|
|
|
chars = string.encode('utf8')
|
2017-03-07 19:15:18 +03:00
|
|
|
return hash_utf8(chars, len(chars))
|
2016-03-24 17:09:55 +03:00
|
|
|
|
|
|
|
|
2017-03-07 19:15:18 +03:00
|
|
|
cdef hash_t hash_utf8(char* utf8_string, int length) nogil:
|
2016-09-30 21:20:22 +03:00
|
|
|
return hash64(utf8_string, length, 1)
|
2015-01-12 02:26:22 +03:00
|
|
|
|
|
|
|
|
2017-03-07 19:15:18 +03:00
|
|
|
cdef uint32_t hash32_utf8(char* utf8_string, int length) nogil:
|
2016-11-01 15:27:13 +03:00
|
|
|
return hash32(utf8_string, length, 1)
|
|
|
|
|
|
|
|
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef unicode _decode(const Utf8Str* string):
|
2015-07-20 13:05:23 +03:00
|
|
|
cdef int i, length
|
2015-07-20 12:26:46 +03:00
|
|
|
if string.s[0] < sizeof(string.s) and string.s[0] != 0:
|
|
|
|
return string.s[1:string.s[0]+1].decode('utf8')
|
2015-07-20 13:05:23 +03:00
|
|
|
elif string.p[0] < 255:
|
2015-07-20 12:26:46 +03:00
|
|
|
return string.p[1:string.p[0]+1].decode('utf8')
|
|
|
|
else:
|
2015-07-20 13:05:23 +03:00
|
|
|
i = 0
|
|
|
|
length = 0
|
|
|
|
while string.p[i] == 255:
|
|
|
|
i += 1
|
|
|
|
length += 255
|
|
|
|
length += string.p[i]
|
2015-07-20 12:26:46 +03:00
|
|
|
i += 1
|
2015-07-20 13:05:23 +03:00
|
|
|
return string.p[i:length + i].decode('utf8')
|
2015-07-20 12:26:46 +03:00
|
|
|
|
|
|
|
|
2017-03-07 19:15:18 +03:00
|
|
|
cdef Utf8Str _allocate(Pool mem, const unsigned char* chars, uint32_t length) except *:
|
2015-07-20 13:05:23 +03:00
|
|
|
cdef int n_length_bytes
|
|
|
|
cdef int i
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef Utf8Str string
|
2017-03-07 19:15:18 +03:00
|
|
|
cdef uint32_t ulength = length
|
2015-07-20 12:26:46 +03:00
|
|
|
if length < sizeof(string.s):
|
|
|
|
string.s[0] = <unsigned char>length
|
|
|
|
memcpy(&string.s[1], chars, length)
|
|
|
|
return string
|
2015-07-20 13:05:23 +03:00
|
|
|
elif length < 255:
|
2015-07-20 12:26:46 +03:00
|
|
|
string.p = <unsigned char*>mem.alloc(length + 1, sizeof(unsigned char))
|
|
|
|
string.p[0] = length
|
|
|
|
memcpy(&string.p[1], chars, length)
|
|
|
|
assert string.s[0] >= sizeof(string.s) or string.s[0] == 0, string.s[0]
|
|
|
|
return string
|
|
|
|
else:
|
2015-07-20 13:05:23 +03:00
|
|
|
i = 0
|
|
|
|
n_length_bytes = (length // 255) + 1
|
|
|
|
string.p = <unsigned char*>mem.alloc(length + n_length_bytes, sizeof(unsigned char))
|
|
|
|
for i in range(n_length_bytes-1):
|
|
|
|
string.p[i] = 255
|
|
|
|
string.p[n_length_bytes-1] = length % 255
|
|
|
|
memcpy(&string.p[n_length_bytes], chars, length)
|
|
|
|
assert string.s[0] >= sizeof(string.s) or string.s[0] == 0, string.s[0]
|
|
|
|
return string
|
2014-12-21 23:25:43 +03:00
|
|
|
|
|
|
|
|
2014-12-19 22:42:01 +03:00
|
|
|
cdef class StringStore:
|
2015-01-24 12:49:15 +03:00
|
|
|
'''Map strings to and from integer IDs.'''
|
2016-10-24 14:49:03 +03:00
|
|
|
def __init__(self, strings=None, freeze=False):
|
2016-11-01 14:25:36 +03:00
|
|
|
'''Create the StringStore.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
strings: A sequence of unicode strings to add to the store.
|
|
|
|
'''
|
2014-12-19 22:42:01 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self._map = PreshMap()
|
2016-10-24 14:49:03 +03:00
|
|
|
self._oov = PreshMap()
|
2015-11-06 19:24:30 +03:00
|
|
|
self._resize_at = 10000
|
2015-07-20 12:26:46 +03:00
|
|
|
self.c = <Utf8Str*>self.mem.alloc(self._resize_at, sizeof(Utf8Str))
|
2015-01-14 16:33:16 +03:00
|
|
|
self.size = 1
|
2016-11-01 15:27:13 +03:00
|
|
|
self.is_frozen = freeze
|
2015-10-12 07:12:32 +03:00
|
|
|
if strings is not None:
|
|
|
|
for string in strings:
|
2016-09-30 21:19:42 +03:00
|
|
|
_ = self[string]
|
2014-12-19 22:42:01 +03:00
|
|
|
|
|
|
|
property size:
|
|
|
|
def __get__(self):
|
2015-07-20 12:26:46 +03:00
|
|
|
return self.size -1
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2017-03-07 19:15:18 +03:00
|
|
|
def __reduce__(self):
|
|
|
|
# TODO: OOV words, for the is_frozen stuff?
|
|
|
|
if self.is_frozen:
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Currently missing support for pickling StringStore when "
|
|
|
|
"is_frozen=True")
|
|
|
|
return (StringStore, (list(self),))
|
|
|
|
|
2015-06-23 01:02:50 +03:00
|
|
|
def __len__(self):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""The number of strings in the store.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int The number of strings in the store.
|
|
|
|
"""
|
2015-07-20 12:26:46 +03:00
|
|
|
return self.size-1
|
2015-06-23 01:02:50 +03:00
|
|
|
|
2014-12-19 22:42:01 +03:00
|
|
|
def __getitem__(self, object string_or_id):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""Retrieve a string from a given integer ID, or vice versa.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
string_or_id (bytes or unicode or int):
|
|
|
|
The value to encode.
|
|
|
|
Returns:
|
|
|
|
unicode or int: The value to retrieved.
|
|
|
|
"""
|
2016-10-24 14:49:03 +03:00
|
|
|
if isinstance(string_or_id, basestring) and len(string_or_id) == 0:
|
|
|
|
return 0
|
|
|
|
elif string_or_id == 0:
|
|
|
|
return u''
|
|
|
|
|
2014-12-19 22:42:01 +03:00
|
|
|
cdef bytes byte_string
|
|
|
|
cdef const Utf8Str* utf8str
|
2016-10-24 14:49:03 +03:00
|
|
|
cdef uint64_t int_id
|
2016-11-01 15:27:13 +03:00
|
|
|
cdef uint32_t oov_id
|
2016-03-24 17:09:55 +03:00
|
|
|
if isinstance(string_or_id, (int, long)):
|
2016-10-24 14:49:03 +03:00
|
|
|
int_id = string_or_id
|
2016-11-01 15:27:13 +03:00
|
|
|
oov_id = string_or_id
|
2016-10-24 14:49:03 +03:00
|
|
|
if int_id < <uint64_t>self.size:
|
|
|
|
return _decode(&self.c[int_id])
|
|
|
|
else:
|
2016-11-01 15:27:13 +03:00
|
|
|
utf8str = <Utf8Str*>self._oov.get(oov_id)
|
2016-10-24 14:49:03 +03:00
|
|
|
if utf8str is not NULL:
|
|
|
|
return _decode(utf8str)
|
|
|
|
else:
|
|
|
|
raise IndexError(string_or_id)
|
2016-10-24 15:22:51 +03:00
|
|
|
else:
|
2016-10-24 14:49:03 +03:00
|
|
|
if isinstance(string_or_id, bytes):
|
|
|
|
byte_string = <bytes>string_or_id
|
2016-10-24 15:22:51 +03:00
|
|
|
elif isinstance(string_or_id, unicode):
|
2016-10-24 14:49:03 +03:00
|
|
|
byte_string = (<unicode>string_or_id).encode('utf8')
|
2016-10-24 15:22:51 +03:00
|
|
|
else:
|
|
|
|
raise TypeError(type(string_or_id))
|
2016-09-30 21:20:22 +03:00
|
|
|
utf8str = self._intern_utf8(byte_string, len(byte_string))
|
2016-10-24 14:49:03 +03:00
|
|
|
if utf8str is NULL:
|
2016-11-01 15:27:13 +03:00
|
|
|
# TODO: We need to use 32 bit here, for compatibility with the
|
|
|
|
# vocabulary values. This makes birthday paradox probabilities
|
|
|
|
# pretty bad.
|
|
|
|
# We could also get unlucky here, and hash into a value that
|
|
|
|
# collides with the 'real' strings.
|
2017-03-07 19:15:18 +03:00
|
|
|
return hash32_utf8(byte_string, len(byte_string))
|
2016-10-24 14:49:03 +03:00
|
|
|
else:
|
|
|
|
return utf8str - self.c
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2016-03-24 17:40:12 +03:00
|
|
|
def __contains__(self, unicode string not None):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""Check whether a string is in the store.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
string (unicode): The string to check.
|
|
|
|
Returns bool:
|
|
|
|
Whether the store contains the string.
|
|
|
|
"""
|
2016-03-24 17:40:12 +03:00
|
|
|
if len(string) == 0:
|
|
|
|
return True
|
2016-09-30 21:19:42 +03:00
|
|
|
cdef hash_t key = hash_string(string)
|
2016-03-24 17:09:55 +03:00
|
|
|
return self._map.get(key) is not NULL
|
2016-03-08 18:49:10 +03:00
|
|
|
|
2015-08-22 23:04:34 +03:00
|
|
|
def __iter__(self):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""Iterate over the strings in the store, in order.
|
|
|
|
|
|
|
|
Yields: unicode A string in the store.
|
|
|
|
"""
|
2015-08-22 23:04:34 +03:00
|
|
|
cdef int i
|
|
|
|
for i in range(self.size):
|
2016-03-24 17:09:55 +03:00
|
|
|
yield _decode(&self.c[i]) if i > 0 else u''
|
2016-10-24 14:49:03 +03:00
|
|
|
# TODO: Iterate OOV here?
|
2015-08-22 23:04:34 +03:00
|
|
|
|
2015-10-12 07:12:32 +03:00
|
|
|
def __reduce__(self):
|
|
|
|
strings = [""]
|
|
|
|
for i in range(1, self.size):
|
|
|
|
string = &self.c[i]
|
|
|
|
py_string = _decode(string)
|
|
|
|
strings.append(py_string)
|
|
|
|
return (StringStore, (strings,), None, None, None)
|
|
|
|
|
2016-10-24 14:49:03 +03:00
|
|
|
def set_frozen(self, bint is_frozen):
|
|
|
|
# TODO
|
|
|
|
self.is_frozen = is_frozen
|
|
|
|
|
|
|
|
def flush_oov(self):
|
|
|
|
self._oov = PreshMap()
|
|
|
|
|
|
|
|
cdef const Utf8Str* intern_unicode(self, unicode py_string):
|
2016-09-30 21:20:22 +03:00
|
|
|
# 0 means missing, but we don't bother offsetting the index.
|
2016-09-30 21:19:42 +03:00
|
|
|
cdef bytes byte_string = py_string.encode('utf8')
|
2016-09-30 21:20:22 +03:00
|
|
|
return self._intern_utf8(byte_string, len(byte_string))
|
2015-07-20 12:26:46 +03:00
|
|
|
|
2016-09-30 11:14:47 +03:00
|
|
|
@cython.final
|
2016-10-24 14:49:03 +03:00
|
|
|
cdef const Utf8Str* _intern_utf8(self, char* utf8_string, int length):
|
|
|
|
# TODO: This function's API/behaviour is an unholy mess...
|
2016-09-30 21:20:22 +03:00
|
|
|
# 0 means missing, but we don't bother offsetting the index.
|
2017-03-07 19:15:18 +03:00
|
|
|
cdef hash_t key = hash_utf8(utf8_string, length)
|
2016-10-24 14:49:03 +03:00
|
|
|
cdef Utf8Str* value = <Utf8Str*>self._map.get(key)
|
|
|
|
if value is not NULL:
|
|
|
|
return value
|
|
|
|
value = <Utf8Str*>self._oov.get(key)
|
2016-09-30 21:20:22 +03:00
|
|
|
if value is not NULL:
|
|
|
|
return value
|
2016-10-24 14:49:03 +03:00
|
|
|
if self.is_frozen:
|
2016-11-01 15:27:13 +03:00
|
|
|
# OOV store uses 32 bit hashes. Pretty ugly :(
|
2017-03-07 19:15:18 +03:00
|
|
|
key32 = hash32_utf8(utf8_string, length)
|
2016-10-24 14:49:03 +03:00
|
|
|
# Important: Make the OOV store own the memory. That way it's trivial
|
|
|
|
# to flush them all.
|
|
|
|
value = <Utf8Str*>self._oov.mem.alloc(1, sizeof(Utf8Str))
|
|
|
|
value[0] = _allocate(self._oov.mem, <unsigned char*>utf8_string, length)
|
2016-11-01 15:27:13 +03:00
|
|
|
self._oov.set(key32, value)
|
2016-10-24 14:49:03 +03:00
|
|
|
return NULL
|
2016-09-30 21:20:22 +03:00
|
|
|
|
2015-07-20 12:26:46 +03:00
|
|
|
if self.size == self._resize_at:
|
|
|
|
self._realloc()
|
2016-09-30 21:20:22 +03:00
|
|
|
self.c[self.size] = _allocate(self.mem, <unsigned char*>utf8_string, length)
|
2015-07-20 12:26:46 +03:00
|
|
|
self._map.set(key, <void*>&self.c[self.size])
|
|
|
|
self.size += 1
|
|
|
|
return &self.c[self.size-1]
|
2014-12-19 22:42:01 +03:00
|
|
|
|
2015-10-22 13:13:03 +03:00
|
|
|
def dump(self, file_):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""Save the strings to a JSON file.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
file_ (buffer): The file to save the strings.
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2017-03-07 19:15:18 +03:00
|
|
|
string_data = ujson.dumps(list(self))
|
2015-10-22 13:13:03 +03:00
|
|
|
if not isinstance(string_data, unicode):
|
|
|
|
string_data = string_data.decode('utf8')
|
2016-10-24 14:49:03 +03:00
|
|
|
# TODO: OOV?
|
2015-10-22 13:13:03 +03:00
|
|
|
file_.write(string_data)
|
|
|
|
|
|
|
|
def load(self, file_):
|
2016-11-01 14:25:36 +03:00
|
|
|
"""Load the strings from a JSON file.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
file_ (buffer): The file from which to load the strings.
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2017-03-07 19:15:18 +03:00
|
|
|
strings = ujson.load(file_)
|
2015-08-23 21:49:18 +03:00
|
|
|
if strings == ['']:
|
|
|
|
return None
|
2014-12-19 22:42:01 +03:00
|
|
|
cdef unicode string
|
2016-03-24 17:09:55 +03:00
|
|
|
for string in strings:
|
|
|
|
# explicit None/len check instead of simple truth testing
|
|
|
|
# (bug in Cython <= 0.23.4)
|
|
|
|
if string is not None and len(string):
|
2016-10-24 14:49:03 +03:00
|
|
|
self.intern_unicode(string)
|
2015-07-20 12:26:46 +03:00
|
|
|
|
|
|
|
def _realloc(self):
|
|
|
|
# We want to map straight to pointers, but they'll be invalidated if
|
|
|
|
# we resize our array. So, first we remap to indices, then we resize,
|
|
|
|
# then we can acquire the new pointers.
|
|
|
|
cdef Pool tmp_mem = Pool()
|
2016-09-30 21:20:22 +03:00
|
|
|
keys = <key_t*>tmp_mem.alloc(self.size, sizeof(key_t))
|
|
|
|
cdef key_t key
|
2015-11-05 14:28:26 +03:00
|
|
|
cdef void* value
|
2015-07-20 12:26:46 +03:00
|
|
|
cdef const Utf8Str ptr
|
2015-11-05 14:28:26 +03:00
|
|
|
cdef int i = 0
|
|
|
|
cdef size_t offset
|
|
|
|
while map_iter(self._map.c_map, &i, &key, &value):
|
2015-07-20 12:26:46 +03:00
|
|
|
# Find array index with pointer arithmetic
|
2015-11-05 14:28:26 +03:00
|
|
|
offset = ((<Utf8Str*>value) - self.c)
|
|
|
|
keys[offset] = key
|
2017-03-07 19:15:18 +03:00
|
|
|
|
2015-07-20 12:26:46 +03:00
|
|
|
self._resize_at *= 2
|
|
|
|
cdef size_t new_size = self._resize_at * sizeof(Utf8Str)
|
|
|
|
self.c = <Utf8Str*>self.mem.realloc(self.c, new_size)
|
|
|
|
|
|
|
|
self._map = PreshMap(self.size)
|
|
|
|
for i in range(self.size):
|
2015-11-05 14:28:26 +03:00
|
|
|
if keys[i]:
|
|
|
|
self._map.set(keys[i], &self.c[i])
|