2014-07-07 05:36:43 +00:00
|
|
|
# cython: profile=True
|
2014-08-20 11:39:39 +00:00
|
|
|
# cython: embedsignature=True
|
2014-07-05 18:51:42 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-07-07 02:21:06 +00:00
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
import json
|
2014-09-11 22:18:31 +00:00
|
|
|
import random
|
2014-07-07 05:36:43 +00:00
|
|
|
from os import path
|
2014-09-25 16:22:52 +00:00
|
|
|
import re
|
2014-08-16 17:59:38 +00:00
|
|
|
|
2014-09-16 11:16:02 +00:00
|
|
|
from cython.operator cimport preincrement as preinc
|
|
|
|
from cython.operator cimport dereference as deref
|
2014-10-29 12:19:38 +00:00
|
|
|
from libc.stdio cimport fopen, fclose, fread, fwrite, FILE
|
2014-09-16 11:16:02 +00:00
|
|
|
|
2014-10-29 12:19:38 +00:00
|
|
|
from cymem.cymem cimport Pool
|
2014-10-23 13:59:17 +00:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-09-26 16:40:03 +00:00
|
|
|
from preshed.maps cimport PreshMap
|
2014-10-23 13:59:17 +00:00
|
|
|
|
|
|
|
from .lexeme cimport Lexeme
|
2014-10-31 06:43:25 +00:00
|
|
|
from .lexeme cimport EMPTY_LEXEME
|
2014-10-29 12:19:38 +00:00
|
|
|
from .lexeme cimport init as lexeme_init
|
2014-12-05 04:56:14 +00:00
|
|
|
from .lexeme cimport check_flag
|
2014-10-23 13:59:17 +00:00
|
|
|
|
2014-12-03 00:04:00 +00:00
|
|
|
from .utf8string cimport slice_unicode
|
|
|
|
|
2014-10-23 13:59:17 +00:00
|
|
|
from . import util
|
|
|
|
from .util import read_lang_data
|
|
|
|
from .tokens import Tokens
|
2014-12-09 03:48:01 +00:00
|
|
|
from .tokens cimport Morphology
|
2014-12-08 10:12:15 +00:00
|
|
|
|
2014-09-14 23:31:44 +00:00
|
|
|
|
2014-07-07 10:47:21 +00:00
|
|
|
cdef class Language:
|
2014-10-30 07:01:27 +00:00
|
|
|
def __init__(self, name):
|
2014-07-07 10:47:21 +00:00
|
|
|
self.name = name
|
2014-10-30 07:01:27 +00:00
|
|
|
self.mem = Pool()
|
|
|
|
self._cache = PreshMap(2 ** 25)
|
|
|
|
self._specials = PreshMap(2 ** 16)
|
2014-12-12 03:33:51 +00:00
|
|
|
self._pos_cache = PreshMap(2 ** 16)
|
2014-10-30 07:14:42 +00:00
|
|
|
rules, prefix, suffix, infix = util.read_lang_data(name)
|
2014-10-30 07:01:27 +00:00
|
|
|
self._prefix_re = re.compile(prefix)
|
|
|
|
self._suffix_re = re.compile(suffix)
|
|
|
|
self._infix_re = re.compile(infix)
|
2014-12-07 12:52:41 +00:00
|
|
|
self.lexicon = Lexicon(self.get_props)
|
2014-08-28 23:59:23 +00:00
|
|
|
self._load_special_tokenization(rules)
|
2014-12-07 04:29:41 +00:00
|
|
|
self.pos_tagger = None
|
2014-12-09 10:16:17 +00:00
|
|
|
self.morphologizer = None
|
2014-10-10 08:17:22 +00:00
|
|
|
|
2014-12-17 22:05:31 +00:00
|
|
|
def load(self, pos_dir=None, parser_dir=None):
|
2014-12-03 04:44:25 +00:00
|
|
|
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-16 11:44:43 +00:00
|
|
|
if pos_dir is None:
|
|
|
|
pos_dir = path.join(util.DATA_DIR, self.name, 'pos')
|
2014-12-17 22:05:31 +00:00
|
|
|
if parser_dir is None:
|
|
|
|
parser_dir = path.join(util.DATA_DIR, self.name, 'deps')
|
2014-12-16 11:44:43 +00:00
|
|
|
if path.exists(pos_dir):
|
|
|
|
self.pos_tagger = Tagger(pos_dir)
|
|
|
|
self.morphologizer = Morphologizer(self.lexicon.strings, pos_dir)
|
|
|
|
#self.load_pos_cache(path.join(util.DATA_DIR, self.name, 'pos', 'bigram_cache_2m'))
|
2014-12-17 22:05:31 +00:00
|
|
|
if path.exists(parser_dir):
|
|
|
|
self.parser = GreedyParser(parser_dir)
|
2014-12-08 10:12:15 +00:00
|
|
|
|
2014-11-11 12:43:14 +00:00
|
|
|
cpdef Tokens tokens_from_list(self, list strings):
|
|
|
|
cdef int length = sum([len(s) for s in strings])
|
2014-12-09 21:09:32 +00:00
|
|
|
cdef Tokens tokens = Tokens(self, length)
|
2014-11-11 12:43:14 +00:00
|
|
|
if length == 0:
|
|
|
|
return tokens
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr string_struct
|
2014-11-11 12:43:14 +00:00
|
|
|
cdef unicode py_string
|
|
|
|
cdef int idx = 0
|
|
|
|
for i, py_string in enumerate(strings):
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&string_struct, py_string, 0, len(py_string))
|
2014-12-09 06:09:23 +00:00
|
|
|
tokens.push_back(idx, <const Lexeme*>self.lexicon.get(tokens.mem, &string_struct))
|
2014-11-11 12:43:14 +00:00
|
|
|
idx += len(py_string) + 1
|
|
|
|
return tokens
|
|
|
|
|
2014-09-11 19:37:32 +00:00
|
|
|
cpdef Tokens tokenize(self, unicode string):
|
2014-08-27 15:15:39 +00:00
|
|
|
"""Tokenize a string.
|
|
|
|
|
2014-10-09 21:11:31 +00:00
|
|
|
The tokenization rules are defined in three places:
|
2014-08-20 11:39:39 +00:00
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
* The data/<lang>/tokenization table, which handles special cases like contractions;
|
2014-10-09 21:11:31 +00: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 10:47:21 +00:00
|
|
|
|
2014-12-19 14:42:09 +00:00
|
|
|
The string is first split on whitespace. To tokenize a whitespace-delimited
|
|
|
|
chunk, we first try to look it up in the special-cases. If it's not found,
|
|
|
|
we split off a prefix, and then try again. If it's still not found, we
|
|
|
|
split off a suffix, and repeat.
|
|
|
|
|
2014-08-20 11:39:39 +00:00
|
|
|
Args:
|
2014-08-27 15:15:39 +00:00
|
|
|
string (unicode): The string to be tokenized.
|
2014-08-20 11:39:39 +00:00
|
|
|
|
|
|
|
Returns:
|
2014-10-09 21:11:31 +00:00
|
|
|
tokens (Tokens): A Tokens object, giving access to a sequence of Lexemes.
|
2014-08-20 11:39:39 +00:00
|
|
|
"""
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int length = len(string)
|
2014-12-09 21:09:32 +00:00
|
|
|
cdef Tokens tokens = Tokens(self, length)
|
2014-09-11 10:28:38 +00:00
|
|
|
if length == 0:
|
2014-09-11 19:37:32 +00:00
|
|
|
return tokens
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int i = 0
|
2014-10-14 09:25:57 +00:00
|
|
|
cdef int start = 0
|
2014-12-09 03:48:01 +00:00
|
|
|
cdef bint cache_hit
|
2014-09-12 21:50:37 +00:00
|
|
|
cdef Py_UNICODE* chars = string
|
2014-10-14 09:25:57 +00:00
|
|
|
cdef bint in_ws = Py_UNICODE_ISSPACE(chars[0])
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr span
|
2014-10-14 09:25:57 +00:00
|
|
|
for i in range(1, length):
|
|
|
|
if Py_UNICODE_ISSPACE(chars[i]) != in_ws:
|
2014-08-18 17:59:59 +00:00
|
|
|
if start < i:
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&span, chars, start, i)
|
2014-12-09 03:48:01 +00:00
|
|
|
cache_hit = self._try_cache(start, span.key, tokens)
|
|
|
|
if not cache_hit:
|
2014-10-14 07:22:41 +00:00
|
|
|
self._tokenize(tokens, &span, start, i)
|
2014-10-14 09:25:57 +00:00
|
|
|
in_ws = not in_ws
|
|
|
|
start = i
|
|
|
|
if chars[i] == ' ':
|
|
|
|
start += 1
|
2014-09-12 00:04:47 +00:00
|
|
|
i += 1
|
2014-08-18 17:59:59 +00:00
|
|
|
if start < i:
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&span, chars, start, i)
|
2014-12-09 03:48:01 +00:00
|
|
|
cache_hit = self._try_cache(start, span.key, tokens)
|
|
|
|
if not cache_hit:
|
2014-10-14 07:22:41 +00:00
|
|
|
self._tokenize(tokens, &span, start, i)
|
2014-09-11 19:37:32 +00:00
|
|
|
return tokens
|
2014-07-07 10:47:21 +00:00
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
cdef int _try_cache(self, int idx, hash_t key, Tokens tokens) except -1:
|
2014-12-09 21:09:32 +00:00
|
|
|
#cached = <Cached*>self._specials.get(key)
|
|
|
|
cached = <Cached*>self._cache.get(key)
|
2014-12-09 06:09:23 +00:00
|
|
|
if cached == NULL:
|
2014-12-09 21:09:32 +00:00
|
|
|
return False
|
2014-12-09 06:09:23 +00:00
|
|
|
cdef int i
|
|
|
|
if cached.is_lex:
|
|
|
|
for i in range(cached.length):
|
|
|
|
idx = tokens.push_back(idx, cached.data.lexemes[i])
|
|
|
|
else:
|
|
|
|
for i in range(cached.length):
|
|
|
|
idx = tokens.push_back(idx, &cached.data.tokens[i])
|
|
|
|
return True
|
2014-12-09 03:48:01 +00:00
|
|
|
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef int _tokenize(self, Tokens tokens, UniStr* span, int start, int end) except -1:
|
2014-10-23 13:59:17 +00:00
|
|
|
cdef vector[Lexeme*] prefixes
|
|
|
|
cdef vector[Lexeme*] suffixes
|
|
|
|
cdef hash_t orig_key
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int orig_size
|
2014-10-14 07:22:41 +00:00
|
|
|
orig_key = span.key
|
2014-10-22 14:57:59 +00:00
|
|
|
orig_size = tokens.length
|
2014-10-14 07:22:41 +00:00
|
|
|
self._split_affixes(span, &prefixes, &suffixes)
|
|
|
|
self._attach_tokens(tokens, start, span, &prefixes, &suffixes)
|
2014-12-05 04:56:14 +00:00
|
|
|
self._save_cached(&tokens.data[orig_size], orig_key, tokens.length - orig_size)
|
2014-10-14 04:47:06 +00:00
|
|
|
|
2014-12-03 04:44:25 +00:00
|
|
|
cdef UniStr* _split_affixes(self, UniStr* string, vector[const Lexeme*] *prefixes,
|
|
|
|
vector[const Lexeme*] *suffixes) except NULL:
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef size_t i
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr prefix
|
|
|
|
cdef UniStr suffix
|
|
|
|
cdef UniStr minus_pre
|
|
|
|
cdef UniStr minus_suf
|
2014-09-16 16:01:46 +00: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 00:04:00 +00:00
|
|
|
slice_unicode(&prefix, string.chars, 0, pre_len)
|
|
|
|
slice_unicode(&minus_pre, string.chars, pre_len, string.n)
|
2014-09-16 16:01:46 +00:00
|
|
|
# Check whether we've hit a special-case
|
2014-10-30 07:01:27 +00:00
|
|
|
if minus_pre.n >= 1 and self._specials.get(minus_pre.key) != NULL:
|
2014-10-14 07:22:41 +00:00
|
|
|
string[0] = minus_pre
|
2014-12-04 16:29:50 +00:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
2014-09-16 16:01:46 +00:00
|
|
|
break
|
|
|
|
suf_len = self._find_suffix(string.chars, string.n)
|
|
|
|
if suf_len != 0:
|
2014-12-03 00:04:00 +00: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 16:01:46 +00:00
|
|
|
# Check whether we've hit a special-case
|
2014-10-30 07:01:27 +00:00
|
|
|
if minus_suf.n >= 1 and self._specials.get(minus_suf.key) != NULL:
|
2014-10-14 07:22:41 +00:00
|
|
|
string[0] = minus_suf
|
2014-12-04 16:29:50 +00:00
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-09-16 16:01:46 +00:00
|
|
|
break
|
|
|
|
if pre_len and suf_len and (pre_len + suf_len) <= string.n:
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(string, string.chars, pre_len, string.n - suf_len)
|
2014-12-04 16:29:50 +00:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-09-16 16:01:46 +00:00
|
|
|
elif pre_len:
|
2014-10-14 07:22:41 +00:00
|
|
|
string[0] = minus_pre
|
2014-12-04 16:29:50 +00:00
|
|
|
prefixes.push_back(self.lexicon.get(self.lexicon.mem, &prefix))
|
2014-09-16 16:01:46 +00:00
|
|
|
elif suf_len:
|
2014-10-14 07:22:41 +00:00
|
|
|
string[0] = minus_suf
|
2014-12-04 16:29:50 +00:00
|
|
|
suffixes.push_back(self.lexicon.get(self.lexicon.mem, &suffix))
|
2014-10-30 07:01:27 +00:00
|
|
|
if self._specials.get(string.key):
|
2014-09-16 16:01:46 +00:00
|
|
|
break
|
2014-10-14 04:47:06 +00:00
|
|
|
return string
|
2014-09-16 16:01:46 +00:00
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
cdef int _attach_tokens(self, Tokens tokens, int idx, UniStr* string,
|
2014-12-03 04:44:25 +00:00
|
|
|
vector[const Lexeme*] *prefixes,
|
|
|
|
vector[const Lexeme*] *suffixes) except -1:
|
2014-12-09 03:48:01 +00:00
|
|
|
cdef bint cache_hit
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int split
|
2014-12-03 04:44:25 +00:00
|
|
|
cdef const Lexeme* const* lexemes
|
2014-10-23 13:59:17 +00:00
|
|
|
cdef Lexeme* lexeme
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr span
|
2014-12-09 06:09:23 +00:00
|
|
|
cdef int i
|
2014-10-23 15:23:42 +00:00
|
|
|
if prefixes.size():
|
2014-12-09 06:09:23 +00:00
|
|
|
for i in range(prefixes.size()):
|
|
|
|
idx = tokens.push_back(idx, prefixes[0][i])
|
2014-10-14 04:47:06 +00:00
|
|
|
if string.n != 0:
|
2014-12-09 03:48:01 +00:00
|
|
|
cache_hit = self._try_cache(idx, string.key, tokens)
|
|
|
|
if cache_hit:
|
|
|
|
idx = tokens.data[tokens.length - 1].idx + 1
|
2014-10-14 04:47:06 +00:00
|
|
|
else:
|
|
|
|
split = self._find_infix(string.chars, string.n)
|
|
|
|
if split == 0 or split == -1:
|
2014-12-04 16:29:50 +00:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, string))
|
2014-10-14 04:47:06 +00:00
|
|
|
else:
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&span, string.chars, 0, split)
|
2014-12-04 16:29:50 +00:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&span, string.chars, split, split+1)
|
2014-12-04 16:29:50 +00:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 00:04:00 +00:00
|
|
|
slice_unicode(&span, string.chars, split + 1, string.n)
|
2014-12-04 16:29:50 +00:00
|
|
|
idx = tokens.push_back(idx, self.lexicon.get(tokens.mem, &span))
|
2014-12-03 04:44:25 +00:00
|
|
|
cdef vector[const Lexeme*].reverse_iterator it = suffixes.rbegin()
|
2014-09-16 11:16:02 +00:00
|
|
|
while it != suffixes.rend():
|
2014-10-14 04:47:06 +00:00
|
|
|
idx = tokens.push_back(idx, deref(it))
|
2014-09-16 11:16:02 +00:00
|
|
|
preinc(it)
|
|
|
|
|
2014-12-05 04:56:14 +00:00
|
|
|
cdef int _save_cached(self, const TokenC* tokens, hash_t key, int n) except -1:
|
2014-10-22 14:57:59 +00:00
|
|
|
cdef int i
|
2014-12-04 16:29:50 +00:00
|
|
|
for i in range(n):
|
2014-12-05 04:56:14 +00:00
|
|
|
if tokens[i].lex.id == 1:
|
2014-12-04 16:29:50 +00:00
|
|
|
return 0
|
2014-12-09 05:50:01 +00:00
|
|
|
cached = <Cached*>self.mem.alloc(1, sizeof(Cached))
|
|
|
|
cached.length = n
|
|
|
|
cached.is_lex = True
|
|
|
|
lexemes = <const Lexeme**>self.mem.alloc(n, sizeof(Lexeme**))
|
2014-10-22 14:57:59 +00:00
|
|
|
for i in range(n):
|
2014-12-05 04:56:14 +00:00
|
|
|
lexemes[i] = tokens[i].lex
|
2014-12-09 05:50:01 +00:00
|
|
|
cached.data.lexemes = <const Lexeme* const*>lexemes
|
|
|
|
self._cache.set(key, cached)
|
2014-10-10 09:23:23 +00:00
|
|
|
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int _find_infix(self, Py_UNICODE* chars, size_t length) except -1:
|
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 07:01:27 +00:00
|
|
|
match = self._infix_re.search(string)
|
2014-10-14 04:47:06 +00:00
|
|
|
return match.start() if match is not None else 0
|
2014-09-16 16:01:46 +00:00
|
|
|
|
|
|
|
cdef int _find_prefix(self, Py_UNICODE* chars, size_t length) except -1:
|
2014-09-25 16:22:52 +00:00
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 07:01:27 +00:00
|
|
|
match = self._prefix_re.search(string)
|
2014-10-10 09:11:22 +00:00
|
|
|
return (match.end() - match.start()) if match is not None else 0
|
2014-09-25 16:22:52 +00:00
|
|
|
|
2014-10-14 04:47:06 +00:00
|
|
|
cdef int _find_suffix(self, Py_UNICODE* chars, size_t length) except -1:
|
2014-09-25 16:22:52 +00:00
|
|
|
cdef unicode string = chars[:length]
|
2014-10-30 07:01:27 +00:00
|
|
|
match = self._suffix_re.search(string)
|
2014-10-10 09:11:22 +00:00
|
|
|
return (match.end() - match.start()) if match is not None else 0
|
2014-08-16 01:22:03 +00:00
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
def _load_special_tokenization(self, object rules):
|
|
|
|
'''Add a special-case tokenization rule.
|
2014-08-20 11:39:39 +00:00
|
|
|
'''
|
2014-12-09 03:48:01 +00:00
|
|
|
cdef int i
|
|
|
|
cdef unicode chunk
|
|
|
|
cdef list substrings
|
|
|
|
cdef unicode form
|
|
|
|
cdef unicode lemma
|
|
|
|
cdef dict props
|
2014-10-23 13:59:17 +00:00
|
|
|
cdef Lexeme** lexemes
|
|
|
|
cdef hash_t hashed
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr string
|
2014-12-09 03:48:01 +00:00
|
|
|
for chunk, substrings in sorted(rules.items()):
|
|
|
|
tokens = <TokenC*>self.mem.alloc(len(substrings) + 1, sizeof(TokenC))
|
|
|
|
for i, props in enumerate(substrings):
|
|
|
|
form = props['F']
|
|
|
|
lemma = props.get("L", None)
|
|
|
|
slice_unicode(&string, form, 0, len(form))
|
|
|
|
tokens[i].lex = <Lexeme*>self.lexicon.get(self.lexicon.mem, &string)
|
|
|
|
if lemma:
|
|
|
|
tokens[i].lemma = self.lexicon.strings[lemma]
|
|
|
|
set_morph_from_dict(&tokens[i].morph, props)
|
2014-12-09 05:50:01 +00:00
|
|
|
cached = <Cached*>self.mem.alloc(1, sizeof(Cached))
|
|
|
|
cached.length = len(substrings)
|
|
|
|
cached.is_lex = False
|
|
|
|
cached.data.tokens = tokens
|
2014-12-09 03:48:01 +00:00
|
|
|
slice_unicode(&string, chunk, 0, len(chunk))
|
2014-12-09 05:50:01 +00:00
|
|
|
self._specials.set(string.key, cached)
|
2014-12-09 21:09:32 +00:00
|
|
|
self._cache.set(string.key, cached)
|
2014-12-09 03:48:01 +00:00
|
|
|
|
2014-12-19 14:42:09 +00:00
|
|
|
cdef int is_base_np_end(self, const TokenC* token) except -1:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
cdef int is_outside_base_np(self, const TokenC* token) except -1:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
|
|
|
|
cdef int set_morph_from_dict(Morphology* morph, dict props) except -1:
|
|
|
|
morph.number = props.get('number', 0)
|
|
|
|
morph.tenspect = props.get('tenspect', 0)
|
|
|
|
morph.mood = props.get('mood', 0)
|
|
|
|
morph.gender = props.get('gender', 0)
|
|
|
|
morph.person = props.get('person', 0)
|
|
|
|
morph.case = props.get('case', 0)
|
|
|
|
morph.misc = props.get('misc', 0)
|
2014-09-12 21:50:37 +00:00
|
|
|
|
2014-08-18 17:59:59 +00:00
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
cdef class Lexicon:
|
2014-11-26 08:53:29 +00: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 12:52:41 +00:00
|
|
|
def __init__(self, object get_props):
|
2014-10-14 04:47:06 +00:00
|
|
|
self.mem = Pool()
|
2014-12-02 12:46:59 +00:00
|
|
|
self._map = PreshMap(2 ** 20)
|
2014-10-23 13:59:17 +00:00
|
|
|
self.strings = StringStore()
|
2014-10-31 06:43:25 +00:00
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
2014-12-07 12:52:41 +00:00
|
|
|
self.get_lex_props = get_props
|
2014-10-29 14:01:00 +00:00
|
|
|
|
2014-12-08 10:12:15 +00:00
|
|
|
def __len__(self):
|
|
|
|
return self.lexemes.size()
|
|
|
|
|
2014-12-04 16:29:50 +00:00
|
|
|
cdef const Lexeme* get(self, Pool mem, UniStr* string) except NULL:
|
2014-12-05 04:56:14 +00: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 13:59:17 +00:00
|
|
|
cdef Lexeme* lex
|
2014-12-02 12:46:59 +00:00
|
|
|
lex = <Lexeme*>self._map.get(string.key)
|
2014-10-09 21:11:31 +00:00
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
2014-12-04 16:29:50 +00:00
|
|
|
if string.n < 3:
|
|
|
|
mem = self.mem
|
2014-12-07 12:52:41 +00:00
|
|
|
cdef unicode py_string = string.chars[:string.n]
|
2014-12-04 16:29:50 +00:00
|
|
|
lex = <Lexeme*>mem.alloc(sizeof(Lexeme), 1)
|
2014-12-08 10:12:15 +00:00
|
|
|
lex[0] = lexeme_init(self.lexemes.size(), py_string, string.key, self.strings,
|
2014-12-07 12:52:41 +00:00
|
|
|
self.get_lex_props(py_string))
|
2014-12-04 16:29:50 +00: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
|
|
|
|
else:
|
|
|
|
lex[0].id = 1
|
2014-10-09 21:11:31 +00:00
|
|
|
return lex
|
2014-09-11 19:54:34 +00:00
|
|
|
|
2014-10-31 06:43:25 +00:00
|
|
|
def __getitem__(self, id_or_string):
|
2014-11-26 08:53:29 +00: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 06:43:25 +00:00
|
|
|
if type(id_or_string) == int:
|
2014-12-08 10:12:15 +00:00
|
|
|
if id_or_string >= self.lexemes.size():
|
|
|
|
raise IndexError
|
2014-10-31 06:43:25 +00:00
|
|
|
return self.lexemes.at(id_or_string)[0]
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr string
|
|
|
|
slice_unicode(&string, id_or_string, 0, len(id_or_string))
|
2014-12-04 16:29:50 +00:00
|
|
|
cdef const Lexeme* lexeme = self.get(self.mem, &string)
|
2014-10-23 13:59:17 +00:00
|
|
|
return lexeme[0]
|
2014-09-12 21:50:37 +00:00
|
|
|
|
2014-10-30 07:14:42 +00:00
|
|
|
def __setitem__(self, unicode uni_string, dict props):
|
2014-12-03 00:04:00 +00:00
|
|
|
cdef UniStr s
|
|
|
|
slice_unicode(&s, uni_string, 0, len(uni_string))
|
2014-12-03 04:58:17 +00:00
|
|
|
# Cast through the const here, since we're allowed to change our own
|
|
|
|
# Lexemes.
|
2014-12-04 16:29:50 +00:00
|
|
|
lex = <Lexeme*><void*>self.get(self.mem, &s)
|
2014-12-03 04:58:17 +00:00
|
|
|
lex[0] = lexeme_init(lex.id, s.chars[:s.n], s.key, self.strings, props)
|
2014-10-30 07:14:42 +00:00
|
|
|
|
2014-10-29 12:19:38 +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
|
2014-10-30 04:21:38 +00:00
|
|
|
cdef hash_t key
|
2014-12-02 12:46:59 +00:00
|
|
|
for i in range(self._map.length):
|
|
|
|
key = self._map.c_map.cells[i].key
|
2014-10-30 04:21:38 +00:00
|
|
|
if key == 0:
|
|
|
|
continue
|
2014-12-02 12:46:59 +00:00
|
|
|
lexeme = <Lexeme*>self._map.c_map.cells[i].value
|
2014-10-30 04:21:38 +00:00
|
|
|
st = fwrite(&key, sizeof(key), 1, fp)
|
|
|
|
assert st == 1
|
|
|
|
st = fwrite(lexeme, sizeof(Lexeme), 1, fp)
|
2014-10-29 12:19:38 +00:00
|
|
|
assert st == 1
|
|
|
|
st = fclose(fp)
|
|
|
|
assert st == 0
|
|
|
|
|
|
|
|
def load(self, loc):
|
2014-12-03 04:44:25 +00:00
|
|
|
if not path.exists(loc):
|
|
|
|
raise IOError('Lexemes file not found at %s' % loc)
|
2014-10-29 12:19:38 +00: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 04:21:38 +00:00
|
|
|
cdef hash_t key
|
2014-10-30 02:38:55 +00:00
|
|
|
i = 0
|
2014-10-29 12:19:38 +00:00
|
|
|
while True:
|
2014-10-30 04:21:38 +00:00
|
|
|
st = fread(&key, sizeof(key), 1, fp)
|
|
|
|
if st != 1:
|
|
|
|
break
|
2014-10-29 12:19:38 +00:00
|
|
|
lexeme = <Lexeme*>self.mem.alloc(sizeof(Lexeme), 1)
|
2014-10-29 14:01:00 +00:00
|
|
|
st = fread(lexeme, sizeof(Lexeme), 1, fp)
|
|
|
|
if st != 1:
|
2014-10-29 12:19:38 +00:00
|
|
|
break
|
2014-12-02 12:46:59 +00:00
|
|
|
self._map.set(key, lexeme)
|
2014-10-31 06:43:25 +00:00
|
|
|
while self.lexemes.size() < (lexeme.id + 1):
|
|
|
|
self.lexemes.push_back(&EMPTY_LEXEME)
|
|
|
|
self.lexemes[lexeme.id] = lexeme
|
2014-10-30 02:38:55 +00:00
|
|
|
i += 1
|
2014-10-29 14:01:00 +00:00
|
|
|
fclose(fp)
|