def _deactivate_alineas(
        text: StructuredText, inapplicability: PotentialInapplicability,
        parameter_values: Dict[Parameter, Any]) -> StructuredText:
    text = copy(text)
    inactive_alineas = inapplicability.alineas
    all_inactive = inactive_alineas is None
    warning = generate_inactive_warning(inapplicability.condition,
                                        parameter_values,
                                        all_alineas=all_inactive,
                                        whole_text=False)
    if inactive_alineas is not None:
        inactive_alineas_set = set(inactive_alineas)
        new_outer_alineas = [
            replace(al, inactive=i in inactive_alineas_set)
            for i, al in enumerate(text.outer_alineas)
        ]
    else:
        new_outer_alineas = [
            replace(al, inactive=True) for al in text.outer_alineas
        ]
    text.applicability = Applicability(active=not all_inactive,
                                       warnings=[warning])
    if inapplicability.subsections_are_inapplicable:
        text.sections = [
            _deactivate_child_section(section, all_inactive=all_inactive)
            for section in text.sections
        ]
    text.outer_alineas = new_outer_alineas
    return text
Пример #2
0
def _detect_and_add_topics(section: StructuredText,
                           matching_depth: int,
                           current_depth: int = 0) -> StructuredText:
    section = copy(section)
    topic = _detect(
        section.title.text) if matching_depth == current_depth else None
    section.annotations = Annotations(topic)
    section.sections = [
        _detect_and_add_topics(sec, matching_depth, current_depth + 1)
        for sec in section.sections
    ]
    return section
def _get_simple_text() -> StructuredText:
    row1 = Row([_cell('AA'), _cell('BB'), _cell('CC')], True)
    row2 = Row([_cell('DD', rs=2), _cell('EE', cs=2)], False)
    row3 = Row([_cell('FF'), _cell('GG')], False)
    table = Table([row1, row2, row3])
    alineas = [
        _text('Alinea 1'),
        _text('Alinea 2'),
        EnrichedString('', table=table)
    ]
    sections = [StructuredText(_text('Title 2'), [], [], None, None, None)]
    text = StructuredText(_text('Title'), alineas, sections, None, None, None)
    return text
def _deactivate_child_section(section: StructuredText,
                              all_inactive: bool) -> StructuredText:
    section = copy(section)
    if section.applicability:
        section.applicability.active = not all_inactive
    else:
        section.applicability = Applicability(active=not all_inactive)
    section.sections = [
        _deactivate_child_section(sec, all_inactive)
        for sec in section.sections
    ]
    section.outer_alineas = [
        replace(al, inactive=True) for al in section.outer_alineas
    ]
    return section
Пример #5
0
 def from_dict(cls, dict_: Dict[str, Any]) -> 'AlternativeSection':
     return AlternativeSection(
         dict_['section_id'],
         StructuredText.from_dict(dict_['new_text']),
         load_condition(dict_['condition']),
         dict_['id'],
     )
Пример #6
0
def compute_detection_performance(dataset: List[_LabelizedText], ontology: TopicOntology):
    all_labels = [label for _, label in dataset]
    texts = [
        (titles, StructuredText(EnrichedString(''), outer_alineas, [], None)) for (titles, outer_alineas), _ in dataset
    ]
    for topic, texts_ in _missing_topics(all_labels, texts, ontology).items():
        _pretty_print(topic, texts_)
    predicted_labels = [_extract_topics(text, titles, ontology) for titles, text in texts]
    print(Counter([exp == pred for exp, pred in zip(all_labels, predicted_labels) if exp]))
Пример #7
0
def _node_section() -> StructuredText:
    return StructuredText(
        _str('abc'),
        [_str('abc'), _enriched_string_table()],
        [_leaf_section()],
        None,
        Reference('ref', 'name'),
        Annotations(None),
    )
def _apply_parameter_values_in_text(
        text: StructuredText, parameter_values: Dict[Parameter,
                                                     Any]) -> StructuredText:
    na_conditions, modifications, warnings = _extract_satisfied_objects_and_warnings(
        text.parametrization, parameter_values)
    text = copy(text)
    if not na_conditions and not modifications:
        text.sections = [
            _apply_parameter_values_in_text(section, parameter_values)
            for section in text.sections
        ]
        text.applicability = Applicability()
    else:
        text = _apply_satisfied_modificators(text, na_conditions,
                                             modifications, parameter_values)

    all_warnings = sorted(
        set(_ensure_applicabiliy(text.applicability).warnings + warnings))
    text.applicability = replace(_ensure_applicabiliy(text.applicability),
                                 warnings=all_warnings)
    return text
