2017-01-12 17:07:31 +03:00
|
|
|
import pytest
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.strings import StringStore
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def stringstore():
|
|
|
|
return StringStore()
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
2017-09-16 12:21:35 +03:00
|
|
|
def test_string_hash(stringstore):
|
2018-07-25 00:38:44 +03:00
|
|
|
"""Test that string hashing is stable across platforms"""
|
2018-11-27 03:09:36 +03:00
|
|
|
assert stringstore.add("apple") == 8566208034543834098
|
|
|
|
heart = "\U0001f499"
|
2018-07-25 00:38:44 +03:00
|
|
|
h = stringstore.add(heart)
|
2017-09-16 17:22:38 +03:00
|
|
|
assert h == 11841826740069053588
|
2018-07-25 00:38:44 +03:00
|
|
|
|
2017-09-16 12:21:35 +03:00
|
|
|
|
2017-05-29 02:05:53 +03:00
|
|
|
def test_stringstore_from_api_docs(stringstore):
|
2018-11-27 03:09:36 +03:00
|
|
|
apple_hash = stringstore.add("apple")
|
2017-05-29 02:05:53 +03:00
|
|
|
assert apple_hash == 8566208034543834098
|
2018-11-27 03:09:36 +03:00
|
|
|
assert stringstore[apple_hash] == "apple"
|
|
|
|
assert "apple" in stringstore
|
|
|
|
assert "cherry" not in stringstore
|
2018-11-30 19:43:08 +03:00
|
|
|
stringstore.add("orange")
|
2017-05-29 02:05:53 +03:00
|
|
|
all_strings = [s for s in stringstore]
|
2018-11-27 03:09:36 +03:00
|
|
|
assert all_strings == ["apple", "orange"]
|
2022-10-06 11:51:06 +03:00
|
|
|
assert all_strings == list(stringstore.keys())
|
|
|
|
all_strings_and_hashes = list(stringstore.items())
|
|
|
|
assert all_strings_and_hashes == [
|
|
|
|
("apple", 8566208034543834098),
|
|
|
|
("orange", 2208928596161743350),
|
|
|
|
]
|
|
|
|
all_hashes = list(stringstore.values())
|
|
|
|
assert all_hashes == [8566208034543834098, 2208928596161743350]
|
2018-11-27 03:09:36 +03:00
|
|
|
banana_hash = stringstore.add("banana")
|
2017-05-29 02:05:53 +03:00
|
|
|
assert len(stringstore) == 3
|
|
|
|
assert banana_hash == 2525716904149915114
|
2018-11-27 03:09:36 +03:00
|
|
|
assert stringstore[banana_hash] == "banana"
|
|
|
|
assert stringstore["banana"] == banana_hash
|
2017-05-29 02:05:53 +03:00
|
|
|
|
|
|
|
|
2022-10-06 11:51:06 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"val_bytes,val_float,val_list,val_text,val_hash",
|
|
|
|
[(b"Hello", 1.1, ["abc"], "apple", 8566208034543834098)],
|
|
|
|
)
|
|
|
|
def test_stringstore_type_checking(
|
|
|
|
stringstore, val_bytes, val_float, val_list, val_text, val_hash
|
|
|
|
):
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert stringstore[val_bytes]
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
stringstore.add(val_float)
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert val_list not in stringstore
|
|
|
|
|
|
|
|
key = stringstore.add(val_text)
|
|
|
|
assert val_hash == key
|
|
|
|
assert stringstore[val_hash] == val_text
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("text1,text2,text3", [("Hello", "goodbye", "hello")])
|
2017-01-12 17:07:31 +03:00
|
|
|
def test_stringstore_save_unicode(stringstore, text1, text2, text3):
|
2017-05-28 13:36:27 +03:00
|
|
|
key = stringstore.add(text1)
|
|
|
|
assert stringstore[text1] == key
|
|
|
|
assert stringstore[text2] != key
|
|
|
|
assert stringstore[text3] != key
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
2022-10-06 11:51:06 +03:00
|
|
|
@pytest.mark.parametrize("text", ["A"])
|
2017-01-12 17:07:31 +03:00
|
|
|
def test_stringstore_retrieve_id(stringstore, text):
|
2017-05-28 13:36:27 +03:00
|
|
|
key = stringstore.add(text)
|
|
|
|
assert len(stringstore) == 1
|
2022-10-06 11:51:06 +03:00
|
|
|
assert stringstore[key] == text
|
2017-05-28 13:36:27 +03:00
|
|
|
with pytest.raises(KeyError):
|
2017-05-28 15:08:09 +03:00
|
|
|
stringstore[20000]
|
2017-01-12 17:07:31 +03:00
|
|
|
|
|
|
|
|
2022-10-06 11:51:06 +03:00
|
|
|
@pytest.mark.parametrize("text1,text2", [("0123456789", "A")])
|
2017-01-12 17:07:31 +03:00
|
|
|
def test_stringstore_med_string(stringstore, text1, text2):
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text1)
|
2022-10-06 11:51:06 +03:00
|
|
|
assert stringstore[store] == text1
|
2018-11-30 19:43:08 +03:00
|
|
|
stringstore.add(text2)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[text1] == store
|
|
|
|
|
|
|
|
|
|
|
|
def test_stringstore_long_string(stringstore):
|
|
|
|
text = "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"
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("factor", [254, 255, 256])
|
2017-01-12 17:07:31 +03:00
|
|
|
def test_stringstore_multiply(stringstore, factor):
|
2018-11-27 03:09:36 +03:00
|
|
|
text = "a" * factor
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
|
|
|
|
|
|
|
|
|
|
|
def test_stringstore_massive_strings(stringstore):
|
2018-11-27 03:09:36 +03:00
|
|
|
text = "a" * 511
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text
|
2018-11-27 03:09:36 +03:00
|
|
|
text2 = "z" * 512
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text2)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text2
|
2018-11-27 03:09:36 +03:00
|
|
|
text3 = "1" * 513
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text3)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert stringstore[store] == text3
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("text", ["qqqqq"])
|
2017-05-22 13:38:00 +03:00
|
|
|
def test_stringstore_to_bytes(stringstore, text):
|
2017-05-28 13:36:27 +03:00
|
|
|
store = stringstore.add(text)
|
2017-05-22 13:38:00 +03:00
|
|
|
serialized = stringstore.to_bytes()
|
|
|
|
new_stringstore = StringStore().from_bytes(serialized)
|
2017-01-12 17:07:31 +03:00
|
|
|
assert new_stringstore[store] == text
|