2014-07-07 09:36:43 +04:00
|
|
|
# cython: profile=True
|
2014-08-20 15:39:39 +04:00
|
|
|
# cython: embedsignature=True
|
2014-07-05 22:51:42 +04:00
|
|
|
from __future__ import unicode_literals
|
2014-07-07 06:21:06 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
import json
|
2014-09-12 02:18:31 +04:00
|
|
|
import random
|
2014-07-07 09:36:43 +04:00
|
|
|
from os import path
|
2014-09-25 20:22:52 +04:00
|
|
|
import re
|
2014-08-16 21:59:38 +04:00
|
|
|
|
2014-09-16 15:16:02 +04:00
|
|
|
from cython.operator cimport preincrement as preinc
|
|
|
|
from cython.operator cimport dereference as deref
|
2014-10-29 15:19:38 +03:00
|
|
|
from libc.stdio cimport fopen, fclose, fread, fwrite, FILE
|
2014-09-16 15:16:02 +04:00
|
|
|
|
2014-10-29 15:19:38 +03:00
|
|
|
from cymem.cymem cimport Pool
|
2014-10-23 17:59:17 +04:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-09-26 20:40:03 +04:00
|
|
|
from preshed.maps cimport PreshMap
|
2014-10-23 17:59:17 +04:00
|
|
|
|
|
|
|
from .lexeme cimport Lexeme
|
2014-10-31 09:43:25 +03:00
|
|
|
from .lexeme cimport EMPTY_LEXEME
|
2014-10-29 15:19:38 +03:00
|
|
|
from .lexeme cimport init as lexeme_init
|
2014-12-05 07:56:14 +03:00
|
|
|
from .lexeme cimport check_flag
|
2014-10-23 17:59:17 +04:00
|
|
|
|
2014-12-03 03:04:00 +03:00
|
|
|
from .utf8string cimport slice_unicode
|
|
|
|
|
2014-10-23 17:59:17 +04:00
|
|
|
from . import util
|
|
|
|
from .util import read_lang_data
|
|
|
|
from .tokens import Tokens
|
2014-09-15 03:31:44 +04:00
|
|
|
|
|
|
|
|
2014-07-07 14:47:21 +04:00
|
|
|
cdef class Language:
|
2014-10-30 10:01:27 +03:00
|
|
|
def __init__(self, name):
|
2014-07-07 14:47:21 +04:00
|
|
|
self.name = name
|
2014-10-30 10:01:27 +03:00
|
|
|
self.mem = Pool()
|
|
|
|
self._cache = PreshMap(2 ** 25)
|
|
|
|
self._specials = PreshMap(2 ** 16)
|
2014-10-30 10:14:42 +03:00
|
|
|
rules, prefix, suffix, infix = util.read_lang_data(name)
|
2014-10-30 10:01:27 +03:00
|
|
|
self._prefix_re = re.compile(prefix)
|
|
|
|
self._suffix_re = re.compile(suffix)
|
|
|
|
self._infix_re = re.compile(infix)
|
2014-12-07 15:52:41 +03:00
|
|
|
self.lexicon = Lexicon(self.get_props)
|
2014-08-29 03:59:23 +04:00
|
|
|
self._load_special_tokenization(rules)
|
2014-12-07 07:29:41 +03:00
|
|
|
self.pos_tagger = None
|
2014-10-10 12:17:22 +04:00
|
|
|
|
2014-12-03 07:44:25 +03:00
|
|
|
def load(self):
|
|
|
|
self.lexicon.load(path.join(util.DATA_DIR, self.name, 'lexemes'))
|
|
|
|
self.lexicon.strings.load(path.join(util.DATA_DIR, self.name, 'strings'))
|
2014-12-07 14:05:57 +03:00
|
|
|
if path.exists(path.join(util.DATA_DIR, self.name, 'pos')):
|
|
|
|
self.pos_tagger = Tagger(path.join(util.DATA_DIR, self.name, 'pos'))
|
2014-12-03 07:44:25 +03:00
|
|
|
|
2014-11-11 15:43:14 +03:00
|
|
|
cpdef Tokens tokens_from_list(self, list strings):
|
|
|
|
cdef int length = sum([len(s) for s in strings])
|
|
|
|
cdef Tokens tokens = Tokens(self.lexicon.strings, length)
|
|
|
|
if length == 0:
|
|
|
|
return tokens
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr string_struct
|
2014-11-11 15:43:14 +03:00
|
|
|
cdef unicode py_string
|
|
|
|
cdef int idx = 0
|
|
|
|
for i, py_string in enumerate(strings):
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&string_struct, py_string, 0, len(py_string))
|
2014-12-04 19:29:50 +03:00
|
|
|
tokens.push_back(idx, self.lexicon.get(tokens.mem, &string_struct))
|
2014-11-11 15:43:14 +03:00
|
|
|
idx += len(py_string) + 1
|
|
|
|
return tokens
|
|
|
|
|
2014-09-11 23:37:32 +04:00
|
|
|
cpdef Tokens tokenize(self, unicode string):
|
2014-08-27 19:15:39 +04:00
|
|
|
"""Tokenize a string.
|
|
|
|
|
2014-10-10 01:11:31 +04:00
|
|
|
The tokenization rules are defined in three places:
|
2014-08-20 15:39:39 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
* The data/<lang>/tokenization table, which handles special cases like contractions;
|
2014-10-10 01:11:31 +04:00
|
|
|
* The data/<lang>/prefix file, used to build a regex to split off prefixes;
|
|
|
|
* The data/<lang>/suffix file, used to build a regex to split off suffixes.
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-08-20 15:39:39 +04:00
|
|
|
Args:
|
2014-08-27 19:15:39 +04:00
|
|
|
string (unicode): The string to be tokenized.
|
2014-08-20 15:39:39 +04:00
|
|
|
|
|
|
|
Returns:
|
2014-10-10 01:11:31 +04:00
|
|
|
tokens (Tokens): A Tokens object, giving access to a sequence of Lexemes.
|
2014-08-20 15:39:39 +04:00
|
|
|
"""
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int length = len(string)
|
2014-10-23 17:59:17 +04:00
|
|
|
cdef Tokens tokens = Tokens(self.lexicon.strings, length)
|
2014-09-11 14:28:38 +04:00
|
|
|
if length == 0:
|
2014-09-11 23:37:32 +04:00
|
|
|
return tokens
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int i = 0
|
2014-10-14 13:25:57 +04:00
|
|
|
cdef int start = 0
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef Py_UNICODE* chars = string
|
2014-10-14 13:25:57 +04:00
|
|
|
cdef bint in_ws = Py_UNICODE_ISSPACE(chars[0])
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr span
|
2014-10-14 13:25:57 +04:00
|
|
|
for i in range(1, length):
|
|
|
|
if Py_UNICODE_ISSPACE(chars[i]) != in_ws:
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&span, chars, start, i)
|
2014-12-03 07:44:25 +03:00
|
|
|
lexemes = <const Lexeme* const*>self._cache.get(span.key)
|
2014-10-14 11:22:41 +04:00
|
|
|
if lexemes != NULL:
|
|
|
|
tokens.extend(start, lexemes, 0)
|
|
|
|
else:
|
|
|
|
self._tokenize(tokens, &span, start, i)
|
2014-10-14 13:25:57 +04:00
|
|
|
in_ws = not in_ws
|
|
|
|
start = i
|
|
|
|
if chars[i] == ' ':
|
|
|
|
start += 1
|
2014-09-12 04:04:47 +04:00
|
|
|
i += 1
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&span, chars, start, i)
|
2014-12-03 07:44:25 +03:00
|
|
|
lexemes = <const Lexeme* const*>self._cache.get(span.key)
|
2014-10-14 11:22:41 +04:00
|
|
|
if lexemes != NULL:
|
|
|
|
tokens.extend(start, lexemes, 0)
|
|
|
|
else:
|
|
|
|
self._tokenize(tokens, &span, start, i)
|
2014-09-11 23:37:32 +04:00
|
|
|
return tokens
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef int _tokenize(self, Tokens tokens, UniStr* span, int start, int end) except -1:
|
2014-10-23 17:59:17 +04:00
|
|
|
cdef vector[Lexeme*] prefixes
|
|
|
|
cdef vector[Lexeme*] suffixes
|
|
|
|
cdef hash_t orig_key
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int orig_size
|
2014-10-14 11:22:41 +04:00
|
|
|
orig_key = span.key
|
2014-10-22 18:57:59 +04:00
|
|
|
orig_size = tokens.length
|
2014-10-14 11:22:41 +04:00
|
|
|
self._split_affixes(span, &prefixes, &suffixes)
|
|
|
|
self._attach_tokens(tokens, start, span, &prefixes, &suffixes)
|
2014-12-05 07:56:14 +03:00
|
|
|
self._save_cached(&tokens.data[orig_size], orig_key, tokens.length - orig_size)
|
2014-10-14 08:47:06 +04:00
|
|
|
|
2014-12-03 07:44:25 +03:00
|
|
|
cdef UniStr* _split_affixes(self, UniStr* string, vector[const Lexeme*] *prefixes,
|
|
|
|
vector[const Lexeme*] *suffixes) except NULL:
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef size_t i
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr prefix
|
|
|
|
cdef UniStr suffix
|
|
|
|
cdef UniStr minus_pre
|
|
|
|
cdef UniStr minus_suf
|
2014-09-16 20:01:46 +04:00
|
|
|
cdef size_t last_size = 0
|
|
|
|
while string.n != 0 and string.n != last_size:
|
|
|
|
last_size = string.n
|
|
|
|
pre_len = self._find_prefix(string.chars, string.n)
|
|
|
|
if pre_len != 0:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&prefix, string.chars, 0, pre_len)
|
|
|
|
slice_unicode(&minus_pre, string.chars, pre_len, string.n)
|
2014-09-16 20:01:46 +04:00
|
|
|
# Check whether we've hit a special-case
|
2014-10-30 10:01:27 +03:00
|
|
|
if minus_pre.n >= 1 and self._specials.get(minus_pre.key) != NULL:
|
2014-10-14 11:22:41 +04:00
|
|
|
string[0] = minus_pre
|
2014-12-04 19:29:50 +03:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
2014-09-16 20:01:46 +04:00
|
|
|
break
|
|
|
|
suf_len = self._find_suffix(string.chars, string.n)
|
|
|
|
if suf_len != 0:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&suffix, string.chars, string.n - suf_len, string.n)
|
|
|
|
slice_unicode(&minus_suf, string.chars, 0, string.n - suf_len)
|
2014-09-16 20:01:46 +04:00
|
|
|
# Check whether we've hit a special-case
|
2014-10-30 10:01:27 +03:00
|
|
|
if minus_suf.n >= 1 and self._specials.get(minus_suf.key) != NULL:
|
2014-10-14 11:22:41 +04:00
|
|
|
string[0] = minus_suf
|
2014-12-04 19:29:50 +03:00
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-09-16 20:01:46 +04:00
|
|
|
break
|
|
|
|
if pre_len and suf_len and (pre_len + suf_len) <= string.n:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(string, string.chars, pre_len, string.n - suf_len)
|
2014-12-04 19:29:50 +03:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-09-16 20:01:46 +04:00
|
|
|
elif pre_len:
|
2014-10-14 11:22:41 +04:00
|
|
|
string[0] = minus_pre
|
2014-12-04 19:29:50 +03:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
2014-09-16 20:01:46 +04:00
|
|
|
elif suf_len:
|
2014-10-14 11:22:41 +04:00
|
|
|
string[0] = minus_suf
|
2014-12-04 19:29:50 +03:00
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-10-30 10:01:27 +03:00
|
|
|
if self._specials.get(string.key):
|
2014-09-16 20:01:46 +04:00
|
|
|
break
|
2014-10-14 08:47:06 +04:00
|
|
|
return string
|
2014-09-16 20:01:46 +04:00
|
|
|
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int _attach_tokens(self, Tokens tokens,
|
2014-12-03 03:04:00 +03:00
|
|
|
int idx, UniStr* string,
|
2014-12-03 07:44:25 +03:00
|
|
|
vector[const Lexeme*] *prefixes,
|
|
|
|
vector[const Lexeme*] *suffixes) except -1:
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int split
|
2014-12-03 07:44:25 +03:00
|
|
|
cdef const Lexeme* const* lexemes
|
2014-10-23 17:59:17 +04:00
|
|
|
cdef Lexeme* lexeme
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr span
|
2014-10-23 19:23:42 +04:00
|
|
|
if prefixes.size():
|
|
|
|
idx = tokens.extend(idx, prefixes.data(), prefixes.size())
|
2014-10-14 08:47:06 +04:00
|
|
|
if string.n != 0:
|
2014-10-23 19:23:42 +04:00
|
|
|
|
2014-12-03 07:44:25 +03:00
|
|
|
lexemes = <const Lexeme* const*>self._cache.get(string.key)
|
2014-10-14 08:47:06 +04:00
|
|
|
if lexemes != NULL:
|
|
|
|
idx = tokens.extend(idx, lexemes, 0)
|
|
|
|
else:
|
|
|
|
split = self._find_infix(string.chars, string.n)
|
|
|
|
if split == 0 or split == -1:
|
2014-12-04 19:29:50 +03:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, string))
|
2014-10-14 08:47:06 +04:00
|
|
|
else:
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&span, string.chars, 0, split)
|
2014-12-04 19:29:50 +03:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&span, string.chars, split, split+1)
|
2014-12-04 19:29:50 +03:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&span, string.chars, split + 1, string.n)
|
2014-12-04 19:29:50 +03:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 07:44:25 +03:00
|
|
|
cdef vector[const Lexeme*].reverse_iterator it = suffixes.rbegin()
|
2014-09-16 15:16:02 +04:00
|
|
|
while it != suffixes.rend():
|
2014-10-14 08:47:06 +04:00
|
|
|
idx = tokens.push_back(idx, deref(it))
|
2014-09-16 15:16:02 +04:00
|
|
|
preinc(it)
|
|
|
|
|
2014-12-05 07:56:14 +03:00
|
|
|
cdef int _save_cached(self, const TokenC* tokens, hash_t key, int n) except -1:
|
2014-10-22 18:57:59 +04:00
|
|
|
cdef int i
|
2014-12-04 19:29:50 +03:00
|
|
|
for i in range(n):
|
2014-12-05 07:56:14 +03:00
|
|
|
if tokens[i].lex.id == 1:
|
2014-12-04 19:29:50 +03:00
|
|
|
return 0
|
|
|
|
lexemes = <const Lexeme**>self.mem.alloc(n + 1, sizeof(Lexeme**))
|
2014-10-22 18:57:59 +04:00
|
|
|
for i in range(n):
|
2014-12-05 07:56:14 +03:00
|
|
|
lexemes[i] = tokens[i].lex
|
2014-09-16 20:01:46 +04:00
|
|
|
lexemes[i + 1] = NULL
|
2014-10-30 10:01:27 +03:00
|
|
|
self._cache.set(key, lexemes)
|
2014-10-10 13:23:23 +04:00
|
|
|
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int _find_infix(self, Py_UNICODE* chars, size_t length) except -1:
|
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 10:01:27 +03:00
|
|
|
match = self._infix_re.search(string)
|
2014-10-14 08:47:06 +04:00
|
|
|
return match.start() if match is not None else 0
|
2014-09-16 20:01:46 +04:00
|
|
|
|
|
|
|
cdef int _find_prefix(self, Py_UNICODE* chars, size_t length) except -1:
|
2014-09-25 20:22:52 +04:00
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 10:01:27 +03:00
|
|
|
match = self._prefix_re.search(string)
|
2014-10-10 13:11:22 +04:00
|
|
|
return (match.end() - match.start()) if match is not None else 0
|
2014-09-25 20:22:52 +04:00
|
|
|
|
2014-10-14 08:47:06 +04:00
|
|
|
cdef int _find_suffix(self, Py_UNICODE* chars, size_t length) except -1:
|
2014-09-25 20:22:52 +04:00
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 10:01:27 +03:00
|
|
|
match = self._suffix_re.search(string)
|
2014-10-10 13:11:22 +04:00
|
|
|
return (match.end() - match.start()) if match is not None else 0
|
2014-08-16 05:22:03 +04:00
|
|
|
|
2014-08-29 03:59:23 +04:00
|
|
|
def _load_special_tokenization(self, token_rules):
|
2014-08-20 15:39:39 +04:00
|
|
|
'''Load special-case tokenization rules.
|
|
|
|
|
2014-10-30 10:01:27 +03:00
|
|
|
Loads special-case tokenization rules into the Language._cache cache,
|
2014-08-20 15:39:39 +04:00
|
|
|
read from data/<lang>/tokenization . The special cases are loaded before
|
|
|
|
any language data is tokenized, giving these priority. For instance,
|
|
|
|
the English tokenization rules map "ain't" to ["are", "not"].
|
|
|
|
|
|
|
|
Args:
|
|
|
|
token_rules (list): A list of (chunk, tokens) pairs, where chunk is
|
|
|
|
a string and tokens is a list of strings.
|
|
|
|
'''
|
2014-10-23 17:59:17 +04:00
|
|
|
cdef Lexeme** lexemes
|
|
|
|
cdef hash_t hashed
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr string
|
2014-09-13 01:50:37 +04:00
|
|
|
for uni_string, substrings in token_rules:
|
2014-10-30 10:01:27 +03:00
|
|
|
lexemes = <Lexeme**>self.mem.alloc(len(substrings) + 1, sizeof(Lexeme*))
|
2014-09-13 01:50:37 +04:00
|
|
|
for i, substring in enumerate(substrings):
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&string, substring, 0, len(substring))
|
2014-12-04 19:29:50 +03:00
|
|
|
lexemes[i] = <Lexeme*>self.lexicon.get(self.lexicon.mem, &string)
|
2014-09-13 01:50:37 +04:00
|
|
|
lexemes[i + 1] = NULL
|
2014-12-03 03:04:00 +03:00
|
|
|
slice_unicode(&string, uni_string, 0, len(uni_string))
|
2014-10-30 10:01:27 +03:00
|
|
|
self._specials.set(string.key, lexemes)
|
|
|
|
self._cache.set(string.key, lexemes)
|
2014-09-13 01:50:37 +04:00
|
|
|
|
2014-08-18 21:59:59 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
cdef class Lexicon:
|
2014-11-26 11:53:29 +03:00
|
|
|
'''A map container for a language's Lexeme structs.
|
|
|
|
|
|
|
|
Also interns UTF-8 strings, and maps them to consecutive integer IDs.
|
|
|
|
'''
|
2014-12-07 15:52:41 +03:00
|
|
|
def __init__(self, object get_props):
|
2014-10-14 08:47:06 +04:00
|
|
|
self.mem = Pool()
|
2014-12-02 15:46:59 +03:00
|
|
|
self._map = PreshMap(2 ** 20)
|
2014-10-23 17:59:17 +04:00
|
|
|
self.strings = StringStore()
|
2014-10-31 09:43:25 +03:00
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
2014-12-04 19:29:50 +03:00
|
|
|
self.size = 2
|
2014-12-07 15:52:41 +03:00
|
|
|
self.get_lex_props = get_props
|
2014-10-29 17:01:00 +03:00
|
|
|
|
2014-12-04 19:29:50 +03:00
|
|
|
cdef const Lexeme* get(self, Pool mem, UniStr* string) except NULL:
|
2014-12-05 07:56:14 +03:00
|
|
|
'''Get a pointer to a Lexeme from the lexicon, creating a new Lexeme
|
|
|
|
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.'''
|
2014-10-23 17:59:17 +04:00
|
|
|
cdef Lexeme* lex
|
2014-12-02 15:46:59 +03:00
|
|
|
lex = <Lexeme*>self._map.get(string.key)
|
2014-10-10 01:11:31 +04:00
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
2014-12-04 19:29:50 +03:00
|
|
|
if string.n < 3:
|
|
|
|
mem = self.mem
|
2014-12-07 15:52:41 +03:00
|
|
|
cdef unicode py_string = string.chars[:string.n]
|
2014-12-04 19:29:50 +03:00
|
|
|
lex = <Lexeme*>mem.alloc(sizeof(Lexeme), 1)
|
2014-12-07 15:52:41 +03:00
|
|
|
lex[0] = lexeme_init(self.size, py_string, string.key, self.strings,
|
|
|
|
self.get_lex_props(py_string))
|
2014-12-04 19:29:50 +03:00
|
|
|
if mem is self.mem:
|
|
|
|
self._map.set(string.key, lex)
|
|
|
|
while self.lexemes.size() < (lex.id + 1):
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
|
|
|
self.lexemes[lex.id] = lex
|
|
|
|
self.size += 1
|
|
|
|
else:
|
|
|
|
lex[0].id = 1
|
2014-10-10 01:11:31 +04:00
|
|
|
return lex
|
2014-09-11 23:54:34 +04:00
|
|
|
|
2014-10-31 09:43:25 +03:00
|
|
|
def __getitem__(self, id_or_string):
|
2014-11-26 11:53:29 +03:00
|
|
|
'''Retrieve a lexeme, given an int ID or a unicode string. If a previously
|
|
|
|
unseen unicode string is given, a new Lexeme is created and stored.
|
|
|
|
|
|
|
|
This function relies on Cython's struct-to-dict conversion. Python clients
|
|
|
|
receive a dict keyed by strings (byte or unicode, depending on Python 2/3),
|
|
|
|
with int values. Cython clients can instead receive a Lexeme struct value.
|
|
|
|
More efficient Cython access is provided by Lexicon.get, which returns
|
|
|
|
a Lexeme*.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
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.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
lexeme (dict): A Lexeme struct instance, which Cython translates into
|
|
|
|
a dict if the operator is called from Python.
|
|
|
|
'''
|
2014-10-31 09:43:25 +03:00
|
|
|
if type(id_or_string) == int:
|
|
|
|
return self.lexemes.at(id_or_string)[0]
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr string
|
|
|
|
slice_unicode(&string, id_or_string, 0, len(id_or_string))
|
2014-12-04 19:29:50 +03:00
|
|
|
cdef const Lexeme* lexeme = self.get(self.mem, &string)
|
2014-10-23 17:59:17 +04:00
|
|
|
return lexeme[0]
|
2014-09-13 01:50:37 +04:00
|
|
|
|
2014-10-30 10:14:42 +03:00
|
|
|
def __setitem__(self, unicode uni_string, dict props):
|
2014-12-03 03:04:00 +03:00
|
|
|
cdef UniStr s
|
|
|
|
slice_unicode(&s, uni_string, 0, len(uni_string))
|
2014-12-03 07:58:17 +03:00
|
|
|
# Cast through the const here, since we're allowed to change our own
|
|
|
|
# Lexemes.
|
2014-12-04 19:29:50 +03:00
|
|
|
lex = <Lexeme*><void*>self.get(self.mem, &s)
|
2014-12-03 07:58:17 +03:00
|
|
|
lex[0] = lexeme_init(lex.id, s.chars[:s.n], s.key, self.strings, props)
|
2014-10-30 10:14:42 +03:00
|
|
|
|
2014-10-29 15:19:38 +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
|
2014-10-30 07:21:38 +03:00
|
|
|
cdef hash_t key
|
2014-12-02 15:46:59 +03:00
|
|
|
for i in range(self._map.length):
|
|
|
|
key = self._map.c_map.cells[i].key
|
2014-10-30 07:21:38 +03:00
|
|
|
if key == 0:
|
|
|
|
continue
|
2014-12-02 15:46:59 +03:00
|
|
|
lexeme = <Lexeme*>self._map.c_map.cells[i].value
|
2014-10-30 07:21:38 +03:00
|
|
|
st = fwrite(&key, sizeof(key), 1, fp)
|
|
|
|
assert st == 1
|
|
|
|
st = fwrite(lexeme, sizeof(Lexeme), 1, fp)
|
2014-10-29 15:19:38 +03:00
|
|
|
assert st == 1
|
|
|
|
st = fclose(fp)
|
|
|
|
assert st == 0
|
|
|
|
|
|
|
|
def load(self, loc):
|
2014-12-03 07:44:25 +03:00
|
|
|
if not path.exists(loc):
|
|
|
|
raise IOError('Lexemes file not found at %s' % loc)
|
2014-10-29 15:19:38 +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
|
|
|
|
cdef Lexeme* lexeme
|
2014-10-30 07:21:38 +03:00
|
|
|
cdef hash_t key
|
2014-10-30 05:38:55 +03:00
|
|
|
i = 0
|
2014-10-29 15:19:38 +03:00
|
|
|
while True:
|
2014-10-30 07:21:38 +03:00
|
|
|
st = fread(&key, sizeof(key), 1, fp)
|
|
|
|
if st != 1:
|
|
|
|
break
|
2014-10-29 15:19:38 +03:00
|
|
|
lexeme = <Lexeme*>self.mem.alloc(sizeof(Lexeme), 1)
|
2014-10-29 17:01:00 +03:00
|
|
|
st = fread(lexeme, sizeof(Lexeme), 1, fp)
|
|
|
|
if st != 1:
|
2014-10-29 15:19:38 +03:00
|
|
|
break
|
2014-12-02 15:46:59 +03:00
|
|
|
self._map.set(key, lexeme)
|
2014-10-31 09:43:25 +03:00
|
|
|
while self.lexemes.size() < (lexeme.id + 1):
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
|
|
|
self.lexemes[lexeme.id] = lexeme
|
2014-10-30 05:38:55 +03:00
|
|
|
i += 1
|
2014-10-31 09:43:25 +03:00
|
|
|
self.size += 1
|
2014-10-29 17:01:00 +03:00
|
|
|
fclose(fp)
|