spaCy/spacy/tests/vocab_vectors/test_lexeme.py

69 lines
2.3 KiB
Python
Raw Normal View History

2017-01-12 17:51:20 +03:00
import pytest
💫 Refactor test suite (#2568) ## Description Related issues: #2379 (should be fixed by separating model tests) * **total execution time down from > 300 seconds to under 60 seconds** 🎉 * removed all model-specific tests that could only really be run manually anyway – those will now live in a separate test suite in the [`spacy-models`](https://github.com/explosion/spacy-models) repository and are already integrated into our new model training infrastructure * changed all relative imports to absolute imports to prepare for moving the test suite from `/spacy/tests` to `/tests` (it'll now always test against the installed version) * merged old regression tests into collections, e.g. `test_issue1001-1500.py` (about 90% of the regression tests are very short anyways) * tidied up and rewrote existing tests wherever possible ### Todo - [ ] move tests to `/tests` and adjust CI commands accordingly - [x] move model test suite from internal repo to `spacy-models` - [x] ~~investigate why `pipeline/test_textcat.py` is flakey~~ - [x] review old regression tests (leftover files) and see if they can be merged, simplified or deleted - [ ] update documentation on how to run tests ### Types of change enhancement, tests ## Checklist <!--- Before you submit the PR, go over this checklist and make sure you can tick off all the boxes. [] -> [x] --> - [x] I have submitted the spaCy Contributor Agreement. - [x] I ran the tests, and all new and existing tests passed. - [ ] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-07-25 00:38:44 +03:00
from spacy.attrs import IS_ALPHA, IS_DIGIT
@pytest.mark.parametrize("text1,prob1,text2,prob2", [("NOUN", -1, "opera", -2)])
2017-01-12 17:51:20 +03:00
def test_vocab_lexeme_lt(en_vocab, text1, text2, prob1, prob2):
"""More frequent is l.t. less frequent"""
lex1 = en_vocab[text1]
lex1.prob = prob1
lex2 = en_vocab[text2]
lex2.prob = prob2
assert lex1 < lex2
assert lex2 > lex1
@pytest.mark.parametrize("text1,text2", [("phantom", "opera")])
2017-01-12 17:51:20 +03:00
def test_vocab_lexeme_hash(en_vocab, text1, text2):
"""Test that lexemes are hashable."""
lex1 = en_vocab[text1]
lex2 = en_vocab[text2]
lexes = {lex1: lex1, lex2: lex2}
assert lexes[lex1].orth_ == text1
assert lexes[lex2].orth_ == text2
def test_vocab_lexeme_is_alpha(en_vocab):
assert en_vocab["the"].flags & (1 << IS_ALPHA)
assert not en_vocab["1999"].flags & (1 << IS_ALPHA)
assert not en_vocab["hello1"].flags & (1 << IS_ALPHA)
2017-01-12 17:51:20 +03:00
def test_vocab_lexeme_is_digit(en_vocab):
assert not en_vocab["the"].flags & (1 << IS_DIGIT)
assert en_vocab["1999"].flags & (1 << IS_DIGIT)
assert not en_vocab["hello1"].flags & (1 << IS_DIGIT)
2017-01-12 17:51:20 +03:00
def test_vocab_lexeme_add_flag_auto_id(en_vocab):
is_len4 = en_vocab.add_flag(lambda string: len(string) == 4)
2018-11-30 19:43:08 +03:00
assert en_vocab["1999"].check_flag(is_len4) is True
assert en_vocab["1999"].check_flag(IS_DIGIT) is True
assert en_vocab["199"].check_flag(is_len4) is False
assert en_vocab["199"].check_flag(IS_DIGIT) is True
assert en_vocab["the"].check_flag(is_len4) is False
assert en_vocab["dogs"].check_flag(is_len4) is True
2017-01-12 17:51:20 +03:00
def test_vocab_lexeme_add_flag_provided_id(en_vocab):
is_len4 = en_vocab.add_flag(lambda string: len(string) == 4, flag_id=IS_DIGIT)
2018-11-30 19:43:08 +03:00
assert en_vocab["1999"].check_flag(is_len4) is True
assert en_vocab["199"].check_flag(is_len4) is False
assert en_vocab["199"].check_flag(IS_DIGIT) is False
assert en_vocab["the"].check_flag(is_len4) is False
assert en_vocab["dogs"].check_flag(is_len4) is True
def test_lexeme_bytes_roundtrip(en_vocab):
one = en_vocab["one"]
alpha = en_vocab["alpha"]
assert one.orth != alpha.orth
assert one.lower != alpha.lower
alpha.from_bytes(one.to_bytes())
assert one.orth_ == alpha.orth_
assert one.orth == alpha.orth
assert one.lower == alpha.lower
assert one.lower_ == alpha.lower_