From ea85bf3a0ab14087f13fdfce6e4ac750bb3c8de4 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 30 Oct 2014 18:01:27 +1100 Subject: [PATCH] * Tighten the interface to Language --- spacy/lang.pxd | 14 +++++++------- spacy/lang.pyx | 44 ++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/spacy/lang.pxd b/spacy/lang.pxd index e2c7c56e6..9d6419557 100644 --- a/spacy/lang.pxd +++ b/spacy/lang.pxd @@ -34,15 +34,15 @@ cdef class Lexicon: cdef class Language: - cdef Pool _mem - cdef unicode name - cdef PreshMap cache - cdef PreshMap specials + cdef Pool mem + cdef readonly unicode name + cdef PreshMap _cache + cdef PreshMap _specials cpdef readonly Lexicon lexicon - cdef object prefix_re - cdef object suffix_re - cdef object infix_re + cdef object _prefix_re + cdef object _suffix_re + cdef object _infix_re cpdef Tokens tokenize(self, unicode text) diff --git a/spacy/lang.pyx b/spacy/lang.pyx index 5042ff4b2..114c10c66 100644 --- a/spacy/lang.pyx +++ b/spacy/lang.pyx @@ -35,15 +35,15 @@ cdef class Language: The language's name is used to look up default data-files, found in data/self.cache.get(span.key) + lexemes = self._cache.get(span.key) if lexemes != NULL: tokens.extend(start, lexemes, 0) else: @@ -90,7 +90,7 @@ cdef class Language: i += 1 if start < i: string_slice(&span, chars, start, i) - lexemes = self.cache.get(span.key) + lexemes = self._cache.get(span.key) if lexemes != NULL: tokens.extend(start, lexemes, 0) else: @@ -123,7 +123,7 @@ cdef class Language: string_slice(&prefix, string.chars, 0, pre_len) string_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: + if minus_pre.n >= 1 and self._specials.get(minus_pre.key) != NULL: string[0] = minus_pre prefixes.push_back(self.lexicon.get(&prefix)) break @@ -132,7 +132,7 @@ cdef class Language: string_slice(&suffix, string.chars, string.n - suf_len, string.n) string_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: + if minus_suf.n >= 1 and self._specials.get(minus_suf.key) != NULL: string[0] = minus_suf suffixes.push_back(self.lexicon.get(&suffix)) break @@ -146,7 +146,7 @@ cdef class Language: elif suf_len: string[0] = minus_suf suffixes.push_back(self.lexicon.get(&suffix)) - if self.specials.get(string.key): + if self._specials.get(string.key): break return string @@ -162,7 +162,7 @@ cdef class Language: idx = tokens.extend(idx, prefixes.data(), prefixes.size()) if string.n != 0: - lexemes = self.cache.get(string.key) + lexemes = self._cache.get(string.key) if lexemes != NULL: idx = tokens.extend(idx, lexemes, 0) else: @@ -182,32 +182,32 @@ cdef class Language: preinc(it) cdef int _save_cached(self, Lexeme** tokens, hash_t key, int n) except -1: - lexemes = self._mem.alloc(n + 1, sizeof(Lexeme**)) + lexemes = self.mem.alloc(n + 1, sizeof(Lexeme**)) cdef int i for i in range(n): lexemes[i] = tokens[i] lexemes[i + 1] = NULL - self.cache.set(key, lexemes) + self._cache.set(key, lexemes) cdef int _find_infix(self, Py_UNICODE* chars, size_t length) except -1: cdef unicode string = chars[:length] - match = self.infix_re.search(string) + match = self._infix_re.search(string) return match.start() if match is not None else 0 cdef int _find_prefix(self, Py_UNICODE* chars, size_t length) except -1: cdef unicode string = chars[:length] - match = self.prefix_re.search(string) + match = self._prefix_re.search(string) return (match.end() - match.start()) if match is not None else 0 cdef int _find_suffix(self, Py_UNICODE* chars, size_t length) except -1: cdef unicode string = chars[:length] - match = self.suffix_re.search(string) + match = self._suffix_re.search(string) return (match.end() - match.start()) if match is not None else 0 def _load_special_tokenization(self, token_rules): '''Load special-case tokenization rules. - Loads special-case tokenization rules into the Language.cache cache, + Loads special-case tokenization rules into the Language._cache cache, read from data//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"]. @@ -220,14 +220,14 @@ cdef class Language: cdef hash_t hashed cdef String string for uni_string, substrings in token_rules: - lexemes = self._mem.alloc(len(substrings) + 1, sizeof(Lexeme*)) + lexemes = self.mem.alloc(len(substrings) + 1, sizeof(Lexeme*)) for i, substring in enumerate(substrings): string_from_unicode(&string, substring) lexemes[i] = self.lexicon.get(&string) lexemes[i + 1] = NULL string_from_unicode(&string, uni_string) - self.specials.set(string.key, lexemes) - self.cache.set(string.key, lexemes) + self._specials.set(string.key, lexemes) + self._cache.set(string.key, lexemes) cdef class Lexicon: