2014-07-07 09:36:43 +04:00
|
|
|
# cython: profile=True
|
2014-08-20 15:39:39 +04:00
|
|
|
# cython: embedsignature=True
|
2014-08-21 20:42:47 +04:00
|
|
|
'''Tokenize English text, using a scheme that differs from the Penn Treebank 3
|
|
|
|
scheme in several important respects:
|
|
|
|
|
2014-08-22 18:35:48 +04:00
|
|
|
* Whitespace is added as tokens, except for single spaces. e.g.,
|
2014-08-21 20:42:47 +04:00
|
|
|
|
2014-08-29 03:59:23 +04:00
|
|
|
>>> [w.string for w in EN.tokenize(u'\\nHello \\tThere')]
|
2014-08-21 20:42:47 +04:00
|
|
|
[u'\\n', u'Hello', u' ', u'\\t', u'There']
|
|
|
|
|
|
|
|
* Contractions are normalized, e.g.
|
|
|
|
|
2014-08-29 03:59:23 +04:00
|
|
|
>>> [w.string for w in EN.tokenize(u"isn't ain't won't he's")]
|
2014-08-21 20:42:47 +04: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-29 03:59:23 +04:00
|
|
|
>>> [w.string for w in EN.tokenize(u'New York-based')]
|
2014-08-21 20:42:47 +04:00
|
|
|
[u'New', u'York', u'-', u'based']
|
|
|
|
|
2014-08-22 18:35:48 +04:00
|
|
|
Other improvements:
|
|
|
|
|
2014-08-21 20:42:47 +04: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 18:35:48 +04:00
|
|
|
Take care to ensure your training and run-time data is tokenized according to the
|
2014-08-21 20:42:47 +04:00
|
|
|
same scheme. Tokenization problems are a major cause of poor performance for
|
2014-08-22 01:49:14 +04: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 22:51:42 +04:00
|
|
|
'''
|
2014-08-27 19:15:39 +04:00
|
|
|
# TODO
|
2014-08-21 20:42:47 +04: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 22:51:42 +04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from libc.stdlib cimport malloc, calloc, free
|
|
|
|
from libc.stdint cimport uint64_t
|
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
cimport lang
|
2014-09-11 18:57:08 +04:00
|
|
|
from spacy.lexeme cimport lexeme_check_flag
|
|
|
|
from spacy.lexeme cimport lexeme_string_view
|
2014-09-13 05:14:43 +04:00
|
|
|
from spacy._hashing cimport PointerHash
|
2014-08-27 19:15:39 +04:00
|
|
|
|
2014-08-30 21:01:15 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
from spacy import orth
|
2014-07-07 14:47:21 +04:00
|
|
|
|
2014-09-10 20:11:13 +04:00
|
|
|
|
2014-08-27 19:15:39 +04:00
|
|
|
cdef class English(Language):
|
2014-08-29 03:59:23 +04: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-12 06:29:09 +04:00
|
|
|
cdef int _split_one(self, Py_UNICODE* characters, size_t length):
|
2014-09-12 07:10:59 +04:00
|
|
|
if length == 1:
|
|
|
|
return 1
|
|
|
|
if characters[0] == "'" and (characters[1] == "s" or characters[1] == "S"):
|
2014-08-18 21:14:00 +04:00
|
|
|
return 2
|
2014-09-12 07:10:59 +04:00
|
|
|
cdef int i = 0
|
2014-07-07 14:47:21 +04:00
|
|
|
# Leading punctuation
|
2014-09-12 07:10:59 +04:00
|
|
|
if _check_punct(characters, 0, length):
|
2014-07-07 14:47:21 +04:00
|
|
|
return 1
|
2014-09-12 07:10:59 +04: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 14:47:21 +04:00
|
|
|
# Split off all trailing punctuation characters
|
|
|
|
i = 0
|
2014-09-12 07:10:59 +04:00
|
|
|
while i < length and not _check_punct(characters, i, length):
|
2014-07-07 14:47:21 +04:00
|
|
|
i += 1
|
|
|
|
return i
|
2014-07-06 20:35:55 +04:00
|
|
|
|
2014-09-10 20:11:13 +04:00
|
|
|
|
2014-09-12 07:10:59 +04: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-07 03:15:59 +04:00
|
|
|
# Don't count appostrophes as punct if the next char is a letter
|
2014-09-12 07:10:59 +04:00
|
|
|
if characters[i] == "'" and i < (length - 1) and char_i1.isalpha():
|
2014-07-08 01:26:01 +04:00
|
|
|
return i == 0
|
2014-09-15 03:31:44 +04:00
|
|
|
if characters[i] == "-":
|
2014-07-23 20:35:18 +04:00
|
|
|
return False
|
2014-09-15 03:31:44 +04:00
|
|
|
#and i < (length - 1) and characters[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
|
2014-09-12 07:10:59 +04:00
|
|
|
if characters[i] == "," and i < (length - 1) and char_i1.isdigit():
|
2014-07-07 03:15:59 +04:00
|
|
|
return False
|
2014-07-23 20:35:18 +04:00
|
|
|
# Don't count periods as punct if the next char is not whitespace
|
2014-09-15 03:31:44 +04:00
|
|
|
if characters[i] == ".":
|
2014-07-07 03:15:59 +04:00
|
|
|
return False
|
2014-09-12 07:10:59 +04:00
|
|
|
return not char_i.isalnum()
|
2014-07-07 09:36:43 +04:00
|
|
|
|
|
|
|
|
2014-08-30 21:01:15 +04:00
|
|
|
EN = English('en', [], [])
|