2014-12-19 19:54:03 +00:00
|
|
|
from libc.stdio cimport fopen, fclose, fread, fwrite, FILE
|
2014-12-24 06:42:00 +00:00
|
|
|
from libc.string cimport memset
|
2015-01-17 05:21:17 +00:00
|
|
|
from libc.stdint cimport int32_t
|
2014-12-19 19:54:03 +00:00
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
import bz2
|
2014-12-19 19:54:03 +00:00
|
|
|
from os import path
|
2015-01-11 23:26:22 +00:00
|
|
|
import codecs
|
2014-12-19 19:54:03 +00:00
|
|
|
|
|
|
|
from .lexeme cimport EMPTY_LEXEME
|
2015-01-13 13:03:48 +00:00
|
|
|
from .lexeme cimport set_lex_struct_props
|
2015-01-17 05:21:17 +00:00
|
|
|
from .lexeme cimport Lexeme
|
2014-12-19 20:56:26 +00:00
|
|
|
from .strings cimport slice_unicode
|
2015-01-11 23:26:22 +00:00
|
|
|
from .strings cimport hash_string
|
2015-01-05 16:18:22 +00:00
|
|
|
from .orth cimport word_shape
|
2015-01-31 05:38:58 +00:00
|
|
|
from .typedefs cimport attr_t
|
2014-12-24 06:42:00 +00:00
|
|
|
|
2015-01-17 05:21:17 +00:00
|
|
|
from cymem.cymem cimport Address
|
|
|
|
|
2014-12-24 06:42:00 +00:00
|
|
|
|
2015-01-11 23:26:22 +00:00
|
|
|
DEF MAX_VEC_SIZE = 100000
|
2014-12-24 06:42:00 +00:00
|
|
|
|
|
|
|
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef float[MAX_VEC_SIZE] EMPTY_VEC
|
|
|
|
memset(EMPTY_VEC, 0, sizeof(EMPTY_VEC))
|
|
|
|
memset(&EMPTY_LEXEME, 0, sizeof(LexemeC))
|
2015-01-21 15:06:22 +00:00
|
|
|
EMPTY_LEXEME.repvec = EMPTY_VEC
|
2015-01-11 23:26:22 +00:00
|
|
|
|
|
|
|
|
2014-12-19 19:54:03 +00:00
|
|
|
cdef class Vocab:
|
2015-01-11 23:26:22 +00:00
|
|
|
'''A map container for a language's LexemeC structs.
|
2014-12-19 19:54:03 +00:00
|
|
|
'''
|
2014-12-23 00:40:32 +00:00
|
|
|
def __init__(self, data_dir=None, get_lex_props=None):
|
2014-12-19 19:54:03 +00:00
|
|
|
self.mem = Pool()
|
|
|
|
self._map = PreshMap(2 ** 20)
|
|
|
|
self.strings = StringStore()
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
2015-01-13 13:03:48 +00:00
|
|
|
self.lexeme_props_getter = get_lex_props
|
2014-12-19 19:54:03 +00:00
|
|
|
|
2014-12-23 00:40:32 +00:00
|
|
|
if data_dir is not None:
|
|
|
|
if not path.exists(data_dir):
|
|
|
|
raise IOError("Directory %s not found -- cannot load Vocab." % data_dir)
|
|
|
|
if data_dir is not None:
|
|
|
|
if not path.isdir(data_dir):
|
|
|
|
raise IOError("Path %s is a file, not a dir -- cannot load Vocab." % data_dir)
|
2015-01-31 05:38:58 +00:00
|
|
|
self.load_lexemes(path.join(data_dir, 'strings.txt'),
|
|
|
|
path.join(data_dir, 'lexemes.bin'))
|
2015-01-25 07:16:48 +00:00
|
|
|
if path.exists(path.join(data_dir, 'vec.bin')):
|
|
|
|
self.load_rep_vectors(path.join(data_dir, 'vec.bin'))
|
2014-12-20 18:36:29 +00:00
|
|
|
|
2014-12-19 19:54:03 +00:00
|
|
|
def __len__(self):
|
2014-12-27 07:45:16 +00:00
|
|
|
"""The current number of lexemes stored."""
|
2014-12-19 19:54:03 +00:00
|
|
|
return self.lexemes.size()
|
|
|
|
|
2015-01-13 13:03:48 +00:00
|
|
|
cdef const LexemeC* get(self, Pool mem, UniStr* c_str) except NULL:
|
2015-01-11 23:26:22 +00:00
|
|
|
'''Get a pointer to a LexemeC from the lexicon, creating a new Lexeme
|
2014-12-19 19:54:03 +00:00
|
|
|
if necessary, using memory acquired from the given pool. If the pool
|
|
|
|
is the lexicon's own memory, the lexeme is saved in the lexicon.'''
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef LexemeC* lex
|
2015-01-13 13:03:48 +00:00
|
|
|
lex = <LexemeC*>self._map.get(c_str.key)
|
2014-12-19 19:54:03 +00:00
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
2015-01-13 13:03:48 +00:00
|
|
|
if c_str.n < 3:
|
2014-12-19 19:54:03 +00:00
|
|
|
mem = self.mem
|
2015-01-13 13:03:48 +00:00
|
|
|
cdef unicode py_str = c_str.chars[:c_str.n]
|
2015-01-11 23:26:22 +00:00
|
|
|
lex = <LexemeC*>mem.alloc(sizeof(LexemeC), 1)
|
2015-01-13 13:03:48 +00:00
|
|
|
props = self.lexeme_props_getter(py_str)
|
2015-01-17 05:21:17 +00:00
|
|
|
set_lex_struct_props(lex, props, self.strings, EMPTY_VEC)
|
2014-12-19 19:54:03 +00:00
|
|
|
if mem is self.mem:
|
2015-01-13 13:03:48 +00:00
|
|
|
lex.id = self.lexemes.size()
|
|
|
|
self._add_lex_to_vocab(c_str.key, lex)
|
2014-12-19 19:54:03 +00:00
|
|
|
else:
|
2015-01-13 13:03:48 +00:00
|
|
|
lex.id = 1
|
2014-12-19 19:54:03 +00:00
|
|
|
return lex
|
|
|
|
|
2015-01-13 13:03:48 +00:00
|
|
|
cdef int _add_lex_to_vocab(self, hash_t key, const LexemeC* lex) except -1:
|
|
|
|
self._map.set(key, <void*>lex)
|
|
|
|
while self.lexemes.size() < (lex.id + 1):
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
|
|
|
self.lexemes[lex.id] = lex
|
|
|
|
|
2014-12-19 19:54:03 +00:00
|
|
|
def __getitem__(self, id_or_string):
|
|
|
|
'''Retrieve a lexeme, given an int ID or a unicode string. If a previously
|
2015-01-13 13:03:48 +00:00
|
|
|
unseen unicode string is given, a new lexeme is created and stored.
|
2014-12-19 19:54:03 +00:00
|
|
|
|
|
|
|
Args:
|
2015-01-14 13:33:16 +00:00
|
|
|
id_or_string (int or unicode):
|
|
|
|
The integer ID of a word, or its unicode string. If an int >= Lexicon.size,
|
|
|
|
IndexError is raised. If id_or_string is neither an int nor a unicode string,
|
|
|
|
ValueError is raised.
|
2014-12-19 19:54:03 +00:00
|
|
|
|
|
|
|
Returns:
|
2015-01-14 13:33:16 +00:00
|
|
|
lexeme (Lexeme):
|
|
|
|
An instance of the Lexeme Python class, with data copied on
|
|
|
|
instantiation.
|
2014-12-19 19:54:03 +00:00
|
|
|
'''
|
2015-01-13 13:03:48 +00:00
|
|
|
cdef UniStr c_str
|
2015-01-12 00:23:44 +00:00
|
|
|
cdef const LexemeC* lexeme
|
2014-12-19 19:54:03 +00:00
|
|
|
if type(id_or_string) == int:
|
|
|
|
if id_or_string >= self.lexemes.size():
|
|
|
|
raise IndexError
|
2015-01-12 00:23:44 +00:00
|
|
|
lexeme = self.lexemes.at(id_or_string)
|
2015-01-14 13:33:16 +00:00
|
|
|
elif type(id_or_string) == unicode:
|
2015-01-13 13:03:48 +00:00
|
|
|
slice_unicode(&c_str, id_or_string, 0, len(id_or_string))
|
|
|
|
lexeme = self.get(self.mem, &c_str)
|
2015-01-14 13:33:16 +00:00
|
|
|
else:
|
2015-01-25 12:01:30 +00:00
|
|
|
raise ValueError("Vocab unable to map type: %s. Maps unicode --> Lexeme or int --> Lexeme" % str(type(id_or_string)))
|
2015-01-17 05:21:17 +00:00
|
|
|
return Lexeme.from_ptr(lexeme, self.strings)
|
2014-12-19 19:54:03 +00:00
|
|
|
|
2015-01-13 13:03:48 +00:00
|
|
|
def __setitem__(self, unicode py_str, dict props):
|
|
|
|
cdef UniStr c_str
|
|
|
|
slice_unicode(&c_str, py_str, 0, len(py_str))
|
|
|
|
cdef LexemeC* lex
|
|
|
|
lex = <LexemeC*>self._map.get(c_str.key)
|
|
|
|
if lex == NULL:
|
|
|
|
lex = <LexemeC*>self.mem.alloc(sizeof(LexemeC), 1)
|
|
|
|
lex.id = self.lexemes.size()
|
|
|
|
self._add_lex_to_vocab(c_str.key, lex)
|
2015-01-17 05:21:17 +00:00
|
|
|
set_lex_struct_props(lex, props, self.strings, EMPTY_VEC)
|
2014-12-19 19:54:03 +00:00
|
|
|
|
|
|
|
def dump(self, loc):
|
|
|
|
if path.exists(loc):
|
|
|
|
assert not path.isdir(loc)
|
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
|
|
|
cdef FILE* fp = fopen(<char*>bytes_loc, 'wb')
|
|
|
|
assert fp != NULL
|
|
|
|
cdef size_t st
|
|
|
|
cdef hash_t key
|
|
|
|
for i in range(self._map.length):
|
|
|
|
key = self._map.c_map.cells[i].key
|
|
|
|
if key == 0:
|
|
|
|
continue
|
2015-01-11 23:26:22 +00:00
|
|
|
lexeme = <LexemeC*>self._map.c_map.cells[i].value
|
2015-01-31 05:38:58 +00:00
|
|
|
st = fwrite(&lexeme.orth, sizeof(lexeme.orth), 1, fp)
|
2014-12-19 19:54:03 +00:00
|
|
|
assert st == 1
|
2015-01-11 23:26:22 +00:00
|
|
|
st = fwrite(lexeme, sizeof(LexemeC), 1, fp)
|
2014-12-19 19:54:03 +00:00
|
|
|
assert st == 1
|
|
|
|
st = fclose(fp)
|
|
|
|
assert st == 0
|
|
|
|
|
2015-01-31 05:38:58 +00:00
|
|
|
def load_lexemes(self, strings_loc, loc):
|
|
|
|
self.strings.load(strings_loc)
|
2014-12-19 19:54:03 +00:00
|
|
|
if not path.exists(loc):
|
2015-01-11 23:26:22 +00:00
|
|
|
raise IOError('LexemeCs file not found at %s' % loc)
|
2014-12-19 19:54:03 +00:00
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
2015-02-01 02:14:04 +00:00
|
|
|
cdef FILE* fp = fopen(<char*>bytes_loc, b'rb')
|
2014-12-19 19:54:03 +00:00
|
|
|
assert fp != NULL
|
|
|
|
cdef size_t st
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef LexemeC* lexeme
|
2015-01-31 05:38:58 +00:00
|
|
|
cdef attr_t orth
|
2014-12-19 19:54:03 +00:00
|
|
|
cdef hash_t key
|
2015-01-31 05:38:58 +00:00
|
|
|
cdef unicode py_str
|
2014-12-19 19:54:03 +00:00
|
|
|
i = 0
|
|
|
|
while True:
|
2015-01-31 05:38:58 +00:00
|
|
|
st = fread(&orth, sizeof(orth), 1, fp)
|
2014-12-19 19:54:03 +00:00
|
|
|
if st != 1:
|
|
|
|
break
|
2015-01-11 23:26:22 +00:00
|
|
|
lexeme = <LexemeC*>self.mem.alloc(sizeof(LexemeC), 1)
|
2015-01-17 05:21:17 +00:00
|
|
|
# Copies data from the file into the lexeme
|
2015-01-11 23:26:22 +00:00
|
|
|
st = fread(lexeme, sizeof(LexemeC), 1, fp)
|
2015-01-21 15:06:22 +00:00
|
|
|
lexeme.repvec = EMPTY_VEC
|
2014-12-19 19:54:03 +00:00
|
|
|
if st != 1:
|
|
|
|
break
|
2015-01-31 05:38:58 +00:00
|
|
|
assert orth == lexeme.orth
|
|
|
|
py_str = self.strings[orth]
|
|
|
|
key = hash_string(py_str)
|
2014-12-19 19:54:03 +00:00
|
|
|
self._map.set(key, lexeme)
|
|
|
|
while self.lexemes.size() < (lexeme.id + 1):
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
|
|
|
self.lexemes[lexeme.id] = lexeme
|
|
|
|
i += 1
|
|
|
|
fclose(fp)
|
2015-01-17 05:21:17 +00:00
|
|
|
|
2015-01-21 15:06:22 +00:00
|
|
|
def load_rep_vectors(self, loc):
|
2015-02-01 02:14:04 +00:00
|
|
|
file_ = _CFile(loc, b'rb')
|
2015-01-17 05:21:17 +00:00
|
|
|
cdef int32_t word_len
|
|
|
|
cdef int32_t vec_len
|
|
|
|
cdef float* vec
|
|
|
|
cdef Address mem
|
|
|
|
cdef id_t string_id
|
|
|
|
cdef bytes py_word
|
|
|
|
cdef vector[float*] vectors
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef int i
|
2015-01-17 05:21:17 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
file_.read(&word_len, sizeof(word_len), 1)
|
|
|
|
except IOError:
|
|
|
|
break
|
|
|
|
file_.read(&vec_len, sizeof(vec_len), 1)
|
|
|
|
|
|
|
|
mem = Address(word_len, sizeof(char))
|
|
|
|
chars = <char*>mem.ptr
|
|
|
|
vec = <float*>self.mem.alloc(vec_len, sizeof(float))
|
|
|
|
|
|
|
|
file_.read(chars, sizeof(char), word_len)
|
|
|
|
file_.read(vec, sizeof(float), vec_len)
|
|
|
|
|
|
|
|
string_id = self.strings[chars[:word_len]]
|
|
|
|
while string_id >= vectors.size():
|
|
|
|
vectors.push_back(EMPTY_VEC)
|
|
|
|
assert vec != NULL
|
|
|
|
vectors[string_id] = vec
|
2015-01-11 23:26:22 +00:00
|
|
|
cdef LexemeC* lex
|
2015-01-17 05:21:17 +00:00
|
|
|
for i in range(self.lexemes.size()):
|
|
|
|
# Cast away the const, cos we can modify our lexemes
|
|
|
|
lex = <LexemeC*>self.lexemes[i]
|
2015-01-23 19:17:03 +00:00
|
|
|
if lex.lower < vectors.size():
|
|
|
|
lex.repvec = vectors[lex.lower]
|
2015-01-17 05:21:17 +00:00
|
|
|
else:
|
2015-01-21 15:06:22 +00:00
|
|
|
lex.repvec = EMPTY_VEC
|
2015-01-17 05:21:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_binary_vectors(in_loc, out_loc):
|
|
|
|
cdef _CFile out_file = _CFile(out_loc, 'wb')
|
|
|
|
cdef Address mem
|
|
|
|
cdef int32_t word_len
|
|
|
|
cdef int32_t vec_len
|
|
|
|
cdef char* chars
|
|
|
|
with bz2.BZ2File(in_loc, 'r') as file_:
|
|
|
|
for line in file_:
|
|
|
|
pieces = line.split()
|
|
|
|
word = pieces.pop(0)
|
|
|
|
mem = Address(len(pieces), sizeof(float))
|
|
|
|
vec = <float*>mem.ptr
|
|
|
|
for i, val_str in enumerate(pieces):
|
|
|
|
vec[i] = float(val_str)
|
|
|
|
|
|
|
|
word_len = len(word)
|
|
|
|
vec_len = len(pieces)
|
|
|
|
|
|
|
|
out_file.write(sizeof(word_len), 1, &word_len)
|
|
|
|
out_file.write(sizeof(vec_len), 1, &vec_len)
|
|
|
|
|
|
|
|
chars = <char*>word
|
|
|
|
out_file.write(sizeof(char), len(word), chars)
|
|
|
|
out_file.write(sizeof(float), vec_len, vec)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class _CFile:
|
|
|
|
cdef FILE* fp
|
2015-02-01 02:14:04 +00:00
|
|
|
def __init__(self, loc, bytes mode):
|
2015-01-17 05:21:17 +00:00
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
|
|
|
self.fp = fopen(<char*>bytes_loc, mode)
|
|
|
|
if self.fp == NULL:
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
def __dealloc__(self):
|
|
|
|
fclose(self.fp)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
fclose(self.fp)
|
|
|
|
|
|
|
|
cdef int read(self, void* dest, size_t elem_size, size_t n) except -1:
|
|
|
|
st = fread(dest, elem_size, n, self.fp)
|
|
|
|
if st != n:
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
cdef int write(self, size_t elem_size, size_t n, void* data) except -1:
|
|
|
|
st = fwrite(data, elem_size, n, self.fp)
|
|
|
|
if st != n:
|
|
|
|
raise IOError
|
|
|
|
|
|
|
|
cdef int write_unicode(self, unicode value):
|
|
|
|
cdef bytes py_bytes = value.encode('utf8')
|
|
|
|
cdef char* chars = <char*>py_bytes
|
|
|
|
self.write(sizeof(char), len(py_bytes), chars)
|