2014-07-07 05:36:43 +00:00
|
|
|
# cython: profile=True
|
2014-07-05 18:51:42 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-07-07 02:21:06 +00:00
|
|
|
|
2014-07-07 06:05:29 +00:00
|
|
|
from libc.stdlib cimport calloc, free
|
2014-07-31 17:19:38 +00:00
|
|
|
from libcpp.pair cimport pair
|
|
|
|
from cython.operator cimport dereference as deref
|
2014-07-07 06:05:29 +00:00
|
|
|
|
2014-07-07 14:58:48 +00:00
|
|
|
from spacy.lexeme cimport Lexeme
|
2014-07-07 02:21:06 +00:00
|
|
|
from spacy.lexeme cimport BLANK_WORD
|
|
|
|
|
2014-07-07 14:58:48 +00:00
|
|
|
from spacy.string_tools cimport substr
|
2014-07-07 02:21:06 +00:00
|
|
|
|
|
|
|
from . import util
|
2014-07-07 05:36:43 +00:00
|
|
|
from os import path
|
2014-08-16 17:59:38 +00:00
|
|
|
|
|
|
|
|
2014-07-07 14:58:48 +00:00
|
|
|
def get_normalized(unicode lex, size_t length):
|
2014-07-23 16:35:18 +00:00
|
|
|
if lex.isalpha() and lex.islower():
|
|
|
|
return lex
|
|
|
|
else:
|
|
|
|
return get_word_shape(lex, length)
|
2014-07-07 14:58:48 +00:00
|
|
|
|
|
|
|
|
2014-08-16 12:35:34 +00:00
|
|
|
def get_word_shape(unicode lex, length):
|
2014-07-07 17:12:19 +00:00
|
|
|
shape = ""
|
|
|
|
last = ""
|
|
|
|
shape_char = ""
|
|
|
|
seq = 0
|
|
|
|
for c in lex:
|
|
|
|
if c.isalpha():
|
|
|
|
if c.isupper():
|
|
|
|
shape_char = "X"
|
|
|
|
else:
|
|
|
|
shape_char = "x"
|
|
|
|
elif c.isdigit():
|
|
|
|
shape_char = "d"
|
|
|
|
else:
|
|
|
|
shape_char = c
|
|
|
|
if shape_char == last:
|
|
|
|
seq += 1
|
|
|
|
else:
|
|
|
|
seq = 0
|
|
|
|
last = shape_char
|
|
|
|
if seq < 3:
|
|
|
|
shape += shape_char
|
|
|
|
assert shape
|
|
|
|
return shape
|
|
|
|
|
2014-07-07 14:58:48 +00:00
|
|
|
|
|
|
|
def set_orth_flags(lex, length):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2014-07-07 10:47:21 +00:00
|
|
|
cdef class Language:
|
|
|
|
def __cinit__(self, name):
|
|
|
|
self.name = name
|
|
|
|
self.bacov = {}
|
2014-08-18 17:14:00 +00:00
|
|
|
self.chunks = dense_hash_map[StringHash, size_t]()
|
|
|
|
self.vocab = dense_hash_map[StringHash, size_t]()
|
|
|
|
self.chunks.set_empty_key(0)
|
|
|
|
self.vocab.set_empty_key(0)
|
2014-07-07 10:47:21 +00:00
|
|
|
self.load_tokenization(util.read_tokenization(name))
|
2014-08-19 00:40:37 +00:00
|
|
|
self.load_dist_info(util.read_dist_info(name))
|
2014-07-07 10:47:21 +00:00
|
|
|
|
2014-08-18 17:59:59 +00:00
|
|
|
cdef Tokens tokenize(self, unicode string):
|
2014-08-18 17:14:00 +00:00
|
|
|
cdef Lexeme** chunk
|
2014-07-07 10:47:21 +00:00
|
|
|
cdef Tokens tokens = Tokens(self)
|
2014-08-18 18:23:54 +00:00
|
|
|
cdef size_t length = len(string)
|
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
|
|
|
|
for c in string:
|
2014-08-18 17:59:59 +00:00
|
|
|
if _is_whitespace(c):
|
|
|
|
if start < i:
|
2014-08-18 18:48:48 +00:00
|
|
|
chunk = self.lookup_chunk(string[start:i])
|
2014-08-18 17:59:59 +00:00
|
|
|
_extend(tokens, chunk)
|
|
|
|
start = i + 1
|
2014-08-18 18:48:48 +00:00
|
|
|
i += 1
|
2014-08-18 17:59:59 +00:00
|
|
|
if start < i:
|
2014-08-18 18:48:48 +00:00
|
|
|
chunk = self.lookup_chunk(string[start:])
|
2014-08-18 17:59:59 +00:00
|
|
|
_extend(tokens, chunk)
|
2014-07-07 10:47:21 +00:00
|
|
|
return tokens
|
|
|
|
|
2014-08-18 17:14:00 +00:00
|
|
|
cdef Lexeme* lookup(self, unicode string) except NULL:
|
|
|
|
if len(string) == 0:
|
|
|
|
return &BLANK_WORD
|
|
|
|
cdef Lexeme* word = <Lexeme*>self.vocab[hash(string)]
|
|
|
|
if word == NULL:
|
|
|
|
word = self.new_lexeme(string)
|
|
|
|
return word
|
2014-07-07 14:58:48 +00:00
|
|
|
|
2014-08-18 18:48:48 +00:00
|
|
|
cdef Lexeme** lookup_chunk(self, unicode string) except NULL:
|
|
|
|
cdef StringHash h = hash(string)
|
2014-08-18 17:59:59 +00:00
|
|
|
cdef Lexeme** chunk = <Lexeme**>self.chunks[h]
|
2014-08-18 17:14:00 +00:00
|
|
|
cdef int split
|
|
|
|
if chunk == NULL:
|
2014-08-18 18:48:48 +00:00
|
|
|
chunk = self.new_chunk(string, self.find_substrings(string))
|
2014-08-18 17:14:00 +00:00
|
|
|
return chunk
|
|
|
|
|
|
|
|
cdef Lexeme** new_chunk(self, unicode string, list substrings) except NULL:
|
|
|
|
cdef Lexeme** chunk = <Lexeme**>calloc(len(substrings) + 1, sizeof(Lexeme*))
|
|
|
|
for i, substring in enumerate(substrings):
|
|
|
|
chunk[i] = self.lookup(substring)
|
|
|
|
chunk[i + 1] = NULL
|
2014-08-18 18:48:48 +00:00
|
|
|
self.chunks[hash(string)] = <size_t>chunk
|
2014-08-18 17:14:00 +00:00
|
|
|
return chunk
|
|
|
|
|
|
|
|
cdef Lexeme* new_lexeme(self, unicode string) except NULL:
|
2014-08-16 01:22:03 +00:00
|
|
|
cdef Lexeme* word = <Lexeme*>calloc(1, sizeof(Lexeme))
|
2014-08-16 15:37:09 +00:00
|
|
|
word.lex = hash(string)
|
2014-08-16 14:09:24 +00:00
|
|
|
self.bacov[word.lex] = string
|
2014-08-18 17:14:00 +00:00
|
|
|
word.orth = self.new_orth(string)
|
2014-08-19 00:40:37 +00:00
|
|
|
|
|
|
|
word.dist = <Distribution*>calloc(1, sizeof(Distribution))
|
2014-08-18 17:14:00 +00:00
|
|
|
self.vocab[word.lex] = <size_t>word
|
2014-08-16 18:10:22 +00:00
|
|
|
return word
|
2014-08-16 01:22:03 +00:00
|
|
|
|
2014-08-16 18:10:22 +00:00
|
|
|
cdef Orthography* new_orth(self, unicode lex) except NULL:
|
2014-08-16 01:22:03 +00:00
|
|
|
cdef unicode last3
|
|
|
|
cdef unicode norm
|
|
|
|
cdef unicode shape
|
|
|
|
cdef int length
|
|
|
|
|
|
|
|
length = len(lex)
|
|
|
|
orth = <Orthography*>calloc(1, sizeof(Orthography))
|
2014-08-16 12:35:34 +00:00
|
|
|
orth.first = lex[0]
|
2014-08-16 01:22:03 +00:00
|
|
|
|
|
|
|
orth.length = length
|
|
|
|
orth.flags = set_orth_flags(lex, orth.length)
|
2014-08-16 18:10:22 +00:00
|
|
|
orth.norm = hash(lex)
|
2014-08-16 14:09:24 +00:00
|
|
|
last3 = substr(lex, length - 3, length, length)
|
2014-08-16 15:37:09 +00:00
|
|
|
orth.last3 = hash(last3)
|
2014-08-16 14:09:24 +00:00
|
|
|
norm = get_normalized(lex, length)
|
2014-08-16 15:37:09 +00:00
|
|
|
orth.norm = hash(norm)
|
2014-08-16 14:09:24 +00:00
|
|
|
shape = get_word_shape(lex, length)
|
2014-08-16 15:37:09 +00:00
|
|
|
orth.shape = hash(shape)
|
2014-08-16 14:09:24 +00:00
|
|
|
|
|
|
|
self.bacov[orth.last3] = last3
|
|
|
|
self.bacov[orth.norm] = norm
|
|
|
|
self.bacov[orth.shape] = shape
|
2014-07-07 14:58:48 +00:00
|
|
|
return orth
|
2014-07-07 10:47:21 +00:00
|
|
|
|
2014-08-16 01:22:03 +00:00
|
|
|
cdef unicode unhash(self, StringHash hash_value):
|
|
|
|
'''Fetch a string from the reverse index, given its hash value.'''
|
2014-08-16 12:35:34 +00:00
|
|
|
return self.bacov[hash_value]
|
2014-08-16 01:22:03 +00:00
|
|
|
|
2014-08-18 17:14:00 +00:00
|
|
|
cpdef list find_substrings(self, unicode word):
|
|
|
|
substrings = []
|
|
|
|
while word:
|
|
|
|
split = self.find_split(word)
|
|
|
|
if split == 0:
|
|
|
|
substrings.append(word)
|
|
|
|
break
|
|
|
|
substrings.append(word[:split])
|
|
|
|
word = word[split:]
|
|
|
|
return substrings
|
|
|
|
|
|
|
|
cdef int find_split(self, unicode word):
|
|
|
|
return len(word)
|
2014-08-16 01:22:03 +00:00
|
|
|
|
|
|
|
def load_tokenization(self, token_rules=None):
|
2014-08-18 17:14:00 +00:00
|
|
|
for chunk, tokens in token_rules:
|
2014-08-18 18:48:48 +00:00
|
|
|
self.new_chunk(chunk, tokens)
|
2014-08-18 17:59:59 +00:00
|
|
|
|
2014-08-19 00:40:37 +00:00
|
|
|
def load_dist_info(self, dist_info):
|
|
|
|
cdef unicode string
|
|
|
|
cdef dict word_dist
|
2014-08-16 01:22:03 +00:00
|
|
|
cdef Lexeme* w
|
2014-08-19 00:40:37 +00:00
|
|
|
for string, word_dist in dist_info.items():
|
|
|
|
w = self.lookup(string)
|
|
|
|
w.prob = word_dist.prob
|
|
|
|
w.cluster = word_dist.cluster
|
|
|
|
for flag in word_dist.flags:
|
|
|
|
w.flags |= lexeme.DIST_FLAGS[flag]
|
|
|
|
for tag in word_dist.tagdict:
|
|
|
|
w.tagdict |= lexeme.TAGS[tag]
|
2014-08-18 17:59:59 +00:00
|
|
|
|
|
|
|
|
2014-08-18 18:23:54 +00:00
|
|
|
cdef inline bint _is_whitespace(Py_UNICODE c) nogil:
|
|
|
|
if c == ' ':
|
2014-08-18 17:59:59 +00:00
|
|
|
return True
|
2014-08-18 18:23:54 +00:00
|
|
|
elif c == '\n':
|
2014-08-18 17:59:59 +00:00
|
|
|
return True
|
2014-08-18 18:23:54 +00:00
|
|
|
elif c == '\t':
|
2014-08-18 17:59:59 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-08-18 18:48:48 +00:00
|
|
|
cdef inline int _extend(Tokens tokens, Lexeme** chunk) nogil:
|
2014-08-18 17:59:59 +00:00
|
|
|
cdef size_t i = 0
|
|
|
|
while chunk[i] != NULL:
|
2014-08-18 18:48:48 +00:00
|
|
|
tokens.vctr[0].push_back(<Lexeme_addr>chunk[i])
|
|
|
|
tokens.length += 1
|
2014-08-18 17:59:59 +00:00
|
|
|
i += 1
|