import pytest from base import BaseTest from conftest import NewOntology from evaluators.base_evaluator import MultipleChoices from helpers import _mt, _ut, get_concepts, get_from, get_metadata, get_parser_input from parsers.SimpleParserParser import SimpleConceptsParser class TestSimpleConceptsParser(BaseTest): @pytest.fixture() def parser(self): return SimpleConceptsParser() @pytest.mark.parametrize("text, expected", [ ("I am a new concept", [_mt("1003", 0, 8)]), ("xxx yyy I am a new concept", [_ut("xxx yyy ", 0, 3), _mt("1003", 4, 12)]), ("I am a new concept xxx yyy", [_mt("1003", 0, 8), _ut(" xxx yyy", 9, 12)]), ("xxx I am a new concept yyy", [_ut("xxx ", 0, 1), _mt("1003", 2, 10), _ut(" yyy", 11, 12)]), ("c:#1003:", [_mt("1003", 0, 0)]), ("xxx c:#1003: yyy", [_ut("xxx ", 0, 1), _mt("1003", 2, 2), _ut(" yyy", 3, 4)]), ("xxx c:I am: yyy", [_ut("xxx ", 0, 1), _mt("1002", 2, 2), _ut(" yyy", 3, 4)]), (" I am a new concept", [_ut(" ", 0, 0), _mt("1003", 1, 9)]) ]) def test_i_can_recognize_a_concept(self, context, parser, text, expected): with NewOntology(context, "test_i_can_recognize_a_concept"): get_concepts(context, "I", "I am", "I am a new concept", use_sheerka=True) pi = get_parser_input(text) res = parser.parse(context, pi) assert res == MultipleChoices([expected]) assert not parser.error_sink @pytest.mark.parametrize("text, expected", [ ("foo", [_mt("1001", 0, 0)]), ("I am a new concept", [_mt("1001", 0, 8)]) ]) def test_i_can_recognize_a_concept_by_its_name_and_its_definition(self, context, parser, text, expected): with NewOntology(context, "test_i_can_recognize_a_concept_by_its_name_and_its_definition"): get_concepts(context, get_metadata(name="foo", definition="I am a new concept"), use_sheerka=True) pi = get_parser_input(text) res = parser.parse(context, pi) assert res == MultipleChoices([expected]) assert not parser.error_sink @pytest.mark.parametrize("text, expected", [ ("long concept name", [_mt("1001", 0, 4)]), ("I am a new concept", [_mt("1001", 0, 8)]) ]) def test_i_can_recognize_a_concept_by_its_name_when_long_name(self, context, parser, text, expected): with NewOntology(context, "test_i_can_recognize_a_concept_by_its_name_when_long_name"): get_concepts(context, get_metadata(name="long concept name", definition="I am a new concept"), use_sheerka=True) pi = get_parser_input(text) res = parser.parse(context, pi) assert res == MultipleChoices([expected]) assert not parser.error_sink def test_i_can_parse_a_sequence_of_concept(self, context, parser): with NewOntology(context, "test_i_can_parse_a_sequence_of_concept"): get_concepts(context, "foo bar", "baz", "qux", use_sheerka=True) pi = get_parser_input("foo bar baz foo, qux") res = parser.parse(context, pi) expected = [_mt("1001", 0, 2), _ut(" ", 3, 3), _mt("1002", 4, 4), _ut(" foo, ", 5, 8), _mt("1003", 9, 9)] assert res == MultipleChoices([expected]) assert not parser.error_sink def test_i_can_detect_multiple_choices(self, context, parser): with NewOntology(context, "test_i_can_detect_multiple_choices"): get_concepts(context, "foo bar", "bar baz", use_sheerka=True) pi = get_parser_input("foo bar baz") res = parser.parse(context, pi) expected1 = [_mt("1001", 0, 2), _ut(" baz", 3, 4)] expected2 = [_ut("foo ", 0, 1), _mt("1002", 2, 4)] assert res == MultipleChoices([expected1, expected2]) assert not parser.error_sink def test_i_can_detect_multiple_choices_2(self, context, parser): with NewOntology(context, "test_i_can_detect_multiple_choices_2"): get_concepts(context, "one two", "one", "two", use_sheerka=True) pi = get_parser_input("one two") res = parser.parse(context, pi) expected1 = [_mt("1001", 0, 2)] expected2 = [_mt("1002", 0, 0), _ut(" ", 1, 1), _mt("1003", 2, 2)] assert res == MultipleChoices([expected1, expected2]) assert not parser.error_sink def test_i_can_detect_multiple_choices_3(self, context, parser): with NewOntology(context, "test_i_can_detect_multiple_choices_2"): get_concepts(context, "one two", "one", "two", use_sheerka=True) pi = get_parser_input("one two xxx one two") res = parser.parse(context, pi) e1 = get_from(_mt("c:one two#1001:"), _ut(" xxx "), _mt("c:#1001:")) e2 = get_from(_mt("c:one#1002:"), _ut(" "), _mt("c:two#1003:"), _ut(" xxx "), _mt("c:one two#1001:")) e3 = get_from(_mt("c:one two#1001:"), _ut(" xxx "), _mt("c:one#1002:"), _ut(" "), _mt("c:two#1003:")) e4 = get_from(_mt("c:one#1002:"), _ut(" "), _mt("c:two#1003:"), _ut(" xxx "), _mt("c:#1002:"), _ut(" "), _mt("c:#1003:")) assert res == MultipleChoices([e1, e2, e3, e4]) assert not parser.error_sink def test_nothing_is_return_is_no_concept_is_recognized(self, context, parser): pi = get_parser_input("one two three") res = parser.parse(context, pi) assert res == MultipleChoices([]) def test_i_can_manage_attribute_reference(self, context, parser): with NewOntology(context, "test_i_can_detect_multiple_choices_2"): get_concepts(context, "foo", "i am a concept", use_sheerka=True) pi = get_parser_input("foo.attribute") res = parser.parse(context, pi) expected = [_mt("1001", 0, 0), _ut(".attribute", 1, 2)] assert res == MultipleChoices([expected]) pi = get_parser_input("i am a concept.attribute") res = parser.parse(context, pi) expected = [_mt("1002", 0, 6), _ut(".attribute", 7, 8)] assert res == MultipleChoices([expected])