2014-07-07 09:36:43 +04:00
|
|
|
# cython: profile=True
|
2014-08-20 15:39:39 +04: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 22:51:42 +04:00
|
|
|
from __future__ import unicode_literals
|
2014-07-07 06:21:06 +04:00
|
|
|
|
2014-07-07 10:05:29 +04:00
|
|
|
from libc.stdlib cimport calloc, free
|
|
|
|
|
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-08-16 21:59:38 +04:00
|
|
|
|
2014-08-28 21:45:09 +04:00
|
|
|
from .util import read_lang_data
|
2014-09-10 20:11:13 +04:00
|
|
|
from spacy.tokens import Tokens
|
2014-09-11 14:28:38 +04:00
|
|
|
from spacy.lexeme cimport LexemeC, lexeme_init
|
2014-09-12 04:04:47 +04:00
|
|
|
from murmurhash.mrmr cimport hash64
|
2014-09-15 05:22:40 +04:00
|
|
|
from cpython.ref cimport Py_INCREF
|
|
|
|
|
2014-08-28 21:45:09 +04:00
|
|
|
|
2014-09-13 05:14:43 +04:00
|
|
|
from spacy._hashing cimport PointerHash
|
2014-09-15 03:31:44 +04:00
|
|
|
from spacy import orth
|
|
|
|
from spacy import util
|
|
|
|
|
|
|
|
|
|
|
|
cdef enum Flags:
|
|
|
|
Flag_IsAlpha
|
|
|
|
Flag_IsAscii
|
|
|
|
Flag_IsDigit
|
|
|
|
Flag_IsLower
|
|
|
|
Flag_IsPunct
|
|
|
|
Flag_IsSpace
|
|
|
|
Flag_IsTitle
|
|
|
|
Flag_IsUpper
|
|
|
|
|
|
|
|
Flag_CanAdj
|
|
|
|
Flag_CanAdp
|
|
|
|
Flag_CanAdv
|
|
|
|
Flag_CanConj
|
|
|
|
Flag_CanDet
|
|
|
|
Flag_CanNoun
|
|
|
|
Flag_CanNum
|
|
|
|
Flag_CanPdt
|
|
|
|
Flag_CanPos
|
|
|
|
Flag_CanPron
|
|
|
|
Flag_CanPrt
|
|
|
|
Flag_CanPunct
|
|
|
|
Flag_CanVerb
|
|
|
|
|
|
|
|
Flag_OftLower
|
|
|
|
Flag_OftTitle
|
|
|
|
Flag_OftUpper
|
|
|
|
Flag_N
|
|
|
|
|
|
|
|
|
|
|
|
cdef enum Views:
|
|
|
|
View_CanonForm
|
|
|
|
View_WordShape
|
|
|
|
View_NonSparse
|
|
|
|
View_Asciied
|
|
|
|
View_N
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Assign the flag and view functions by enum value.
|
|
|
|
# This is verbose, but it ensures we don't get nasty order sensitivities.
|
|
|
|
STRING_VIEW_FUNCS = [None] * View_N
|
|
|
|
STRING_VIEW_FUNCS[View_CanonForm] = orth.canon_case
|
|
|
|
STRING_VIEW_FUNCS[View_WordShape] = orth.word_shape
|
|
|
|
STRING_VIEW_FUNCS[View_NonSparse] = orth.non_sparse
|
|
|
|
STRING_VIEW_FUNCS[View_Asciied] = orth.asciied
|
|
|
|
|
|
|
|
FLAG_FUNCS = [None] * Flag_N
|
|
|
|
FLAG_FUNCS[Flag_IsAlpha] = orth.is_alpha
|
|
|
|
FLAG_FUNCS[Flag_IsAscii] = orth.is_ascii
|
|
|
|
FLAG_FUNCS[Flag_IsDigit] = orth.is_digit
|
|
|
|
FLAG_FUNCS[Flag_IsLower] = orth.is_lower
|
|
|
|
FLAG_FUNCS[Flag_IsPunct] = orth.is_punct
|
|
|
|
FLAG_FUNCS[Flag_IsSpace] = orth.is_space
|
|
|
|
FLAG_FUNCS[Flag_IsTitle] = orth.is_title
|
|
|
|
FLAG_FUNCS[Flag_IsUpper] = orth.is_upper
|
|
|
|
|
|
|
|
FLAG_FUNCS[Flag_CanAdj] = orth.can_tag('ADJ')
|
|
|
|
FLAG_FUNCS[Flag_CanAdp] = orth.can_tag('ADP')
|
|
|
|
FLAG_FUNCS[Flag_CanAdv] = orth.can_tag('ADV')
|
|
|
|
FLAG_FUNCS[Flag_CanConj] = orth.can_tag('CONJ')
|
|
|
|
FLAG_FUNCS[Flag_CanDet] = orth.can_tag('DET')
|
|
|
|
FLAG_FUNCS[Flag_CanNoun] = orth.can_tag('NOUN')
|
|
|
|
FLAG_FUNCS[Flag_CanNum] = orth.can_tag('NUM')
|
|
|
|
FLAG_FUNCS[Flag_CanPdt] = orth.can_tag('PDT')
|
|
|
|
FLAG_FUNCS[Flag_CanPos] = orth.can_tag('POS')
|
|
|
|
FLAG_FUNCS[Flag_CanPron] = orth.can_tag('PRON')
|
|
|
|
FLAG_FUNCS[Flag_CanPrt] = orth.can_tag('PRT')
|
|
|
|
FLAG_FUNCS[Flag_CanPunct] = orth.can_tag('PUNCT')
|
|
|
|
FLAG_FUNCS[Flag_CanVerb] = orth.can_tag('VERB')
|
|
|
|
|
|
|
|
FLAG_FUNCS[Flag_OftLower] = orth.oft_case('lower', 0.7)
|
|
|
|
FLAG_FUNCS[Flag_OftTitle] = orth.oft_case('title', 0.7)
|
|
|
|
FLAG_FUNCS[Flag_OftUpper] = orth.oft_case('upper', 0.7)
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-07 18:58:48 +04:00
|
|
|
|
2014-07-07 14:47:21 +04:00
|
|
|
cdef class Language:
|
2014-08-28 21:45:09 +04:00
|
|
|
"""Base class for language-specific tokenizers.
|
|
|
|
|
|
|
|
Most subclasses will override the _split or _split_one methods, which take
|
|
|
|
a string of non-whitespace characters and output a list of strings. This
|
|
|
|
function is called by _tokenize, which sits behind a cache and turns the
|
|
|
|
list of strings into Lexeme objects via the Lexicon. Most languages will not
|
|
|
|
need to override _tokenize or tokenize.
|
|
|
|
|
|
|
|
The language is supplied a list of boolean functions, used to compute flag
|
|
|
|
features. These are passed to the language's Lexicon object.
|
|
|
|
|
|
|
|
The language's name is used to look up default data-files, found in data/<name.
|
|
|
|
"""
|
2014-09-15 03:31:44 +04:00
|
|
|
fl_is_alpha = Flag_IsAlpha
|
|
|
|
fl_is_digit = Flag_IsDigit
|
|
|
|
v_shape = View_WordShape
|
|
|
|
|
|
|
|
def __cinit__(self, name, user_string_features, user_flag_features):
|
2014-07-07 14:47:21 +04:00
|
|
|
self.name = name
|
2014-09-15 03:31:44 +04:00
|
|
|
self.cache = PointerHash(2 ** 25)
|
2014-09-13 18:43:42 +04:00
|
|
|
self.specials = PointerHash(2 ** 16)
|
2014-09-15 03:31:44 +04:00
|
|
|
lang_data = util.read_lang_data(name)
|
2014-08-28 21:45:09 +04:00
|
|
|
rules, words, probs, clusters, case_stats, tag_stats = lang_data
|
|
|
|
self.lexicon = Lexicon(words, probs, clusters, case_stats, tag_stats,
|
2014-09-15 03:31:44 +04:00
|
|
|
STRING_VIEW_FUNCS + user_string_features,
|
|
|
|
FLAG_FUNCS + user_flag_features)
|
2014-08-29 03:59:23 +04:00
|
|
|
self._load_special_tokenization(rules)
|
2014-09-10 20:11:13 +04:00
|
|
|
|
2014-09-13 01:50:37 +04:00
|
|
|
def __dealloc__(self):
|
2014-09-13 18:43:42 +04:00
|
|
|
pass
|
2014-09-13 01:50:37 +04:00
|
|
|
|
2014-09-10 20:11:13 +04:00
|
|
|
property nr_types:
|
|
|
|
def __get__(self):
|
|
|
|
"""Return the number of lexical types in the vocabulary"""
|
|
|
|
return self.lexicon.size
|
|
|
|
|
|
|
|
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 15:39:39 +04:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
The tokenization rules are defined in two 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;
|
|
|
|
* The appropriate :py:meth:`find_split` function, which is used to split
|
|
|
|
off punctuation etc.
|
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-08-27 19:15:39 +04:00
|
|
|
tokens (Tokens): A Tokens object, giving access to a sequence of LexIDs.
|
2014-08-20 15:39:39 +04:00
|
|
|
"""
|
2014-08-18 22:23:54 +04:00
|
|
|
cdef size_t length = len(string)
|
2014-09-15 03:31:44 +04:00
|
|
|
cdef Tokens tokens = Tokens(length)
|
2014-09-11 14:28:38 +04:00
|
|
|
if length == 0:
|
2014-09-11 23:37:32 +04:00
|
|
|
return tokens
|
2014-09-11 14:28:38 +04:00
|
|
|
|
2014-08-18 21:59:59 +04:00
|
|
|
cdef size_t start = 0
|
2014-08-18 22:48:48 +04:00
|
|
|
cdef size_t i = 0
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef Py_UNICODE* chars = string
|
2014-09-12 04:04:47 +04:00
|
|
|
cdef Py_UNICODE c
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef String span
|
2014-09-12 04:04:47 +04:00
|
|
|
for i in range(length):
|
2014-09-13 01:50:37 +04:00
|
|
|
c = chars[i]
|
2014-09-12 20:00:42 +04:00
|
|
|
if Py_UNICODE_ISSPACE(c) == 1:
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-09-13 01:50:37 +04:00
|
|
|
string_from_slice(&span, chars, start, i)
|
|
|
|
self._tokenize(tokens, &span)
|
2014-08-18 21:59:59 +04:00
|
|
|
start = i + 1
|
2014-09-12 04:04:47 +04:00
|
|
|
i += 1
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-09-13 01:50:37 +04:00
|
|
|
string_from_slice(&span, chars, start, i)
|
|
|
|
self._tokenize(tokens, &span)
|
2014-09-11 23:37:32 +04:00
|
|
|
return tokens
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-09-13 02:14:05 +04:00
|
|
|
cdef int _tokenize(self, Tokens tokens, String* string):
|
2014-09-13 19:02:06 +04:00
|
|
|
cdef LexemeC** lexemes = <LexemeC**>self.cache.get(string.key)
|
2014-09-13 02:59:34 +04:00
|
|
|
cdef size_t i
|
2014-09-13 18:43:42 +04:00
|
|
|
if lexemes != NULL:
|
2014-09-13 02:59:34 +04:00
|
|
|
i = 0
|
|
|
|
while lexemes[i] != NULL:
|
2014-09-15 05:22:40 +04:00
|
|
|
tokens.v.push_back(lexemes[i])
|
2014-09-13 02:59:34 +04:00
|
|
|
i += 1
|
2014-09-12 04:43:36 +04:00
|
|
|
return 0
|
2014-09-13 18:43:42 +04:00
|
|
|
cdef uint64_t key = string.key
|
2014-09-15 05:22:40 +04:00
|
|
|
cdef size_t first_token = len(tokens)
|
2014-09-13 02:31:53 +04:00
|
|
|
cdef int split
|
2014-09-13 02:14:05 +04:00
|
|
|
cdef int remaining = string.n
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef String prefix
|
2014-09-13 02:14:05 +04:00
|
|
|
while remaining >= 1:
|
2014-09-13 01:50:37 +04:00
|
|
|
split = self._split_one(string.chars, string.n)
|
2014-09-13 02:14:05 +04:00
|
|
|
remaining -= split
|
2014-09-13 01:50:37 +04:00
|
|
|
string_slice_prefix(string, &prefix, split)
|
2014-09-13 19:02:06 +04:00
|
|
|
lexemes = <LexemeC**>self.specials.get(prefix.key)
|
2014-09-13 01:50:37 +04:00
|
|
|
if lexemes != NULL:
|
2014-09-13 02:59:34 +04:00
|
|
|
i = 0
|
|
|
|
while lexemes[i] != NULL:
|
2014-09-15 05:22:40 +04:00
|
|
|
tokens.v.push_back(lexemes[i])
|
2014-09-13 02:59:34 +04:00
|
|
|
i += 1
|
2014-09-12 05:30:50 +04:00
|
|
|
else:
|
2014-09-15 05:22:40 +04:00
|
|
|
tokens.v.push_back(<LexemeC*>self.lexicon.get(&prefix))
|
|
|
|
lexemes = <LexemeC**>calloc(len(tokens) - first_token, sizeof(LexemeC*))
|
2014-09-13 02:14:05 +04:00
|
|
|
cdef size_t j
|
2014-09-15 05:22:40 +04:00
|
|
|
for i, j in enumerate(range(first_token, tokens.v.size())):
|
|
|
|
lexemes[i] = tokens.v[j]
|
2014-09-13 19:02:06 +04:00
|
|
|
self.cache.set(key, lexemes)
|
2014-08-27 19:15:39 +04:00
|
|
|
|
2014-09-12 06:29:09 +04:00
|
|
|
cdef int _split_one(self, Py_UNICODE* characters, size_t length):
|
|
|
|
return length
|
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-08-27 19:15:39 +04: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-09-13 01:50:37 +04:00
|
|
|
cdef LexemeC** lexemes
|
2014-09-12 04:04:47 +04:00
|
|
|
cdef uint64_t hashed
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef String string
|
|
|
|
for uni_string, substrings in token_rules:
|
|
|
|
lexemes = <LexemeC**>calloc(len(substrings) + 1, sizeof(LexemeC*))
|
|
|
|
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 19:02:06 +04: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-08-28 21:45:09 +04:00
|
|
|
def __cinit__(self, words, probs, clusters, case_stats, tag_stats,
|
|
|
|
string_features, flag_features):
|
2014-08-29 03:59:23 +04:00
|
|
|
self._flag_features = flag_features
|
|
|
|
self._string_features = string_features
|
2014-09-13 18:43:42 +04:00
|
|
|
self._dict = PointerHash(2 ** 20)
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size = 0
|
2014-09-13 19:02:06 +04:00
|
|
|
cdef String string
|
|
|
|
for uni_string in words:
|
|
|
|
prob = probs.get(uni_string, 0.0)
|
|
|
|
cluster = clusters.get(uni_string, 0.0)
|
|
|
|
cases = case_stats.get(uni_string, {})
|
|
|
|
tags = tag_stats.get(uni_string, {})
|
|
|
|
views = [string_view(uni_string, prob, cluster, cases, tags)
|
2014-09-11 14:28:38 +04:00
|
|
|
for string_view in self._string_features]
|
|
|
|
flags = set()
|
|
|
|
for i, flag_feature in enumerate(self._flag_features):
|
2014-09-13 19:02:06 +04:00
|
|
|
if flag_feature(uni_string, prob, cluster, cases, tags):
|
2014-09-11 14:28:38 +04:00
|
|
|
flags.add(i)
|
2014-09-13 19:02:06 +04:00
|
|
|
lexeme = lexeme_init(uni_string, prob, cluster, views, flags)
|
|
|
|
string_from_unicode(&string, uni_string)
|
|
|
|
self._dict.set(string.key, lexeme)
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size += 1
|
2014-08-20 15:39:39 +04:00
|
|
|
|
2014-09-13 19:29:58 +04:00
|
|
|
cdef LexemeC* get(self, String* string):
|
|
|
|
cdef LexemeC* lexeme
|
|
|
|
lexeme = <LexemeC*>self._dict.get(string.key)
|
|
|
|
if lexeme != NULL:
|
|
|
|
return lexeme
|
2014-08-27 19:15:39 +04:00
|
|
|
|
2014-09-13 01:50:37 +04:00
|
|
|
cdef unicode uni_string = string.chars[:string.n]
|
|
|
|
views = [string_view(uni_string, 0.0, 0, {}, {})
|
2014-09-11 14:28:38 +04:00
|
|
|
for string_view in self._string_features]
|
|
|
|
flags = set()
|
|
|
|
for i, flag_feature in enumerate(self._flag_features):
|
2014-09-13 01:50:37 +04:00
|
|
|
if flag_feature(uni_string, 0.0, {}, {}):
|
2014-09-11 14:28:38 +04:00
|
|
|
flags.add(i)
|
|
|
|
|
2014-09-13 19:29:58 +04:00
|
|
|
lexeme = lexeme_init(uni_string, 0, 0, views, flags)
|
2014-09-13 19:02:06 +04:00
|
|
|
self._dict.set(string.key, lexeme)
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size += 1
|
2014-09-13 19:29:58 +04:00
|
|
|
return lexeme
|
2014-09-11 23:54:34 +04:00
|
|
|
|
2014-09-13 01:50:37 +04:00
|
|
|
cpdef Lexeme lookup(self, unicode uni_string):
|
2014-09-11 23:54:34 +04: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-13 01:50:37 +04:00
|
|
|
cdef String string
|
|
|
|
string_from_unicode(&string, uni_string)
|
2014-09-13 19:29:58 +04:00
|
|
|
cdef LexemeC* lexeme = self.get(&string)
|
|
|
|
return Lexeme(<size_t>lexeme)
|
2014-09-13 01:50:37 +04:00
|
|
|
|
|
|
|
|
2014-09-13 02:14:05 +04:00
|
|
|
cdef void string_from_unicode(String* s, unicode uni):
|
2014-09-15 05:22:40 +04:00
|
|
|
Py_INCREF(uni)
|
2014-09-15 03:31:44 +04:00
|
|
|
cdef Py_UNICODE* c_uni = <Py_UNICODE*>uni
|
|
|
|
string_from_slice(s, c_uni, 0, len(uni))
|
2014-09-13 01:50:37 +04:00
|
|
|
|
|
|
|
|
2014-09-13 02:31:53 +04:00
|
|
|
cdef inline void string_from_slice(String* s, Py_UNICODE* chars, size_t start, size_t end) nogil:
|
2014-09-13 01:50:37 +04:00
|
|
|
s.chars = &chars[start]
|
|
|
|
s.n = end - start
|
|
|
|
s.key = hash64(s.chars, s.n * sizeof(Py_UNICODE), 0)
|
|
|
|
|
|
|
|
|
2014-09-13 02:31:53 +04:00
|
|
|
cdef inline void string_slice_prefix(String* s, String* prefix, size_t n) nogil:
|
2014-09-13 01:50:37 +04: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)
|