spaCy/tests/vocab/test_intern.py

96 lines
2.2 KiB
Python
Raw Normal View History

2014-10-23 13:47:06 +04:00
# -*- coding: utf8 -*-
from __future__ import unicode_literals
import pickle
2015-10-13 12:50:07 +03:00
import io
2014-10-23 13:47:06 +04:00
2014-12-21 12:44:21 +03:00
from spacy.strings import StringStore
2014-10-23 13:47:06 +04:00
import pytest
2015-04-19 22:39:18 +03:00
2014-10-23 13:47:06 +04:00
@pytest.fixture
def sstore():
return StringStore()
2015-04-19 22:39:18 +03:00
2014-10-23 13:47:06 +04:00
def test_save_bytes(sstore):
Hello_i = sstore[b'Hello']
assert Hello_i == 1
assert sstore[b'Hello'] == 1
assert sstore[b'goodbye'] != Hello_i
assert sstore[b'hello'] != Hello_i
assert Hello_i == 1
def test_save_unicode(sstore):
Hello_i = sstore[u'Hello']
assert Hello_i == 1
assert sstore[u'Hello'] == 1
assert sstore[u'goodbye'] != Hello_i
assert sstore[u'hello'] != Hello_i
assert Hello_i == 1
2014-10-23 13:47:06 +04:00
def test_retrieve_id(sstore):
A_i = sstore[b'A']
assert sstore.size == 1
2015-01-25 11:04:23 +03:00
assert sstore[1] == 'A'
2014-10-23 13:47:06 +04:00
with pytest.raises(IndexError):
sstore[2]
def test_med_string(sstore):
nine_char_string = sstore[b'0123456789']
2015-07-24 04:47:59 +03:00
assert sstore[nine_char_string] == u'0123456789'
dummy = sstore[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'
orth = sstore[url]
assert sstore[orth] == url
def test_254_string(sstore):
s254 = 'a' * 254
orth = sstore[s254]
assert sstore[orth] == s254
def test_255_string(sstore):
s255 = 'b' * 255
orth = sstore[s255]
assert sstore[orth] == s255
def test_256_string(sstore):
s256 = 'c' * 256
orth = sstore[s256]
assert sstore[orth] == s256
def test_massive_strings(sstore):
s511 = 'd' * 511
orth = sstore[s511]
assert sstore[orth] == s511
s512 = 'e' * 512
orth = sstore[s512]
assert sstore[orth] == s512
s513 = '1' * 513
orth = sstore[s513]
assert sstore[orth] == s513
def test_pickle_string_store(sstore):
hello_id = sstore[u'Hi']
2015-10-13 12:50:07 +03: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'