def test_add_topics():
    sub_section_1 = StructuredText(EnrichedString('Section 1.1'), [], [], None)
    section_1 = StructuredText(EnrichedString('Section 1'), [],
                               [sub_section_1], None)
    section_2 = StructuredText(EnrichedString('Section 2'), [], [], None)
    am = ArreteMinisteriel(EnrichedString('arrete du 10/10/10'),
                           [section_1, section_2], [],
                           None,
                           id='FAKE_ID')

    am_with_topics = add_topics(
        am, {
            (0, ): TopicName.INCENDIE,
            (0, 0): TopicName.INCENDIE,
            (1, ): TopicName.BRUIT_VIBRATIONS
        })
    sections = am_with_topics.sections
    assert sections[0].annotations.topic == TopicName.INCENDIE  # type: ignore
    assert sections[0].sections[
        0].annotations.topic == TopicName.INCENDIE  # type: ignore
    assert sections[
        1].annotations.topic == TopicName.BRUIT_VIBRATIONS  # type: ignore
Пример #10
0
def test_extract_text_lines():
    assert _get_simple_text().text_lines() == [
        'AM',
        'alinea',
        'foo',
        '# Section 1',
        '## Section 1.1',
        '# Section 2',
        'bar',
    ]
    assert StructuredText(EnrichedString(' A'), [], [], None).text_lines() == ['A']
    assert StructuredText(EnrichedString(' A'), [EnrichedString('')], [], None).text_lines() == ['A', '']
    assert _TEXT_A.text_lines() == [
        '6. Schématisation des différents types de joints mentionnés :',
        'Vous pouvez consulter les schémas dans le',
        'JO',
        'n° 265 du 16/11/2010 texte numéro 21',
    ]

    assert _TEXT_B.text_lines() == [
        '6. Schématisation des différents types de joints mentionnés :',
        'Vous pouvez consulter les schémas dans le',
        'JO n° 265 du 16/11/2010 texte numéro 21',
    ]
Пример #11
0
def _build_am() -> ArreteMinisteriel:
    subsection1 = StructuredText(EnrichedString('A'), [], [], None)
    subsection2 = StructuredText(EnrichedString('A'), [], [], None)
    sections = [
        StructuredText(EnrichedString('Art. 1'), [], [subsection1], None),
        StructuredText(EnrichedString('Art. 2'), [], [subsection2], None),
        StructuredText(EnrichedString('Conditions d\'application'), [], [],
                       None),
        StructuredText(EnrichedString('Art. 3'), [], [], None),
    ]
    return ArreteMinisteriel(EnrichedString('arrete du 10/10/10'),
                             sections, [],
                             None,
                             id='FAKE_ID')
def test_check_parametrization_consistency():
    date_ = ParameterEnum.DATE_INSTALLATION.value
    dt_1 = datetime.now()
    dt_2 = dt_1 + timedelta(days=1)
    cd_1 = Range(date_, dt_1, dt_2)
    cd_3 = Littler(date_, dt_1)
    cd_4 = Greater(date_, dt_2)
    cd_5 = Littler(date_, dt_2)
    cd_6 = Greater(date_, dt_1)
    new_text = StructuredText(_str('Art. 2'), [_str('version modifiée')], [],
                              None)
    Parametrization(
        [_NAC('', None, cd_1), _NAC('', None, cd_3)],
        [AlternativeSection('', new_text, cd_4)], []).check_consistency()

    Parametrization([_NAC('', None, cd_3)], [], []).check_consistency()
    with pytest.raises(ParametrizationError):
        Parametrization(
            [_NAC('', None, cd_1), _NAC('', None, cd_5)],
            [AlternativeSection('', new_text, cd_6)], []).check_consistency()
    with pytest.raises(ParametrizationError):
        Parametrization(
            [_NAC('', None, cd_1), _NAC('', None, cd_1)], [],
            []).check_consistency()
Пример #13
0
    assert not _is_probably_cid('JORFTEX')


def _get_simple_text() -> StructuredText:
    sub_section_1 = StructuredText(_str('Section 1.1'), [], [], None)
    section_1 = StructuredText(_str('Section 1'), [], [sub_section_1], None)
    section_2 = StructuredText(_str('Section 2'), [_str('bar')], [], None)
    return StructuredText(_str('AM '), [_str('alinea'), _str('foo')], [section_1, section_2], None)


