2014-12-19 22:54:03 +03:00
|
|
|
from libc.stdio cimport fopen, fclose, fread, fwrite, FILE
|
2014-12-24 09:42:00 +03:00
|
|
|
from libc.string cimport memset
|
2014-12-19 22:54:03 +03:00
|
|
|
|
|
|
|
from os import path
|
2015-01-12 02:26:22 +03:00
|
|
|
import codecs
|
2014-12-19 22:54:03 +03:00
|
|
|
|
|
|
|
from .lexeme cimport EMPTY_LEXEME
|
2015-01-13 16:03:48 +03:00
|
|
|
from .lexeme cimport set_lex_struct_props
|
2015-01-12 03:23:44 +03:00
|
|
|
from .lexeme cimport Lexeme_cinit
|
2014-12-19 23:56:26 +03:00
|
|
|
from .strings cimport slice_unicode
|
2015-01-12 02:26:22 +03:00
|
|
|
from .strings cimport hash_string
|
2015-01-05 19:18:22 +03:00
|
|
|
from .orth cimport word_shape
|
2014-12-24 09:42:00 +03:00
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
DEF MAX_VEC_SIZE = 100000
|
2014-12-24 09:42:00 +03:00
|
|
|
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef float[MAX_VEC_SIZE] EMPTY_VEC
|
|
|
|
memset(EMPTY_VEC, 0, sizeof(EMPTY_VEC))
|
|
|
|
memset(&EMPTY_LEXEME, 0, sizeof(LexemeC))
|
|
|
|
EMPTY_LEXEME.vec = EMPTY_VEC
|
|
|
|
|
|
|
|
|
2014-12-19 22:54:03 +03:00
|
|
|
cdef class Vocab:
|
2015-01-12 02:26:22 +03:00
|
|
|
'''A map container for a language's LexemeC structs.
|
2014-12-19 22:54:03 +03:00
|
|
|
'''
|
2014-12-23 03:40:32 +03:00
|
|
|
def __init__(self, data_dir=None, get_lex_props=None):
|
2014-12-19 22:54:03 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self._map = PreshMap(2 ** 20)
|
|
|
|
self.strings = StringStore()
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
2015-01-13 16:03:48 +03:00
|
|
|
self.lexeme_props_getter = get_lex_props
|
2014-12-19 22:54:03 +03:00
|
|
|
|
2014-12-23 03:40:32 +03: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-02 17:59:22 +03:00
|
|
|
self.strings.load(path.join(data_dir, 'strings.txt'))
|
2015-01-12 02:26:22 +03:00
|
|
|
self.load_lexemes(path.join(data_dir, 'lexemes.bin'))
|
|
|
|
#self.load_vectors(path.join(data_dir, 'deps.words'))
|
2014-12-20 21:36:29 +03:00
|
|
|
|
2014-12-19 22:54:03 +03:00
|
|
|
def __len__(self):
|
2014-12-27 10:45:16 +03:00
|
|
|
"""The current number of lexemes stored."""
|
2014-12-19 22:54:03 +03:00
|
|
|
return self.lexemes.size()
|
|
|
|
|
2015-01-13 16:03:48 +03:00
|
|
|
cdef const LexemeC* get(self, Pool mem, UniStr* c_str) except NULL:
|
2015-01-12 02:26:22 +03:00
|
|
|
'''Get a pointer to a LexemeC from the lexicon, creating a new Lexeme
|
2014-12-19 22:54:03 +03: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-12 02:26:22 +03:00
|
|
|
cdef LexemeC* lex
|
2015-01-13 16:03:48 +03:00
|
|
|
lex = <LexemeC*>self._map.get(c_str.key)
|
2014-12-19 22:54:03 +03:00
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
2015-01-13 16:03:48 +03:00
|
|
|
if c_str.n < 3:
|
2014-12-19 22:54:03 +03:00
|
|
|
mem = self.mem
|
2015-01-13 16:03:48 +03:00
|
|
|
cdef unicode py_str = c_str.chars[:c_str.n]
|
2015-01-12 02:26:22 +03:00
|
|
|
lex = <LexemeC*>mem.alloc(sizeof(LexemeC), 1)
|
2015-01-13 16:03:48 +03:00
|
|
|
props = self.lexeme_props_getter(py_str)
|
|
|
|
set_lex_struct_props(lex, props, self.strings)
|
2014-12-19 22:54:03 +03:00
|
|
|
if mem is self.mem:
|
2015-01-13 16:03:48 +03:00
|
|
|
lex.id = self.lexemes.size()
|
|
|
|
self._add_lex_to_vocab(c_str.key, lex)
|
2014-12-19 22:54:03 +03:00
|
|
|
else:
|
2015-01-13 16:03:48 +03:00
|
|
|
lex.id = 1
|
2014-12-19 22:54:03 +03:00
|
|
|
return lex
|
|
|
|
|
2015-01-13 16:03:48 +03: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 22:54:03 +03:00
|
|
|
def __getitem__(self, id_or_string):
|
|
|
|
'''Retrieve a lexeme, given an int ID or a unicode string. If a previously
|
2015-01-13 16:03:48 +03:00
|
|
|
unseen unicode string is given, a new lexeme is created and stored.
|
2014-12-19 22:54:03 +03:00
|
|
|
|
|
|
|
Args:
|
2015-01-14 16:33:16 +03: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 22:54:03 +03:00
|
|
|
|
|
|
|
Returns:
|
2015-01-14 16:33:16 +03:00
|
|
|
lexeme (Lexeme):
|
|
|
|
An instance of the Lexeme Python class, with data copied on
|
|
|
|
instantiation.
|
2014-12-19 22:54:03 +03:00
|
|
|
'''
|
2015-01-13 16:03:48 +03:00
|
|
|
cdef UniStr c_str
|
2015-01-12 03:23:44 +03:00
|
|
|
cdef const LexemeC* lexeme
|
2014-12-19 22:54:03 +03:00
|
|
|
if type(id_or_string) == int:
|
|
|
|
if id_or_string >= self.lexemes.size():
|
|
|
|
raise IndexError
|
2015-01-12 03:23:44 +03:00
|
|
|
lexeme = self.lexemes.at(id_or_string)
|
2015-01-14 16:33:16 +03:00
|
|
|
elif type(id_or_string) == unicode:
|
2015-01-13 16:03:48 +03:00
|
|
|
slice_unicode(&c_str, id_or_string, 0, len(id_or_string))
|
|
|
|
lexeme = self.get(self.mem, &c_str)
|
2015-01-14 16:33:16 +03:00
|
|
|
else:
|
|
|
|
raise ValueError("Vocab unable to map type: %s. Maps unicode --> int or int --> unicode" % str(type(id_or_string)))
|
2015-01-12 03:23:44 +03:00
|
|
|
return Lexeme_cinit(lexeme, self.strings)
|
2014-12-19 22:54:03 +03:00
|
|
|
|
2015-01-13 16:03:48 +03: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)
|
|
|
|
set_lex_struct_props(lex, props, self.strings)
|
|
|
|
assert lex.sic < 1000000
|
2014-12-19 22:54:03 +03: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-12 02:26:22 +03:00
|
|
|
lexeme = <LexemeC*>self._map.c_map.cells[i].value
|
2014-12-19 22:54:03 +03:00
|
|
|
st = fwrite(&key, sizeof(key), 1, fp)
|
|
|
|
assert st == 1
|
2015-01-12 02:26:22 +03:00
|
|
|
st = fwrite(lexeme, sizeof(LexemeC), 1, fp)
|
2014-12-19 22:54:03 +03:00
|
|
|
assert st == 1
|
|
|
|
st = fclose(fp)
|
|
|
|
assert st == 0
|
|
|
|
|
2015-01-12 02:26:22 +03:00
|
|
|
def load_lexemes(self, loc):
|
2014-12-19 22:54:03 +03:00
|
|
|
if not path.exists(loc):
|
2015-01-12 02:26:22 +03:00
|
|
|
raise IOError('LexemeCs file not found at %s' % loc)
|
2014-12-19 22:54:03 +03:00
|
|
|
cdef bytes bytes_loc = loc.encode('utf8') if type(loc) == unicode else loc
|
|
|
|
cdef FILE* fp = fopen(<char*>bytes_loc, 'rb')
|
|
|
|
assert fp != NULL
|
|
|
|
cdef size_t st
|
2015-01-12 02:26:22 +03:00
|
|
|
cdef LexemeC* lexeme
|
2014-12-19 22:54:03 +03:00
|
|
|
cdef hash_t key
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
st = fread(&key, sizeof(key), 1, fp)
|
|
|
|
if st != 1:
|
|
|
|
break
|
2015-01-12 02:26:22 +03:00
|
|
|
lexeme = <LexemeC*>self.mem.alloc(sizeof(LexemeC), 1)
|
2015-01-13 16:03:48 +03:00
|
|
|
lexeme.vec = EMPTY_VEC
|
2015-01-12 02:26:22 +03:00
|
|
|
st = fread(lexeme, sizeof(LexemeC), 1, fp)
|
2014-12-19 22:54:03 +03:00
|
|
|
if st != 1:
|
|
|
|
break
|
|
|
|
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-12 02:26:22 +03:00
|
|
|
|
|
|
|
def load_vectors(self, loc):
|
|
|
|
cdef int i
|
|
|
|
cdef unicode line
|
|
|
|
cdef unicode word
|
|
|
|
cdef unicode val_str
|
|
|
|
cdef hash_t key
|
|
|
|
cdef LexemeC* lex
|
|
|
|
cdef float* vec
|
|
|
|
|
|
|
|
with codecs.open(loc, 'r', 'utf8') as file_:
|
|
|
|
for line in file_:
|
|
|
|
pieces = line.split()
|
|
|
|
word = pieces.pop(0)
|
|
|
|
if len(pieces) >= MAX_VEC_SIZE:
|
|
|
|
sizes = (len(pieces), MAX_VEC_SIZE)
|
|
|
|
msg = ("Your vector is %d elements."
|
|
|
|
"The compile-time limit is %d elements." % sizes)
|
|
|
|
raise ValueError(msg)
|
|
|
|
key = hash_string(word)
|
|
|
|
lex = <LexemeC*>self._map.get(key)
|
|
|
|
if lex is not NULL:
|
|
|
|
vec = <float*>self.mem.alloc(len(pieces), sizeof(float))
|
|
|
|
for i, val_str in enumerate(pieces):
|
|
|
|
vec[i] = float(val_str)
|
|
|
|
lex.vec = vec
|