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-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-08-28 21:45:09 +04:00
|
|
|
|
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-08-30 21:01:15 +04:00
|
|
|
def __cinit__(self, name, string_features, flag_features):
|
2014-08-28 21:45:09 +04:00
|
|
|
if flag_features is None:
|
|
|
|
flag_features = []
|
|
|
|
if string_features is None:
|
|
|
|
string_features = []
|
2014-07-07 14:47:21 +04:00
|
|
|
self.name = name
|
2014-08-27 19:15:39 +04:00
|
|
|
self.cache = {}
|
2014-08-28 21:45:09 +04:00
|
|
|
lang_data = read_lang_data(name)
|
|
|
|
rules, words, probs, clusters, case_stats, tag_stats = lang_data
|
|
|
|
self.lexicon = Lexicon(words, probs, clusters, case_stats, tag_stats,
|
|
|
|
string_features, flag_features)
|
2014-08-29 03:59:23 +04:00
|
|
|
self._load_special_tokenization(rules)
|
2014-09-10 20:27:44 +04:00
|
|
|
self.tokens_class = Tokens
|
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-11 23:37:32 +04:00
|
|
|
cdef Tokens tokens = self.tokens_class(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
|
|
|
|
for c in string:
|
2014-08-25 18:42:22 +04:00
|
|
|
if c == ' ':
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-09-10 20:11:13 +04:00
|
|
|
self._tokenize(tokens, string[start:i])
|
2014-08-18 21:59:59 +04:00
|
|
|
start = i + 1
|
2014-08-18 22:48:48 +04:00
|
|
|
i += 1
|
2014-08-18 21:59:59 +04:00
|
|
|
if start < i:
|
2014-09-10 20:11:13 +04:00
|
|
|
self._tokenize(tokens, string[start:i])
|
2014-09-11 23:37:32 +04:00
|
|
|
return tokens
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-09-10 20:11:13 +04:00
|
|
|
cdef _tokenize(self, Tokens tokens, unicode string):
|
2014-09-11 23:44:58 +04:00
|
|
|
cdef LexemeC** lexemes
|
|
|
|
if string in self.cache:
|
|
|
|
lexemes = <LexemeC**><size_t>self.cache[string]
|
2014-09-10 20:11:13 +04:00
|
|
|
else:
|
|
|
|
substrings = self._split(string)
|
2014-09-11 23:44:58 +04:00
|
|
|
lexemes = <LexemeC**>calloc(len(substrings) + 1, sizeof(LexemeC*))
|
2014-09-10 20:11:13 +04:00
|
|
|
for i, substring in enumerate(substrings):
|
2014-09-11 23:44:58 +04:00
|
|
|
lexemes[i] = self.lexicon.lookup(substring)._c
|
|
|
|
lexemes[i + 1] = NULL
|
|
|
|
self.cache[string] = <size_t>lexemes
|
|
|
|
cdef LexemeC* lexeme
|
|
|
|
i = 0
|
|
|
|
while lexemes[i] != NULL:
|
|
|
|
tokens.push_back(lexemes[i])
|
|
|
|
i += 1
|
2014-08-27 19:15:39 +04:00
|
|
|
|
2014-08-29 03:59:23 +04:00
|
|
|
cdef list _split(self, unicode string):
|
2014-08-27 19:15:39 +04:00
|
|
|
"""Find how to split a contiguous span of non-space characters into substrings.
|
2014-08-20 15:39:39 +04:00
|
|
|
|
|
|
|
This method calls find_split repeatedly. Most languages will want to
|
2014-08-27 19:15:39 +04:00
|
|
|
override _split_one, but it may be useful to override this instead.
|
2014-08-20 15:39:39 +04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chunk (unicode): The string to be split, e.g. u"Mike's!"
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
substrings (list): The component substrings, e.g. [u"Mike", "'s", "!"].
|
|
|
|
"""
|
2014-08-18 21:14:00 +04:00
|
|
|
substrings = []
|
2014-08-27 19:15:39 +04:00
|
|
|
while string:
|
|
|
|
split = self._split_one(string)
|
2014-08-18 21:14:00 +04:00
|
|
|
if split == 0:
|
2014-08-27 19:15:39 +04:00
|
|
|
substrings.append(string)
|
2014-08-18 21:14:00 +04:00
|
|
|
break
|
2014-08-27 19:15:39 +04:00
|
|
|
substrings.append(string[:split])
|
|
|
|
string = string[split:]
|
2014-08-18 21:14:00 +04:00
|
|
|
return substrings
|
|
|
|
|
2014-08-29 03:59:23 +04:00
|
|
|
cdef int _split_one(self, unicode word):
|
2014-08-18 21:14:00 +04:00
|
|
|
return len(word)
|
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-11 23:44:58 +04:00
|
|
|
cdef LexemeC** lexemes
|
2014-08-27 19:15:39 +04:00
|
|
|
for string, substrings in token_rules:
|
2014-09-11 23:44:58 +04:00
|
|
|
lexemes = <LexemeC**>calloc(len(substrings) + 1, sizeof(LexemeC*))
|
2014-08-27 19:15:39 +04:00
|
|
|
for i, substring in enumerate(substrings):
|
2014-09-11 23:44:58 +04:00
|
|
|
lexemes[i] = self.lexicon.lookup(substring)._c
|
|
|
|
lexemes[i + 1] = NULL
|
|
|
|
self.cache[string] = <size_t>lexemes
|
2014-08-27 19:15:39 +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-08-28 21:45:09 +04:00
|
|
|
self._dict = {}
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size = 0
|
2014-08-28 21:45:09 +04:00
|
|
|
cdef Lexeme word
|
|
|
|
for string in words:
|
2014-09-11 14:28:38 +04:00
|
|
|
prob = probs.get(string, 0.0)
|
|
|
|
cluster = clusters.get(string, 0.0)
|
|
|
|
cases = case_stats.get(string, {})
|
|
|
|
tags = tag_stats.get(string, {})
|
|
|
|
views = [string_view(string, prob, cluster, cases, tags)
|
|
|
|
for string_view in self._string_features]
|
|
|
|
flags = set()
|
|
|
|
for i, flag_feature in enumerate(self._flag_features):
|
|
|
|
if flag_feature(string, prob, cluster, cases, tags):
|
|
|
|
flags.add(i)
|
|
|
|
lexeme = lexeme_init(string, prob, cluster, views, flags)
|
|
|
|
self._dict[string] = <size_t>lexeme
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size += 1
|
2014-08-20 15:39:39 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
cpdef Lexeme lookup(self, unicode string):
|
|
|
|
"""Retrieve (or create, if not found) a Lexeme for a string, and return it.
|
|
|
|
|
2014-08-28 21:45:09 +04:00
|
|
|
Args
|
2014-08-27 19:15:39 +04:00
|
|
|
string (unicode): The string to be looked up. Must be unicode, not bytes.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
lexeme (Lexeme): A reference to a lexical type.
|
|
|
|
"""
|
2014-09-11 14:28:38 +04:00
|
|
|
cdef LexemeC* lexeme
|
2014-08-27 19:15:39 +04:00
|
|
|
assert len(string) != 0
|
2014-08-28 21:45:09 +04:00
|
|
|
if string in self._dict:
|
2014-09-11 14:28:38 +04:00
|
|
|
return Lexeme(self._dict[string])
|
2014-08-27 19:15:39 +04:00
|
|
|
|
2014-09-11 14:28:38 +04:00
|
|
|
views = [string_view(string, 0.0, 0, {}, {})
|
|
|
|
for string_view in self._string_features]
|
|
|
|
flags = set()
|
|
|
|
for i, flag_feature in enumerate(self._flag_features):
|
|
|
|
if flag_feature(string, 0.0, {}, {}):
|
|
|
|
flags.add(i)
|
|
|
|
|
|
|
|
lexeme = lexeme_init(string, 0, 0, views, flags)
|
|
|
|
self._dict[string] = <size_t>lexeme
|
2014-09-10 20:11:13 +04:00
|
|
|
self.size += 1
|
2014-09-11 14:28:38 +04:00
|
|
|
return Lexeme(<size_t>lexeme)
|