spaCy/spacy/tests/vocab/test_intern.py

124 lines
3.2 KiB
Python
Raw Normal View History

2014-10-23 09:47:06 +00:00
# -*- coding: utf8 -*-
from __future__ import unicode_literals
import pickle
2014-10-23 09:47:06 +00:00
2014-12-21 09:44:21 +00:00
from spacy.strings import StringStore
2016-09-30 18:00:58 +00:00
from cymem.cymem import Pool
2014-10-23 09:47:06 +00:00
import pytest
2015-10-26 10:08:18 +00:00
import tempfile
2014-10-23 09:47:06 +00:00
import io
2015-04-19 19:39:18 +00:00
2014-10-23 09:47:06 +00:00
@pytest.fixture
def sstore():
return StringStore()
2015-04-19 19:39:18 +00:00
2014-10-23 09:47:06 +00:00
def test_save_bytes(sstore):
2016-09-30 18:00:58 +00:00
Hello_i = sstore.intern(b'Hello')
2014-10-23 09:47:06 +00:00
assert Hello_i == 1
2016-09-30 18:00:58 +00:00
assert sstore.intern(b'Hello') == 1
2014-10-23 09:47:06 +00:00
assert sstore[b'Hello'] == 1
2016-09-30 18:00:58 +00:00
assert sstore.intern(b'goodbye') != Hello_i
assert sstore.intern(b'hello') != Hello_i
2014-10-23 09:47:06 +00:00
assert Hello_i == 1
def test_save_unicode(sstore):
2016-09-30 18:00:58 +00:00
Hello_i = sstore.intern(u'Hello')
assert Hello_i == 1
2016-09-30 18:00:58 +00:00
assert sstore.intern(u'Hello') == 1
assert sstore[u'Hello'] == 1
2016-09-30 18:00:58 +00:00
assert sstore.intern(u'goodbye') != Hello_i
assert sstore.intern(u'hello') != Hello_i
assert Hello_i == 1
2014-10-23 09:47:06 +00:00
def test_retrieve_id(sstore):
2016-09-30 18:00:58 +00:00
A_i = sstore.intern(b'A')
2014-10-23 09:47:06 +00:00
assert sstore.size == 1
2015-01-25 08:04:23 +00:00
assert sstore[1] == 'A'
2014-10-23 09:47:06 +00:00
with pytest.raises(IndexError):
sstore[2]
def test_med_string(sstore):
2016-09-30 18:00:58 +00:00
nine_char_string = sstore.intern(b'0123456789')
2015-07-24 01:47:59 +00:00
assert sstore[nine_char_string] == u'0123456789'
2016-09-30 18:00:58 +00:00
dummy = sstore.intern(b'A')
assert sstore[b'0123456789'] == nine_char_string
def test_long_string(sstore):
url = u'INFORMATIVE](http://www.google.com/search?as_q=RedditMonkey&hl=en&num=50&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_nlo=&as_nhi=&as_occt=any&as_dt=i&as_sitesearch=&as_rights=&safe=off'
2016-09-30 18:00:58 +00:00
orth = sstore.intern(url)
assert sstore[orth] == url
def test_254_string(sstore):
s254 = 'a' * 254
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s254)
assert sstore[orth] == s254
def test_255_string(sstore):
s255 = 'b' * 255
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s255)
assert sstore[orth] == s255
def test_256_string(sstore):
s256 = 'c' * 256
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s256)
assert sstore[orth] == s256
def test_massive_strings(sstore):
s511 = 'd' * 511
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s511)
assert sstore[orth] == s511
s512 = 'e' * 512
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s512)
assert sstore[orth] == s512
s513 = '1' * 513
2016-09-30 18:00:58 +00:00
orth = sstore.intern(s513)
assert sstore[orth] == s513
2016-09-30 18:00:58 +00:00
def test_intern_oov(sstore):
mem1 = Pool()
mem2 = Pool()
one = sstore.intern(u'Pool1', mem=mem1)
two = sstore.intern(u'Pool2', mem=mem2)
assert sstore.decode_int(one, mem=mem1) == 'Pool1'
assert sstore.decode_int(two, mem=mem2) == 'Pool2'
with pytest.raises(IndexError):
sstore.decode_int(one, mem=mem2)
sstore.decode_int(two, mem=mem1)
sstore.remove_oov_map(mem1)
with pytest.raises(IndexError):
sstore.decode_int(one, mem=mem1)
def test_pickle_string_store(sstore):
2016-09-30 18:00:58 +00:00
hello_id = sstore.intern(u'Hi')
2015-10-13 09:50:07 +00:00
string_file = io.BytesIO()
pickle.dump(sstore, string_file)
string_file.seek(0)
loaded = pickle.load(string_file)
assert loaded[hello_id] == u'Hi'
def test_dump_load(sstore):
2016-09-30 18:00:58 +00:00
id_ = sstore.intern(u'qqqqq')
2015-10-26 11:40:48 +00:00
with tempfile.TemporaryFile('w+t') as file_:
sstore.dump(file_)
2015-10-26 10:08:18 +00:00
file_.seek(0)
new_store = StringStore()
new_store.load(file_)
assert new_store[id_] == u'qqqqq'