_TEXT_A = StructuredText(
    title=EnrichedString(text='6. Schématisation des différents types de joints mentionnés :'),
    outer_alineas=[
        EnrichedString(text='Vous pouvez consulter les schémas dans le'),
        EnrichedString(text='JO\nn° 265 du 16/11/2010 texte numéro 21'),
    ],
    sections=[],
    applicability=None,
    reference=Reference('ref', 'name'),
    annotations=None,
    id='0bEB0b14A96f',
)
_TEXT_B = StructuredText(
    title=EnrichedString(text='6. Schématisation des différents types de joints mentionnés :'),
    outer_alineas=[
        EnrichedString(text='Vous pouvez consulter les schémas dans le'),
        EnrichedString(text='JO n° 265 du 16/11/2010 texte numéro 21'),
    ],
    sections=[],
    applicability=None,
    reference=None,
    annotations=None,
Пример #14
0
def test_structured_text():
    dict_ = _node_section().to_dict()
    new_dict = StructuredText.from_dict(dict_).to_dict()
    assert new_dict == dict_
Пример #15
0
def _leaf_section() -> StructuredText:
    app = Applicability(True, True, ['beware'], StructuredText(_str('abc'), [], [], None))
    annotations = Annotations(TopicName.AIR_ODEURS)
    return StructuredText(_str('abc'), [_str('abc')], [], app, Reference('ref', 'name'), annotations)
Пример #16
0
def test_add_references():
    sub_sub_sections = [
        StructuredText(estr('1.1. azeaze'), [], [], None, None),
        StructuredText(estr('1. 2. azeaze'), [], [], None, None),
    ]
    sub_sections = [
        StructuredText(estr('1. azeaze'), [], sub_sub_sections, None, None)
    ]
    sections = [
        StructuredText(estr('Article 1. efzefz'), [], sub_sections, None),
        StructuredText(estr('2. zefez'), [], [], None),
        StructuredText(estr('A. zefze'), [], [], None),
        StructuredText(estr('a) zefze'), [], [], None),
        StructuredText(estr('V. zefze'), [], [], None),
        StructuredText(estr('ANNEXE I zefze'), [], [], None),
        StructuredText(estr('Article 18.1'), [], [], None),
        StructuredText(estr('Article 1'), [], [], None),
    ]
    am = ArreteMinisteriel(estr('Arrete du 10/10/10'),
                           sections, [],
                           None,
                           id='FAKE_ID')
    am_with_references = add_references(am)
    secs = am_with_references.sections
    assert ensure_not_none(secs[0].reference).nb == 'Article 1.'
    assert ensure_not_none(
        secs[0].sections[0].reference).nb == 'Article 1. > 1.'
    assert ensure_not_none(
        secs[0].sections[0].sections[0].reference).nb == 'Article 1. > 1.1.'
    assert ensure_not_none(
        secs[0].sections[0].sections[1].reference).nb == 'Article 1. > 1.2.'
    assert ensure_not_none(secs[1].reference).nb == '2.'
    assert ensure_not_none(secs[2].reference).nb == 'A.'
    assert ensure_not_none(secs[3].reference).nb == 'a)'
    assert ensure_not_none(secs[4].reference).nb == 'V.'
    assert ensure_not_none(secs[5].reference).nb == 'Annexe I'
    assert ensure_not_none(secs[6].reference).nb == 'Article 18.1'
    assert ensure_not_none(secs[7].reference).nb == 'Article 1'
def _add_parametrization_in_section(text: StructuredText,
                                    parametrization: Parametrization) -> None:
    text.parametrization = _init_section_parametrization(
        parametrization, text.id)
Пример #18
0
def _get_simple_text() -> StructuredText:
    sub_section_1 = StructuredText(_str('Section 1.1'), [], [], None)
    section_1 = StructuredText(_str('Section 1'), [], [sub_section_1], None)
    section_2 = StructuredText(_str('Section 2'), [_str('bar')], [], None)
    return StructuredText(_str('AM '), [_str('alinea'), _str('foo')], [section_1, section_2], None)
from envinorma.models.text_elements import EnrichedString
from envinorma.parametrization.combinations import _generate_options_dicts, generate_exhaustive_combinations
from envinorma.parametrization.models.parametrization import (
    AlternativeSection,
    AMWarning,
    InapplicableSection,
    Parametrization,
    extract_conditions_from_parametrization,
)

_DATE = Parameter(id='date-d-installation', type=ParameterType.DATE)
_NEW_TEXT = StructuredText(
    title=EnrichedString(text='Article 2.1'),
    outer_alineas=[EnrichedString(text='Contenu nouveau')],
    sections=[],
    applicability=None,
    reference=None,
    annotations=None,
    id='d16d0fE7C7fc',
)
_PARAMETRIZATION = Parametrization(
    inapplicable_sections=[
        InapplicableSection(
            section_id='abcdef',
            alineas=None,
            condition=AndCondition(
                conditions=frozenset([Littler(parameter=_DATE, target=date(2021, 1, 1), strict=True)])
            ),
        )
    ],
    alternative_sections=[