mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 09:57:26 +03:00 
			
		
		
		
	Modernise vocab API tests and remove old xfailing tests
This commit is contained in:
		
							parent
							
								
									7ea87684cd
								
							
						
					
					
						commit
						92e3d8b3ee
					
				| 
						 | 
					@ -1,90 +1,39 @@
 | 
				
			||||||
 | 
					# coding: utf-8
 | 
				
			||||||
from __future__ import unicode_literals
 | 
					from __future__ import unicode_literals
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from ...attrs import LEMMA, ORTH, PROB, IS_ALPHA
 | 
				
			||||||
 | 
					from ...parts_of_speech import NOUN, VERB
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import pytest
 | 
					import pytest
 | 
				
			||||||
import io
 | 
					 | 
				
			||||||
import cloudpickle
 | 
					 | 
				
			||||||
import pickle
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from spacy.attrs import LEMMA, ORTH, PROB, IS_ALPHA
 | 
					 | 
				
			||||||
from spacy.parts_of_speech import NOUN, VERB
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from spacy.attrs import LEMMA, ORTH, PROB, IS_ALPHA
 | 
					 | 
				
			||||||
from spacy.parts_of_speech import NOUN, VERB
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_neq(en_vocab):
 | 
					@pytest.mark.parametrize('text1,text2', [
 | 
				
			||||||
    addr = en_vocab['Hello']
 | 
					    ("Hello", "bye"), ("Hello", "hello"), ("Hello", "Hello,")])
 | 
				
			||||||
    assert en_vocab['bye'].orth != addr.orth
 | 
					def test_vocab_api_neq(en_vocab, text1, text2):
 | 
				
			||||||
 | 
					    assert en_vocab[text1].orth != en_vocab[text2].orth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_eq(en_vocab):
 | 
					@pytest.mark.parametrize('text', "Hello")
 | 
				
			||||||
    addr = en_vocab['Hello']
 | 
					def test_vocab_api_eq(en_vocab, text):
 | 
				
			||||||
    assert en_vocab['Hello'].orth == addr.orth
 | 
					    lex = en_vocab[text]
 | 
				
			||||||
 | 
					    assert en_vocab[text].orth == lex.orth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_case_neq(en_vocab):
 | 
					@pytest.mark.parametrize('text', ["example"])
 | 
				
			||||||
    addr = en_vocab['Hello']
 | 
					def test_vocab_api_shape_attr(en_vocab, text):
 | 
				
			||||||
    assert en_vocab['hello'].orth != addr.orth
 | 
					    lex = en_vocab[text]
 | 
				
			||||||
 | 
					    assert lex.orth != lex.shape
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_punct_neq(en_vocab):
 | 
					@pytest.mark.parametrize('string,symbol', [
 | 
				
			||||||
    addr = en_vocab['Hello']
 | 
					    ('IS_ALPHA', IS_ALPHA), ('NOUN', NOUN), ('VERB', VERB), ('LEMMA', LEMMA),
 | 
				
			||||||
    assert en_vocab['Hello,'].orth != addr.orth
 | 
					    ('ORTH', ORTH), ('PROB', PROB)])
 | 
				
			||||||
 | 
					def test_vocab_api_symbols(en_vocab, string, symbol):
 | 
				
			||||||
 | 
					    assert en_vocab.strings[string] == symbol
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_shape_attr(en_vocab):
 | 
					@pytest.mark.parametrize('text', "Hello")
 | 
				
			||||||
    example = en_vocab['example']
 | 
					def test_contains(en_vocab, text):
 | 
				
			||||||
    assert example.orth != example.shape
 | 
					    _ = en_vocab[text]
 | 
				
			||||||
 | 
					    assert text in en_vocab
 | 
				
			||||||
 | 
					    assert "LKsdjvlsakdvlaksdvlkasjdvljasdlkfvm" not in en_vocab
 | 
				
			||||||
def test_symbols(en_vocab):
 | 
					 | 
				
			||||||
    assert en_vocab.strings['IS_ALPHA'] == IS_ALPHA
 | 
					 | 
				
			||||||
    assert en_vocab.strings['NOUN'] == NOUN
 | 
					 | 
				
			||||||
    assert en_vocab.strings['VERB'] == VERB
 | 
					 | 
				
			||||||
    assert en_vocab.strings['LEMMA'] == LEMMA
 | 
					 | 
				
			||||||
    assert en_vocab.strings['ORTH'] == ORTH
 | 
					 | 
				
			||||||
    assert en_vocab.strings['PROB'] == PROB
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def test_contains(en_vocab):
 | 
					 | 
				
			||||||
    assert 'Hello' in en_vocab
 | 
					 | 
				
			||||||
    assert 'LKsdjvlsakdvlaksdvlkasjdvljasdlkfvm' not in en_vocab
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@pytest.mark.xfail
 | 
					 | 
				
			||||||
def test_pickle_vocab(en_vocab):
 | 
					 | 
				
			||||||
    file_ = io.BytesIO()
 | 
					 | 
				
			||||||
    cloudpickle.dump(en_vocab, file_)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    file_.seek(0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    loaded = pickle.load(file_)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@pytest.mark.xfail
 | 
					 | 
				
			||||||
def test_pickle_vocab_vectors(en_vocab):
 | 
					 | 
				
			||||||
    vectors_length = en_vocab.vectors_length
 | 
					 | 
				
			||||||
    assert vectors_length != 0
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    apples = en_vocab['apples']
 | 
					 | 
				
			||||||
    oranges = en_vocab['oranges']
 | 
					 | 
				
			||||||
    hippos = en_vocab['hippos']
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    assert apples.similarity(oranges) > apples.similarity(hippos)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    apples.vector = hippos.vector
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert apples.similarity(oranges) < apples.similarity(hippos)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    file_ = io.BytesIO()
 | 
					 | 
				
			||||||
    cloudpickle.dump(en_vocab, file_)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    file_.seek(0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    loaded = pickle.load(file_)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    apples = loaded['apples']
 | 
					 | 
				
			||||||
    oranges = loaded['oranges']
 | 
					 | 
				
			||||||
    hippos = loaded['hippos']
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    assert apples.similarity(oranges) < apples.similarity(hippos)
 | 
					 | 
				
			||||||
   
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user