2014-07-07 05:36:43 +00:00
|
|
|
# cython: profile=True
|
2014-07-05 18:51:42 +00:00
|
|
|
'''Serve pointers to Lexeme structs, given strings. Maintain a reverse index,
|
|
|
|
so that strings can be retrieved from hashes. Use 64-bit hash values and
|
|
|
|
boldly assume no collisions.
|
|
|
|
'''
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from libc.stdlib cimport malloc, calloc, free
|
|
|
|
from libc.stdint cimport uint64_t
|
2014-07-06 23:15:59 +00:00
|
|
|
from libcpp.vector cimport vector
|
2014-07-05 18:51:42 +00:00
|
|
|
|
|
|
|
from spacy.lexeme cimport Lexeme
|
2014-07-07 02:21:06 +00:00
|
|
|
from spacy.string_tools cimport substr
|
2014-07-06 22:02:55 +00:00
|
|
|
from . import util
|
2014-07-05 18:51:42 +00:00
|
|
|
|
2014-07-07 02:21:06 +00:00
|
|
|
cimport spacy
|
2014-07-05 18:51:42 +00:00
|
|
|
|
2014-07-07 02:21:06 +00:00
|
|
|
BACOV = {}
|
2014-07-07 05:36:43 +00:00
|
|
|
VOCAB = new Vocab(100000)
|
2014-07-07 02:21:06 +00:00
|
|
|
VOCAB.set_empty_key(0)
|
2014-07-05 18:51:42 +00:00
|
|
|
|
|
|
|
|
2014-07-07 02:21:06 +00:00
|
|
|
spacy.load_tokenization(VOCAB, BACOV, util.read_tokenization('en'))
|
2014-07-06 22:02:55 +00:00
|
|
|
|
2014-07-06 23:15:59 +00:00
|
|
|
cpdef vector[Lexeme_addr] tokenize(unicode string) except *:
|
2014-07-07 02:21:06 +00:00
|
|
|
return spacy.tokenize(VOCAB, BACOV, find_split, string)
|
|
|
|
|
2014-07-06 23:15:59 +00:00
|
|
|
|
2014-07-05 18:51:42 +00:00
|
|
|
cpdef Lexeme_addr lookup(unicode string) except 0:
|
2014-07-07 05:36:43 +00:00
|
|
|
return spacy.lookup(VOCAB, BACOV, find_split, -1, string, len(string))
|
2014-07-05 18:51:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
cpdef unicode unhash(StringHash hash_value):
|
2014-07-07 02:21:06 +00:00
|
|
|
return spacy.unhash(BACOV, hash_value)
|
2014-07-05 18:51:42 +00:00
|
|
|
|
|
|
|
|
2014-07-07 02:21:06 +00:00
|
|
|
cdef vector[StringHash] make_string_views(unicode word):
|
2014-07-05 18:51:42 +00:00
|
|
|
cdef unicode s
|
2014-07-07 02:21:06 +00:00
|
|
|
return vector[StringHash]()
|
|
|
|
#if word.isdigit() and len(word) == 4:
|
|
|
|
# return '!YEAR'
|
|
|
|
#elif word[0].isdigit():
|
|
|
|
# return '!DIGITS'
|
|
|
|
#else:
|
|
|
|
# return word.lower()
|
2014-07-05 18:51:42 +00:00
|
|
|
|
|
|
|
|
2014-07-07 02:21:06 +00:00
|
|
|
cdef int find_split(unicode word, size_t length):
|
2014-07-06 17:28:42 +00:00
|
|
|
cdef int i = 0
|
|
|
|
# Contractions
|
2014-07-06 22:02:55 +00:00
|
|
|
if word.endswith("'s"):
|
|
|
|
return length - 2
|
2014-07-06 17:28:42 +00:00
|
|
|
# Leading punctuation
|
|
|
|
if is_punct(word, 0, length):
|
|
|
|
return 1
|
2014-07-06 23:15:59 +00:00
|
|
|
elif length >= 1:
|
2014-07-06 17:28:42 +00:00
|
|
|
# Split off all trailing punctuation characters
|
2014-07-06 23:15:59 +00:00
|
|
|
i = 0
|
|
|
|
while i < length and not is_punct(word, i, length):
|
|
|
|
i += 1
|
2014-07-05 18:51:42 +00:00
|
|
|
return i
|
2014-07-06 16:35:55 +00:00
|
|
|
|
2014-07-06 22:02:55 +00:00
|
|
|
|
2014-07-06 16:35:55 +00:00
|
|
|
cdef bint is_punct(unicode word, size_t i, size_t length):
|
2014-07-06 23:15:59 +00:00
|
|
|
# Don't count appostrophes as punct if the next char is a letter
|
|
|
|
if word[i] == "'" and i < (length - 1) and word[i+1].isalpha():
|
|
|
|
return False
|
|
|
|
# Don't count commas as punct if the next char is a number
|
|
|
|
if word[i] == "," and i < (length - 1) and word[i+1].isdigit():
|
|
|
|
return False
|
|
|
|
# Don't count periods as punct if the next char is a number
|
|
|
|
if word[i] == "." and i < (length - 1) and word[i+1].isdigit():
|
|
|
|
return False
|
2014-07-06 17:28:42 +00:00
|
|
|
return not word[i].isalnum()
|
2014-07-07 05:36:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
#spacy.load_browns(VOCAB, BACOV, find_split)
|