2014-09-15 04:31:58 +00:00
|
|
|
# coding: utf-8
|
2014-07-07 02:23:46 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-12-21 09:38:27 +00:00
|
|
|
import pytest
|
2015-10-24 05:18:47 +00:00
|
|
|
import io
|
|
|
|
import pickle
|
|
|
|
import cloudpickle
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.models
|
|
|
|
def test_pickle(en_tokenizer):
|
|
|
|
file_ = io.BytesIO()
|
|
|
|
cloudpickle.dump(en_tokenizer, file_)
|
|
|
|
file_.seek(0)
|
|
|
|
loaded = pickle.load(file_)
|
|
|
|
assert loaded is not None
|
2014-07-07 02:23:46 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_no_word(en_tokenizer):
|
|
|
|
tokens = en_tokenizer(u'')
|
2015-02-07 02:01:44 +00:00
|
|
|
assert len(tokens) == 0
|
|
|
|
|
2015-04-19 19:39:18 +00:00
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_single_word(en_tokenizer):
|
|
|
|
tokens = en_tokenizer(u'hello')
|
2015-01-23 20:22:30 +00:00
|
|
|
assert tokens[0].orth_ == 'hello'
|
2014-07-07 02:23:46 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_two_words(en_tokenizer):
|
|
|
|
tokens = en_tokenizer('hello possums')
|
2014-10-23 13:59:17 +00:00
|
|
|
assert len(tokens) == 2
|
2015-01-23 20:22:30 +00:00
|
|
|
assert tokens[0].orth_ != tokens[1].orth_
|
2014-07-07 02:23:46 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_punct(en_tokenizer):
|
|
|
|
tokens = en_tokenizer('hello, possums.')
|
2014-09-15 04:31:58 +00:00
|
|
|
assert len(tokens) == 4
|
2015-01-23 20:22:30 +00:00
|
|
|
assert tokens[0].orth_ == 'hello'
|
|
|
|
assert tokens[1].orth_ == ','
|
|
|
|
assert tokens[2].orth_ == 'possums'
|
|
|
|
assert tokens[1].orth_ != 'hello'
|
2014-07-07 02:23:46 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_digits(en_tokenizer):
|
|
|
|
tokens = en_tokenizer('The year: 1984.')
|
2014-10-23 13:59:17 +00:00
|
|
|
assert len(tokens) == 5
|
2015-06-07 15:24:49 +00:00
|
|
|
assert tokens[0].orth == en_tokenizer.vocab['The'].orth
|
|
|
|
assert tokens[3].orth == en_tokenizer.vocab['1984'].orth
|
2014-07-07 02:23:46 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_contraction(en_tokenizer):
|
|
|
|
tokens = en_tokenizer("don't giggle")
|
2014-10-23 13:59:17 +00:00
|
|
|
assert len(tokens) == 3
|
2015-06-07 15:24:49 +00:00
|
|
|
assert tokens[1].orth == en_tokenizer.vocab["n't"].orth
|
|
|
|
tokens = en_tokenizer("i said don't!")
|
2014-10-23 13:59:17 +00:00
|
|
|
assert len(tokens) == 5
|
2015-06-07 15:24:49 +00:00
|
|
|
assert tokens[4].orth == en_tokenizer.vocab['!'].orth
|
2014-09-12 16:00:42 +00:00
|
|
|
|
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_contraction_punct(en_tokenizer):
|
|
|
|
tokens = en_tokenizer("(can't")
|
2014-09-12 16:00:42 +00:00
|
|
|
assert len(tokens) == 3
|
2015-06-07 15:24:49 +00:00
|
|
|
tokens = en_tokenizer("`ain't")
|
2014-09-12 16:00:42 +00:00
|
|
|
assert len(tokens) == 3
|
2015-06-07 15:24:49 +00:00
|
|
|
tokens = en_tokenizer('''"isn't''')
|
2014-09-12 16:00:42 +00:00
|
|
|
assert len(tokens) == 3
|
2015-06-07 15:24:49 +00:00
|
|
|
tokens = en_tokenizer("can't!")
|
2014-09-12 16:00:42 +00:00
|
|
|
assert len(tokens) == 3
|
|
|
|
|
2015-04-19 19:39:18 +00:00
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
def test_sample(en_tokenizer):
|
2014-09-14 23:32:51 +00:00
|
|
|
text = """Tributes pour in for late British Labour Party leader
|
|
|
|
|
2015-04-19 08:31:31 +00:00
|
|
|
Tributes poured in from around the world Thursday
|
|
|
|
to the late Labour Party leader John Smith, who died earlier from a massive
|
2014-09-14 23:32:51 +00:00
|
|
|
heart attack aged 55.
|
|
|
|
|
2015-04-19 08:31:31 +00:00
|
|
|
In Washington, the US State Department issued a statement regretting "the
|
2014-09-14 23:32:51 +00:00
|
|
|
untimely death" of the rapier-tongued Scottish barrister and parliamentarian.
|
|
|
|
|
|
|
|
"Mr. Smith, throughout his distinguished"""
|
2015-04-19 08:31:31 +00:00
|
|
|
|
2015-06-07 15:24:49 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2014-09-14 23:32:51 +00:00
|
|
|
assert len(tokens) > 5
|
2014-09-15 04:31:58 +00:00
|
|
|
|
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts1(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = u"""The U.S. Army likes Shock and Awe."""
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2014-09-15 04:31:58 +00:00
|
|
|
assert len(tokens) == 8
|
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts2(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = u"""U.N. regulations are not a part of their concern."""
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2014-09-15 04:31:58 +00:00
|
|
|
assert len(tokens) == 10
|
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts3(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = u"“Isn't it?”"
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2015-01-23 20:22:30 +00:00
|
|
|
words = [t.orth_ for t in tokens]
|
2014-12-09 03:48:01 +00:00
|
|
|
assert len(words) == 6
|
|
|
|
|
2014-09-15 04:31:58 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts4(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = u"""Yes! "I'd rather have a walk", Ms. Comble sighed. """
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2015-01-23 20:22:30 +00:00
|
|
|
words = [t.orth_ for t in tokens]
|
2014-12-09 03:48:01 +00:00
|
|
|
assert len(words) == 15
|
|
|
|
|
2014-09-15 04:31:58 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts5(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = """'Me too!', Mr. P. Delaware cried. """
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2014-09-15 04:31:58 +00:00
|
|
|
assert len(tokens) == 11
|
|
|
|
|
2015-06-08 14:17:07 +00:00
|
|
|
# TODO: This is currently difficult --- infix interferes here.
|
|
|
|
#def test_mr(en_tokenizer):
|
|
|
|
# text = """Today is Tuesday.Mr."""
|
|
|
|
# tokens = en_tokenizer(text)
|
|
|
|
# assert len(tokens) == 5
|
2015-10-24 05:18:47 +00:00
|
|
|
# assert [w.orth_ for w in tokens] == ['Today', 'is', 'Tuesday', '.', 'Mr.']
|
2015-06-07 19:29:17 +00:00
|
|
|
|
2014-12-09 03:48:01 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_cnts6(en_tokenizer):
|
2014-09-15 04:31:58 +00:00
|
|
|
text = u'They ran about 10km.'
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2015-01-23 20:22:30 +00:00
|
|
|
words = [t.orth_ for t in tokens]
|
2014-12-09 03:48:01 +00:00
|
|
|
assert len(words) == 6
|
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_bracket_period(en_tokenizer):
|
2015-01-22 11:25:18 +00:00
|
|
|
text = u'(And a 6a.m. run through Washington Park).'
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2015-01-23 20:22:30 +00:00
|
|
|
assert tokens[len(tokens) - 1].orth_ == u'.'
|
2015-01-22 11:25:18 +00:00
|
|
|
|
2015-02-18 11:37:04 +00:00
|
|
|
|
2015-06-07 19:29:17 +00:00
|
|
|
def test_ie(en_tokenizer):
|
2015-02-18 11:37:04 +00:00
|
|
|
text = u"It's mediocre i.e. bad."
|
2015-06-07 19:29:17 +00:00
|
|
|
tokens = en_tokenizer(text)
|
2015-02-18 11:37:04 +00:00
|
|
|
assert len(tokens) == 6
|
|
|
|
assert tokens[3].orth_ == "i.e."
|
|
|
|
|
2016-01-16 15:41:26 +00:00
|
|
|
|
|
|
|
def test_two_whitespace(en_tokenizer):
|
|
|
|
orig_str = u'there are 2 spaces after this '
|
|
|
|
tokens = en_tokenizer(orig_str)
|
|
|
|
assert repr(tokens.text_with_ws) == repr(orig_str)
|
|
|
|
|
2014-11-04 15:03:22 +00:00
|
|
|
#def test_cnts7():
|
|
|
|
# text = 'But then the 6,000-year ice age came...'
|
|
|
|
# tokens = EN.tokenize(text)
|
|
|
|
# assert len(tokens) == 10
|