2014-09-15 08:31:58 +04:00
|
|
|
# coding: utf-8
|
2014-07-07 06:23:46 +04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-08-28 21:45:09 +04:00
|
|
|
from spacy.en import EN
|
2014-07-07 06:23:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_single_word():
|
2014-08-28 21:45:09 +04:00
|
|
|
lex_ids = EN.tokenize(u'hello')
|
2014-09-10 22:58:30 +04:00
|
|
|
assert lex_ids[0].string == EN.lexicon.lookup(u'hello').string
|
2014-07-07 06:23:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_two_words():
|
2014-08-28 21:45:09 +04:00
|
|
|
words = EN.tokenize('hello possums')
|
2014-08-23 21:55:06 +04:00
|
|
|
assert len(words) == 2
|
2014-09-10 22:58:30 +04:00
|
|
|
assert words[0].string == EN.lexicon.lookup('hello').string
|
|
|
|
assert words[0].string != words[1].string
|
2014-07-07 06:23:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_punct():
|
2014-08-28 21:45:09 +04:00
|
|
|
tokens = EN.tokenize('hello, possums.')
|
2014-09-15 08:31:58 +04:00
|
|
|
assert len(tokens) == 4
|
2014-08-28 21:45:09 +04:00
|
|
|
assert tokens[0].string == EN.lexicon.lookup('hello').string
|
|
|
|
assert tokens[1].string == EN.lexicon.lookup(',').string
|
2014-09-15 08:31:58 +04:00
|
|
|
assert tokens[2].string == EN.lexicon.lookup('possums').string
|
2014-08-28 21:45:09 +04:00
|
|
|
assert tokens[1].string != EN.lexicon.lookup('hello').string
|
2014-07-07 06:23:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_digits():
|
2014-08-28 21:45:09 +04:00
|
|
|
lex_ids = EN.tokenize('The year: 1984.')
|
2014-10-14 08:47:06 +04:00
|
|
|
assert lex_ids.orig(3) == "1984"
|
2014-09-15 08:31:58 +04:00
|
|
|
assert len(lex_ids) == 5
|
2014-08-28 21:45:09 +04:00
|
|
|
assert lex_ids[0].string == EN.lexicon.lookup('The').string
|
2014-09-15 08:31:58 +04:00
|
|
|
assert lex_ids[3].string == EN.lexicon.lookup('1984').string
|
2014-07-07 06:23:46 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_contraction():
|
2014-08-28 21:45:09 +04:00
|
|
|
lex_ids = EN.tokenize("don't giggle")
|
2014-07-07 06:23:46 +04:00
|
|
|
assert len(lex_ids) == 3
|
2014-08-28 21:45:09 +04:00
|
|
|
assert lex_ids[1].string == EN.lexicon.lookup("not").string
|
|
|
|
lex_ids = EN.tokenize("i said don't!")
|
2014-09-12 20:00:42 +04:00
|
|
|
assert len(lex_ids) == 5
|
|
|
|
assert lex_ids[4].string == EN.lexicon.lookup('!').string
|
|
|
|
|
|
|
|
|
|
|
|
def test_contraction_punct():
|
|
|
|
tokens = EN.tokenize("(can't")
|
|
|
|
assert len(tokens) == 3
|
|
|
|
tokens = EN.tokenize("`ain't")
|
|
|
|
assert len(tokens) == 3
|
|
|
|
tokens = EN.tokenize('''"isn't''')
|
|
|
|
assert len(tokens) == 3
|
|
|
|
tokens = EN.tokenize("can't!")
|
|
|
|
assert len(tokens) == 3
|
|
|
|
|
2014-09-15 03:32:51 +04:00
|
|
|
def test_sample():
|
|
|
|
text = """Tributes pour in for late British Labour Party leader
|
|
|
|
|
|
|
|
Tributes poured in from around the world Thursday
|
|
|
|
to the late Labour Party leader John Smith, who died earlier from a massive
|
|
|
|
heart attack aged 55.
|
|
|
|
|
|
|
|
In Washington, the US State Department issued a statement regretting "the
|
|
|
|
untimely death" of the rapier-tongued Scottish barrister and parliamentarian.
|
|
|
|
|
|
|
|
"Mr. Smith, throughout his distinguished"""
|
|
|
|
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) > 5
|
2014-09-15 08:31:58 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_cnts1():
|
|
|
|
text = u"""The U.S. Army likes Shock and Awe."""
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 8
|
|
|
|
|
|
|
|
def test_cnts2():
|
|
|
|
text = u"""U.N. regulations are not a part of their concern."""
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 10
|
|
|
|
|
|
|
|
def test_cnts3():
|
|
|
|
text = u"“Isn't it?”"
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 6
|
|
|
|
|
|
|
|
def test_cnts4():
|
|
|
|
text = u"""Yes! "I'd rather have a walk", Ms. Comble sighed. """
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 15
|
|
|
|
|
|
|
|
def test_cnts5():
|
|
|
|
text = """'Me too!', Mr. P. Delaware cried. """
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 11
|
|
|
|
|
|
|
|
def test_cnts6():
|
|
|
|
text = u'They ran about 10km.'
|
|
|
|
tokens = EN.tokenize(text)
|
|
|
|
assert len(tokens) == 6
|
|
|
|
|
|
|
|
def test_cnts7():
|
|
|
|
text = 'But then the 6,000-year ice age came...'
|
|
|
|
tokens = EN.tokenize(text)
|
2014-10-14 08:47:06 +04:00
|
|
|
assert len(tokens) == 10
|