2014-07-07 05:36:43 +00:00
|
|
|
# cython: profile=True
|
2014-08-20 11:39:39 +00:00
|
|
|
# cython: embedsignature=True
|
|
|
|
"""Common classes and utilities across languages.
|
|
|
|
|
|
|
|
Provides the main implementation for the spacy tokenizer. Specific languages
|
|
|
|
subclass the Language class, over-writing the tokenization rules as necessary.
|
|
|
|
Special-case tokenization rules are read from data/<lang>/tokenization .
|
|
|
|
"""
|
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-08-28 17:45:09 +00:00
|
|
|
from .util import read_lang_data
|
2014-09-10 16:11:13 +00:00
|
|
|
from spacy.tokens import Tokens
|
2014-10-09 21:11:31 +00:00
|
|
|
from spacy.lexeme cimport LexemeC, get_lexeme_dict, lexeme_pack, lexeme_unpack
|
2014-10-10 08:17:22 +00:00
|
|
|
from spacy.lexeme cimport LexStr_orig
|
2014-09-12 00:04:47 +00:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-09-17 18:02:26 +00:00
|
|
|
|
2014-09-15 01:22:40 +00:00
|
|
|
from cpython.ref cimport Py_INCREF
|
|
|
|
|
2014-09-17 21:09:24 +00:00
|
|
|
from cymem.cymem cimport Pool
|
2014-09-17 18:02:26 +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-09-26 16:40:03 +00:00
|
|
|
from preshed.maps cimport PreshMap
|
2014-09-14 23:31:44 +00:00
|
|
|
from spacy import orth
|
|
|
|
from spacy import util
|
|
|
|
|
|
|
|
|
2014-07-07 10:47:21 +00:00
|
|
|
cdef class Language:
|
2014-08-28 17:45:09 +00:00
|
|
|
"""Base class for language-specific tokenizers.
|
|
|
|
|
|
|
|
The language's name is used to look up default data-files, found in data/<name.
|
|
|
|
"""
|
2014-10-09 03:51:35 +00:00
|
|
|
def __init__(self, name, user_string_features, user_flag_features):
|
2014-07-07 10:47:21 +00:00
|
|
|
self.name = name
|
2014-09-17 18:02:26 +00:00
|
|
|
self._mem = Pool()
|
2014-09-26 16:40:03 +00:00
|
|
|
self.cache = PreshMap(2 ** 25)
|
|
|
|
self.specials = PreshMap(2 ** 16)
|
2014-10-09 03:51:35 +00:00
|
|
|
rules, prefix, suffix, lexemes = util.read_lang_data(name)
|
2014-09-25 16:22:52 +00:00
|
|
|
self.prefix_re = re.compile(prefix)
|
|
|
|
self.suffix_re = re.compile(suffix)
|
2014-10-09 21:11:31 +00:00
|
|
|
self.lexicon = Lexicon(lexemes)
|
2014-08-28 23:59:23 +00:00
|
|
|
self._load_special_tokenization(rules)
|
2014-10-10 08:17:22 +00:00
|
|
|
|
2014-09-10 16:11:13 +00:00
|
|
|
cpdef Lexeme lookup(self, unicode string):
|
|
|
|
"""Retrieve (or create, if not found) a Lexeme for a string, and return it.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
string (unicode): The string to be looked up. Must be unicode, not bytes.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
lexeme (Lexeme): A reference to a lexical type.
|
|
|
|
"""
|
|
|
|
return self.lexicon.lookup(string)
|
2014-08-20 11:39:39 +00:00
|
|
|
|
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-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-08-18 18:23:54 +00:00
|
|
|
cdef size_t length = len(string)
|
2014-09-14 23:31:44 +00:00
|
|
|
cdef Tokens tokens = Tokens(length)
|
2014-09-11 10:28:38 +00:00
|
|
|
if length == 0:
|
2014-09-11 19:37:32 +00:00
|
|
|
return tokens
|
2014-09-11 10:28:38 +00:00
|
|
|
|
2014-08-18 17:59:59 +00:00
|
|
|
cdef size_t start = 0
|
2014-08-18 18:48:48 +00:00
|
|
|
cdef size_t i = 0
|
2014-09-12 21:50:37 +00:00
|
|
|
cdef Py_UNICODE* chars = string
|
|
|
|
cdef String span
|
2014-09-12 00:04:47 +00:00
|
|
|
for i in range(length):
|
2014-10-10 08:17:22 +00:00
|
|
|
if Py_UNICODE_ISSPACE(chars[i]) == 1:
|
2014-08-18 17:59:59 +00:00
|
|
|
if start < i:
|
2014-09-12 21:50:37 +00:00
|
|
|
string_from_slice(&span, chars, start, i)
|
2014-10-10 08:50:07 +00:00
|
|
|
if not _extend_from_map(tokens.v, &span, self.cache):
|
|
|
|
self._tokenize(tokens.v, &span)
|
2014-08-18 17:59:59 +00:00
|
|
|
start = i + 1
|
2014-09-12 00:04:47 +00:00
|
|
|
i += 1
|
2014-08-18 17:59:59 +00:00
|
|
|
if start < i:
|
2014-09-12 21:50:37 +00:00
|
|
|
string_from_slice(&span, chars, start, i)
|
2014-10-10 08:50:07 +00:00
|
|
|
if not _extend_from_map(tokens.v, &span, self.cache):
|
|
|
|
self._tokenize(tokens.v, &span)
|
2014-09-11 19:37:32 +00:00
|
|
|
return tokens
|
2014-07-07 10:47:21 +00:00
|
|
|
|
2014-09-15 04:33:53 +00:00
|
|
|
cdef int _tokenize(self, vector[LexemeC*] *tokens_v, String* string) except -1:
|
2014-09-16 16:01:46 +00:00
|
|
|
cdef size_t i
|
2014-09-16 11:16:02 +00:00
|
|
|
cdef uint64_t orig_key = string.key
|
|
|
|
cdef size_t orig_size = tokens_v.size()
|
|
|
|
|
|
|
|
cdef vector[LexemeC*] prefixes
|
|
|
|
cdef vector[LexemeC*] suffixes
|
|
|
|
|
2014-09-16 16:01:46 +00:00
|
|
|
cdef String prefix
|
|
|
|
cdef String suffix
|
|
|
|
cdef String minus_pre
|
|
|
|
cdef String minus_suf
|
|
|
|
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:
|
|
|
|
string_from_slice(&prefix, string.chars, 0, pre_len)
|
|
|
|
string_from_slice(&minus_pre, string.chars, pre_len, string.n)
|
|
|
|
# Check whether we've hit a special-case
|
|
|
|
if minus_pre.n >= 1 and self.specials.get(minus_pre.key) != NULL:
|
|
|
|
string = &minus_pre
|
|
|
|
prefixes.push_back(self.lexicon.get(&prefix))
|
|
|
|
break
|
|
|
|
suf_len = self._find_suffix(string.chars, string.n)
|
|
|
|
if suf_len != 0:
|
|
|
|
string_from_slice(&suffix, string.chars, string.n - suf_len, string.n)
|
|
|
|
string_from_slice(&minus_suf, string.chars, 0, string.n - suf_len)
|
|
|
|
# Check whether we've hit a special-case
|
|
|
|
if minus_suf.n >= 1 and self.specials.get(minus_suf.key) != NULL:
|
|
|
|
string = &minus_suf
|
|
|
|
suffixes.push_back(self.lexicon.get(&suffix))
|
|
|
|
break
|
|
|
|
|
|
|
|
if pre_len and suf_len and (pre_len + suf_len) <= string.n:
|
|
|
|
string_from_slice(string, string.chars, pre_len, string.n - suf_len)
|
|
|
|
prefixes.push_back(self.lexicon.get(&prefix))
|
|
|
|
suffixes.push_back(self.lexicon.get(&suffix))
|
|
|
|
elif pre_len:
|
|
|
|
string = &minus_pre
|
|
|
|
prefixes.push_back(self.lexicon.get(&prefix))
|
|
|
|
elif suf_len:
|
|
|
|
string = &minus_suf
|
|
|
|
suffixes.push_back(self.lexicon.get(&suffix))
|
|
|
|
|
|
|
|
if self.specials.get(string.key):
|
|
|
|
break
|
|
|
|
|
2014-09-16 11:16:02 +00:00
|
|
|
self._attach_tokens(tokens_v, string, &prefixes, &suffixes)
|
|
|
|
self._save_cached(tokens_v, orig_key, orig_size)
|
|
|
|
|
|
|
|
cdef int _attach_tokens(self, vector[LexemeC*] *tokens, String* string,
|
|
|
|
vector[LexemeC*] *prefixes,
|
|
|
|
vector[LexemeC*] *suffixes) except -1:
|
2014-09-16 16:01:46 +00:00
|
|
|
cdef size_t i
|
|
|
|
cdef LexemeC** lexemes
|
2014-09-15 04:33:53 +00:00
|
|
|
cdef LexemeC* lexeme
|
2014-09-16 16:01:46 +00:00
|
|
|
for lexeme in deref(prefixes):
|
2014-09-16 11:16:02 +00:00
|
|
|
tokens.push_back(lexeme)
|
2014-10-10 09:23:23 +00:00
|
|
|
if not _extend_from_map(tokens, string, self.specials):
|
|
|
|
self._split_body_token(tokens, string)
|
2014-09-16 11:16:02 +00:00
|
|
|
cdef vector[LexemeC*].reverse_iterator it = suffixes.rbegin()
|
|
|
|
while it != suffixes.rend():
|
|
|
|
tokens.push_back(deref(it))
|
|
|
|
preinc(it)
|
|
|
|
|
2014-10-10 09:23:23 +00:00
|
|
|
cdef int _save_cached(self, vector[LexemeC*] *tokens, uint64_t key, size_t n) except -1:
|
2014-09-16 16:01:46 +00:00
|
|
|
assert tokens.size() > n
|
2014-09-17 18:02:26 +00:00
|
|
|
lexemes = <LexemeC**>self._mem.alloc((tokens.size() - n) + 1, sizeof(LexemeC**))
|
2014-09-16 16:01:46 +00:00
|
|
|
cdef size_t i, j
|
|
|
|
for i, j in enumerate(range(n, tokens.size())):
|
|
|
|
lexemes[i] = tokens.at(j)
|
|
|
|
lexemes[i + 1] = NULL
|
|
|
|
self.cache.set(key, lexemes)
|
2014-10-10 09:23:23 +00:00
|
|
|
|
|
|
|
cdef int _split_body_token(self, vector[LexemeC*] *tokens, String* string) except -1:
|
|
|
|
tokens.push_back(self.lexicon.get(string))
|
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]
|
|
|
|
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
|
|
|
|
|
|
|
cdef int _find_suffix(self, Py_UNICODE* chars, size_t length):
|
|
|
|
cdef unicode string = chars[:length]
|
|
|
|
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-08-28 23:59:23 +00:00
|
|
|
def _load_special_tokenization(self, token_rules):
|
2014-08-20 11:39:39 +00:00
|
|
|
'''Load special-case tokenization rules.
|
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
Loads special-case tokenization rules into the Language.cache cache,
|
2014-08-20 11:39:39 +00: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-09-12 21:50:37 +00:00
|
|
|
cdef LexemeC** lexemes
|
2014-09-12 00:04:47 +00:00
|
|
|
cdef uint64_t hashed
|
2014-09-12 21:50:37 +00:00
|
|
|
cdef String string
|
|
|
|
for uni_string, substrings in token_rules:
|
2014-09-17 18:02:26 +00:00
|
|
|
lexemes = <LexemeC**>self._mem.alloc(len(substrings) + 1, sizeof(LexemeC*))
|
2014-09-12 21:50:37 +00:00
|
|
|
for i, substring in enumerate(substrings):
|
|
|
|
string_from_unicode(&string, substring)
|
|
|
|
lexemes[i] = <LexemeC*>self.lexicon.get(&string)
|
|
|
|
lexemes[i + 1] = NULL
|
|
|
|
string_from_unicode(&string, uni_string)
|
2014-09-13 15:02:06 +00:00
|
|
|
self.specials.set(string.key, lexemes)
|
|
|
|
self.cache.set(string.key, lexemes)
|
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-10-09 21:11:31 +00:00
|
|
|
def __cinit__(self, lexemes):
|
2014-09-17 18:02:26 +00:00
|
|
|
self._mem = Pool()
|
2014-09-26 16:40:03 +00:00
|
|
|
self._dict = PreshMap(2 ** 20)
|
2014-09-10 16:11:13 +00:00
|
|
|
self.size = 0
|
2014-09-13 15:02:06 +00:00
|
|
|
cdef String string
|
2014-10-09 03:51:35 +00:00
|
|
|
cdef dict lexeme_dict
|
|
|
|
cdef LexemeC* lexeme
|
|
|
|
for lexeme_dict in lexemes:
|
|
|
|
string_from_unicode(&string, lexeme_dict['string'])
|
|
|
|
lexeme = <LexemeC*>self._mem.alloc(1, sizeof(LexemeC))
|
|
|
|
lexeme_unpack(lexeme, lexeme_dict)
|
2014-09-13 15:02:06 +00:00
|
|
|
self._dict.set(string.key, lexeme)
|
2014-10-10 08:17:22 +00:00
|
|
|
self.lexemes.push_back(lexeme)
|
2014-09-10 16:11:13 +00:00
|
|
|
self.size += 1
|
2014-08-20 11:39:39 +00:00
|
|
|
|
2014-09-16 16:01:46 +00:00
|
|
|
cdef LexemeC* get(self, String* string) except NULL:
|
2014-10-09 21:11:31 +00:00
|
|
|
cdef LexemeC* lex
|
|
|
|
lex = <LexemeC*>self._dict.get(string.key)
|
|
|
|
if lex != NULL:
|
|
|
|
return lex
|
|
|
|
|
|
|
|
lex = <LexemeC*>self._mem.alloc(1, sizeof(LexemeC))
|
|
|
|
cdef unicode unicode_string = string.chars[:string.n]
|
|
|
|
lexeme_unpack(lex, get_lexeme_dict(self.size, unicode_string))
|
|
|
|
self._dict.set(string.key, lex)
|
2014-10-10 08:17:22 +00:00
|
|
|
self.lexemes.push_back(lex)
|
2014-09-10 16:11:13 +00:00
|
|
|
self.size += 1
|
2014-10-09 21:11:31 +00:00
|
|
|
return lex
|
2014-09-11 19:54:34 +00:00
|
|
|
|
2014-09-12 21:50:37 +00:00
|
|
|
cpdef Lexeme lookup(self, unicode uni_string):
|
2014-09-11 19:54:34 +00:00
|
|
|
"""Retrieve (or create, if not found) a Lexeme for a string, and return it.
|
|
|
|
|
|
|
|
Args
|
|
|
|
string (unicode): The string to be looked up. Must be unicode, not bytes.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
lexeme (Lexeme): A reference to a lexical type.
|
|
|
|
"""
|
2014-09-12 21:50:37 +00:00
|
|
|
cdef String string
|
|
|
|
string_from_unicode(&string, uni_string)
|
2014-09-13 15:29:58 +00:00
|
|
|
cdef LexemeC* lexeme = self.get(&string)
|
|
|
|
return Lexeme(<size_t>lexeme)
|
2014-09-12 21:50:37 +00:00
|
|
|
|
|
|
|
|
2014-10-10 08:50:07 +00:00
|
|
|
cdef int _extend_from_map(vector[LexemeC*] *tokens, String* string, PreshMap map_) except -1:
|
2014-10-10 09:23:23 +00:00
|
|
|
if string.n == 0:
|
|
|
|
return 1
|
2014-10-10 08:50:07 +00:00
|
|
|
lexemes = <LexemeC**>map_.get(string.key)
|
|
|
|
if lexemes == NULL:
|
|
|
|
return 0
|
|
|
|
cdef size_t i = 0
|
|
|
|
while lexemes[i] != NULL:
|
|
|
|
tokens.push_back(lexemes[i])
|
|
|
|
i += 1
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2014-09-12 22:14:05 +00:00
|
|
|
cdef void string_from_unicode(String* s, unicode uni):
|
2014-09-14 23:31:44 +00:00
|
|
|
cdef Py_UNICODE* c_uni = <Py_UNICODE*>uni
|
|
|
|
string_from_slice(s, c_uni, 0, len(uni))
|
2014-09-12 21:50:37 +00:00
|
|
|
|
|
|
|
|
2014-09-12 22:31:53 +00:00
|
|
|
cdef inline void string_from_slice(String* s, Py_UNICODE* chars, size_t start, size_t end) nogil:
|
2014-09-12 21:50:37 +00:00
|
|
|
s.chars = &chars[start]
|
|
|
|
s.n = end - start
|
|
|
|
s.key = hash64(s.chars, s.n * sizeof(Py_UNICODE), 0)
|
|
|
|
|
|
|
|
|
2014-09-12 22:31:53 +00:00
|
|
|
cdef inline void string_slice_prefix(String* s, String* prefix, size_t n) nogil:
|
2014-09-12 21:50:37 +00:00
|
|
|
string_from_slice(prefix, s.chars, 0, n)
|
|
|
|
s.chars += n
|
|
|
|
s.n -= n
|
|
|
|
s.key = hash64(s.chars, s.n * sizeof(Py_UNICODE), 0)
|
2014-09-16 11:16:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
cdef inline void string_slice_suffix(String* s, String* suffix, size_t n) nogil:
|
|
|
|
string_from_slice(suffix, s.chars, s.n - n, s.n)
|
|
|
|
s.n -= n
|
|
|
|
s.key = hash64(s.chars, s.n * sizeof(Py_UNICODE), 0)
|