2014-07-07 09:36:43 +04:00
|
|
|
# cython: profile=True
|
2014-07-05 22:51:42 +04: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-07 03:15:59 +04:00
|
|
|
from libcpp.vector cimport vector
|
2014-07-05 22:51:42 +04:00
|
|
|
|
2014-07-07 06:21:06 +04:00
|
|
|
from spacy.string_tools cimport substr
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-07-07 02:02:55 +04:00
|
|
|
from . import util
|
2014-07-05 22:51:42 +04:00
|
|
|
|
2014-07-07 06:21:06 +04:00
|
|
|
cimport spacy
|
2014-07-05 22:51:42 +04:00
|
|
|
|
|
|
|
|
2014-07-07 14:47:21 +04:00
|
|
|
cdef class English(spacy.Language):
|
2014-08-18 21:14:00 +04:00
|
|
|
cdef int find_split(self, unicode word):
|
|
|
|
cdef size_t length = len(word)
|
2014-07-07 14:47:21 +04:00
|
|
|
cdef int i = 0
|
2014-08-18 21:14:00 +04:00
|
|
|
if word.startswith("'s") or word.startswith("'S"):
|
|
|
|
return 2
|
2014-07-07 14:47:21 +04:00
|
|
|
# Contractions
|
2014-08-18 21:14:00 +04:00
|
|
|
if word.endswith("'s") and length >= 3:
|
2014-07-07 14:47:21 +04:00
|
|
|
return length - 2
|
|
|
|
# Leading punctuation
|
|
|
|
if is_punct(word, 0, length):
|
|
|
|
return 1
|
|
|
|
elif length >= 1:
|
|
|
|
# Split off all trailing punctuation characters
|
|
|
|
i = 0
|
|
|
|
while i < length and not is_punct(word, i, length):
|
|
|
|
i += 1
|
|
|
|
return i
|
2014-07-06 20:35:55 +04:00
|
|
|
|
2014-07-07 02:02:55 +04:00
|
|
|
|
2014-07-06 20:35:55 +04:00
|
|
|
cdef bint is_punct(unicode word, size_t i, size_t length):
|
2014-07-07 03:15:59 +04: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():
|
2014-07-08 01:26:01 +04:00
|
|
|
return i == 0
|
2014-07-23 20:35:18 +04:00
|
|
|
if word[i] == "-" and i < (length - 1) and word[i+1] == '-':
|
|
|
|
return False
|
2014-07-07 03:15:59 +04:00
|
|
|
# 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
|
2014-07-23 20:35:18 +04:00
|
|
|
# Don't count periods as punct if the next char is not whitespace
|
|
|
|
if word[i] == "." and i < (length - 1) and not word[i+1].isspace():
|
2014-07-07 03:15:59 +04:00
|
|
|
return False
|
2014-07-06 21:28:42 +04:00
|
|
|
return not word[i].isalnum()
|
2014-07-07 09:36:43 +04:00
|
|
|
|
|
|
|
|
2014-07-07 14:47:21 +04:00
|
|
|
EN = English('en')
|
|
|
|
|
|
|
|
|
|
|
|
cpdef Tokens tokenize(unicode string):
|
|
|
|
return EN.tokenize(string)
|
|
|
|
|
|
|
|
|
|
|
|
cpdef Lexeme_addr lookup(unicode string) except 0:
|
2014-08-18 21:14:00 +04:00
|
|
|
return <Lexeme_addr>EN.lookup(string)
|
2014-07-07 14:47:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
cpdef unicode unhash(StringHash hash_value):
|
|
|
|
return EN.unhash(hash_value)
|
2014-08-19 04:40:37 +04:00
|
|
|
|
|
|
|
|