2014-07-07 05:36:43 +00:00
|
|
|
# cython: profile=True
|
2014-08-20 11:39:39 +00:00
|
|
|
# cython: embedsignature=True
|
2014-08-21 16:42:47 +00:00
|
|
|
'''Tokenize English text, using a scheme that differs from the Penn Treebank 3
|
|
|
|
scheme in several important respects:
|
|
|
|
|
2014-08-22 14:35:48 +00:00
|
|
|
* Whitespace is added as tokens, except for single spaces. e.g.,
|
2014-08-21 16:42:47 +00:00
|
|
|
|
2014-08-28 23:59:23 +00:00
|
|
|
>>> [w.string for w in EN.tokenize(u'\\nHello \\tThere')]
|
2014-08-21 16:42:47 +00:00
|
|
|
[u'\\n', u'Hello', u' ', u'\\t', u'There']
|
|
|
|
|
|
|
|
* Contractions are normalized, e.g.
|
|
|
|
|
2014-08-28 23:59:23 +00:00
|
|
|
>>> [w.string for w in EN.tokenize(u"isn't ain't won't he's")]
|
2014-08-21 16:42:47 +00:00
|
|
|
[u'is', u'not', u'are', u'not', u'will', u'not', u'he', u"__s"]
|
|
|
|
|
|
|
|
* Hyphenated words are split, with the hyphen preserved, e.g.:
|
|
|
|
|
2014-08-28 23:59:23 +00:00
|
|
|
>>> [w.string for w in EN.tokenize(u'New York-based')]
|
2014-08-21 16:42:47 +00:00
|
|
|
[u'New', u'York', u'-', u'based']
|
|
|
|
|
2014-08-22 14:35:48 +00:00
|
|
|
Other improvements:
|
|
|
|
|
2014-08-21 16:42:47 +00:00
|
|
|
* Email addresses, URLs, European-formatted dates and other numeric entities not
|
|
|
|
found in the PTB are tokenized correctly
|
|
|
|
* Heuristic handling of word-final periods (PTB expects sentence boundary detection
|
|
|
|
as a pre-process before tokenization.)
|
|
|
|
|
2014-08-22 14:35:48 +00:00
|
|
|
Take care to ensure your training and run-time data is tokenized according to the
|
2014-08-21 16:42:47 +00:00
|
|
|
same scheme. Tokenization problems are a major cause of poor performance for
|
2014-08-21 21:49:14 +00:00
|
|
|
NLP tools. If you're using a pre-trained model, the :py:mod:`spacy.ptb3` module
|
|
|
|
provides a fully Penn Treebank 3-compliant tokenizer.
|
2014-07-05 18:51:42 +00:00
|
|
|
'''
|
2014-08-27 15:15:39 +00:00
|
|
|
# TODO
|
2014-08-21 16:42:47 +00:00
|
|
|
#The script translate_treebank_tokenization can be used to transform a treebank's
|
|
|
|
#annotation to use one of the spacy tokenization schemes.
|
|
|
|
|
|
|
|
|
2014-07-05 18:51:42 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from libc.stdlib cimport malloc, calloc, free
|
|
|
|
from libc.stdint cimport uint64_t
|
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
cimport lang
|
2014-09-11 14:57:08 +00:00
|
|
|
from spacy.lexeme cimport lexeme_check_flag
|
|
|
|
from spacy.lexeme cimport lexeme_string_view
|
2014-08-27 15:15:39 +00:00
|
|
|
|
2014-08-30 17:01:15 +00:00
|
|
|
from spacy import util
|
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
from spacy import orth
|
2014-07-07 10:47:21 +00:00
|
|
|
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cdef enum Flags:
|
|
|
|
Flag_IsAlpha
|
|
|
|
Flag_IsAscii
|
|
|
|
Flag_IsDigit
|
|
|
|
Flag_IsLower
|
|
|
|
Flag_IsPunct
|
|
|
|
Flag_IsSpace
|
|
|
|
Flag_IsTitle
|
|
|
|
Flag_IsUpper
|
|
|
|
|
|
|
|
Flag_CanAdj
|
|
|
|
Flag_CanAdp
|
|
|
|
Flag_CanAdv
|
|
|
|
Flag_CanConj
|
|
|
|
Flag_CanDet
|
|
|
|
Flag_CanNoun
|
|
|
|
Flag_CanNum
|
|
|
|
Flag_CanPdt
|
|
|
|
Flag_CanPos
|
|
|
|
Flag_CanPron
|
|
|
|
Flag_CanPrt
|
|
|
|
Flag_CanPunct
|
|
|
|
Flag_CanVerb
|
|
|
|
|
|
|
|
Flag_OftLower
|
|
|
|
Flag_OftTitle
|
|
|
|
Flag_OftUpper
|
|
|
|
Flag_N
|
|
|
|
|
|
|
|
|
|
|
|
cdef enum Views:
|
|
|
|
View_CanonForm
|
|
|
|
View_WordShape
|
|
|
|
View_NonSparse
|
|
|
|
View_Asciied
|
|
|
|
View_N
|
|
|
|
|
|
|
|
|
|
|
|
# Assign the flag and view functions by enum value.
|
|
|
|
# This is verbose, but it ensures we don't get nasty order sensitivities.
|
|
|
|
STRING_VIEW_FUNCS = [None] * View_N
|
|
|
|
STRING_VIEW_FUNCS[View_CanonForm] = orth.canon_case
|
|
|
|
STRING_VIEW_FUNCS[View_WordShape] = orth.word_shape
|
|
|
|
STRING_VIEW_FUNCS[View_NonSparse] = orth.non_sparse
|
|
|
|
STRING_VIEW_FUNCS[View_Asciied] = orth.asciied
|
|
|
|
|
|
|
|
FLAG_FUNCS = [None] * Flag_N
|
|
|
|
FLAG_FUNCS[Flag_IsAlpha] = orth.is_alpha
|
|
|
|
FLAG_FUNCS[Flag_IsAscii] = orth.is_ascii
|
|
|
|
FLAG_FUNCS[Flag_IsDigit] = orth.is_digit
|
|
|
|
FLAG_FUNCS[Flag_IsLower] = orth.is_lower
|
|
|
|
FLAG_FUNCS[Flag_IsPunct] = orth.is_punct
|
|
|
|
FLAG_FUNCS[Flag_IsSpace] = orth.is_space
|
|
|
|
FLAG_FUNCS[Flag_IsTitle] = orth.is_title
|
|
|
|
FLAG_FUNCS[Flag_IsUpper] = orth.is_upper
|
|
|
|
|
|
|
|
FLAG_FUNCS[Flag_CanAdj] = orth.can_tag('ADJ')
|
|
|
|
FLAG_FUNCS[Flag_CanAdp] = orth.can_tag('ADP')
|
|
|
|
FLAG_FUNCS[Flag_CanAdv] = orth.can_tag('ADV')
|
|
|
|
FLAG_FUNCS[Flag_CanConj] = orth.can_tag('CONJ')
|
|
|
|
FLAG_FUNCS[Flag_CanDet] = orth.can_tag('DET')
|
|
|
|
FLAG_FUNCS[Flag_CanNoun] = orth.can_tag('NOUN')
|
|
|
|
FLAG_FUNCS[Flag_CanNum] = orth.can_tag('NUM')
|
|
|
|
FLAG_FUNCS[Flag_CanPdt] = orth.can_tag('PDT')
|
|
|
|
FLAG_FUNCS[Flag_CanPos] = orth.can_tag('POS')
|
|
|
|
FLAG_FUNCS[Flag_CanPron] = orth.can_tag('PRON')
|
|
|
|
FLAG_FUNCS[Flag_CanPrt] = orth.can_tag('PRT')
|
|
|
|
FLAG_FUNCS[Flag_CanPunct] = orth.can_tag('PUNCT')
|
|
|
|
FLAG_FUNCS[Flag_CanVerb] = orth.can_tag('VERB')
|
|
|
|
|
|
|
|
FLAG_FUNCS[Flag_OftLower] = orth.oft_case('lower', 0.7)
|
|
|
|
FLAG_FUNCS[Flag_OftTitle] = orth.oft_case('title', 0.7)
|
|
|
|
FLAG_FUNCS[Flag_OftUpper] = orth.oft_case('upper', 0.7)
|
|
|
|
|
|
|
|
|
|
|
|
cdef class EnglishTokens(Tokens):
|
|
|
|
# Provide accessor methods for the features supported by the language.
|
|
|
|
# Without these, clients have to use the underlying string_view and check_flag
|
|
|
|
# methods, which requires them to know the IDs.
|
|
|
|
cpdef unicode canon_string(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_string_view(self.lexemes[i], View_CanonForm)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef unicode shape_string(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_string_view(self.lexemes[i], View_WordShape)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef unicode non_sparse_string(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_string_view(self.lexemes[i], View_NonSparse)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef unicode asciied(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], View_Asciied)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_alpha(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsAlpha)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_ascii(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsAscii)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_digit(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsDigit)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_lower(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsLower)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_punct(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsPunct)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_space(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsSpace)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_title(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsTitle)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint is_upper(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_IsUpper)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_adj(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanAdj)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_adp(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanAdp)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_adv(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanAdv)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_conj(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanConj)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_det(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanDet)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_noun(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanNoun)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_num(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanNum)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_pdt(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanPdt)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_pos(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanPos)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_pron(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanPron)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_prt(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanPrt)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_punct(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanPunct)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint can_verb(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_CanVerb)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint oft_lower(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_OftLower)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint oft_title(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_OftTitle)
|
2014-09-10 16:11:13 +00:00
|
|
|
|
|
|
|
cpdef bint oft_upper(self, size_t i):
|
2014-09-11 14:57:08 +00:00
|
|
|
return lexeme_check_flag(self.lexemes[i], Flag_OftUpper)
|
2014-08-30 17:01:15 +00:00
|
|
|
|
2014-07-05 18:51:42 +00:00
|
|
|
|
2014-08-27 15:15:39 +00:00
|
|
|
cdef class English(Language):
|
2014-08-28 23:59:23 +00:00
|
|
|
"""English tokenizer, tightly coupled to lexicon.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name (unicode): The two letter code used by Wikipedia for the language.
|
|
|
|
lexicon (Lexicon): The lexicon. Exposes the lookup method.
|
|
|
|
"""
|
2014-09-10 16:27:44 +00:00
|
|
|
fl_is_alpha = Flag_IsAlpha
|
|
|
|
fl_is_digit = Flag_IsDigit
|
|
|
|
v_shape = View_WordShape
|
2014-09-10 16:11:13 +00:00
|
|
|
def __cinit__(self, name, user_string_features, user_flag_features):
|
2014-09-12 00:23:51 +00:00
|
|
|
self.cache.set_empty_key(0)
|
2014-08-30 17:01:15 +00:00
|
|
|
lang_data = util.read_lang_data(name)
|
|
|
|
rules, words, probs, clusters, case_stats, tag_stats = lang_data
|
|
|
|
self.lexicon = lang.Lexicon(words, probs, clusters, case_stats, tag_stats,
|
2014-09-10 16:11:13 +00:00
|
|
|
STRING_VIEW_FUNCS + user_string_features,
|
|
|
|
FLAG_FUNCS + user_flag_features)
|
2014-08-30 17:01:15 +00:00
|
|
|
self._load_special_tokenization(rules)
|
2014-09-10 16:27:44 +00:00
|
|
|
self.tokens_class = EnglishTokens
|
2014-08-28 17:45:09 +00:00
|
|
|
|
2014-09-12 02:29:09 +00:00
|
|
|
cdef int _split_one(self, Py_UNICODE* characters, size_t length):
|
2014-09-12 03:10:59 +00:00
|
|
|
if length == 1:
|
|
|
|
return 1
|
|
|
|
if characters[0] == "'" and (characters[1] == "s" or characters[1] == "S"):
|
2014-08-18 17:14:00 +00:00
|
|
|
return 2
|
2014-09-12 03:10:59 +00:00
|
|
|
cdef int i = 0
|
2014-07-07 10:47:21 +00:00
|
|
|
# Leading punctuation
|
2014-09-12 03:10:59 +00:00
|
|
|
if _check_punct(characters, 0, length):
|
2014-07-07 10:47:21 +00:00
|
|
|
return 1
|
2014-09-12 03:10:59 +00:00
|
|
|
# Contractions
|
|
|
|
elif length >= 3 and characters[length - 2] == "'":
|
|
|
|
c2 = characters[length-1]
|
|
|
|
if c2 == "s" or c2 == "S":
|
|
|
|
return length - 2
|
|
|
|
if length >= 1:
|
2014-07-07 10:47:21 +00:00
|
|
|
# Split off all trailing punctuation characters
|
|
|
|
i = 0
|
2014-09-12 03:10:59 +00:00
|
|
|
while i < length and not _check_punct(characters, i, length):
|
2014-07-07 10:47:21 +00:00
|
|
|
i += 1
|
|
|
|
return i
|
2014-07-06 16:35:55 +00:00
|
|
|
|
2014-09-10 16:11:13 +00:00
|
|
|
|
2014-09-12 03:10:59 +00:00
|
|
|
cdef bint _check_punct(Py_UNICODE* characters, size_t i, size_t length):
|
|
|
|
cdef unicode char_i = characters[i]
|
|
|
|
cdef unicode char_i1 = characters[i+1]
|
2014-07-06 23:15:59 +00:00
|
|
|
# Don't count appostrophes as punct if the next char is a letter
|
2014-09-12 03:10:59 +00:00
|
|
|
if characters[i] == "'" and i < (length - 1) and char_i1.isalpha():
|
2014-07-07 21:26:01 +00:00
|
|
|
return i == 0
|
2014-09-12 03:10:59 +00:00
|
|
|
if characters[i] == "-" and i < (length - 1) and characters[i+1] == '-':
|
2014-07-23 16:35:18 +00:00
|
|
|
return False
|
2014-07-06 23:15:59 +00:00
|
|
|
# Don't count commas as punct if the next char is a number
|
2014-09-12 03:10:59 +00:00
|
|
|
if characters[i] == "," and i < (length - 1) and char_i1.isdigit():
|
2014-07-06 23:15:59 +00:00
|
|
|
return False
|
2014-07-23 16:35:18 +00:00
|
|
|
# Don't count periods as punct if the next char is not whitespace
|
2014-09-12 03:10:59 +00:00
|
|
|
if characters[i] == "." and i < (length - 1) and not char_i1.isspace():
|
2014-07-06 23:15:59 +00:00
|
|
|
return False
|
2014-09-12 03:10:59 +00:00
|
|
|
return not char_i.isalnum()
|
2014-07-07 05:36:43 +00:00
|
|
|
|
|
|
|
|
2014-08-30 17:01:15 +00:00
|
|
|
EN = English('en', [], [])
|