Coverage for src/wiktextract/extractor/ru/example.py: 89%
39 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-12-27 08:07 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-12-27 08:07 +0000
1from wikitextprocessor import NodeKind, TemplateNode
3from ...page import clean_node
4from ...wxr_context import WiktextractContext
5from .models import Example, Sense
7EXAMPLE_TEMPLATES = frozenset(["пример", "english surname example"])
10def process_example_template(
11 wxr: WiktextractContext,
12 sense: Sense,
13 template_node: TemplateNode,
14):
15 if template_node.template_name == "пример":
16 process_пример_template(wxr, sense, template_node)
17 elif template_node.template_name == "english surname example": 17 ↛ exitline 17 didn't return from function 'process_example_template' because the condition on line 17 was always true
18 process_en_surname_example_template(wxr, sense, template_node)
21ПРИМЕР_TEMPLATE_ARG_MAPPING = {
22 "автор": "author",
23 "титул": "title",
24 "дата": "date",
25 "издание": "collection",
26 "дата издания": "date_published",
27 "ответственный": "editor",
28 "перев": "translator",
29 "источник": "source",
30 2: "author",
31 3: "title",
32 4: "date",
33 5: "collection",
34 6: "date_published",
35}
38def process_пример_template(
39 wxr: WiktextractContext,
40 sense: Sense,
41 template_node: TemplateNode,
42):
43 # https://ru.wiktionary.org/wiki/Шаблон:пример
44 example = Example()
45 for arg_name, arg_value in template_node.template_parameters.items():
46 value = clean_node(wxr, None, arg_value)
47 if len(value) == 0:
48 continue
49 if arg_name == 1:
50 example.text = value
51 elif arg_name == "текст": 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true
52 example.text = value
53 elif arg_name == "перевод":
54 example.translation = value
55 elif arg_name in ПРИМЕР_TEMPLATE_ARG_MAPPING: 55 ↛ 45line 55 didn't jump to line 45 because the condition on line 55 was always true
56 field_name = ПРИМЕР_TEMPLATE_ARG_MAPPING[arg_name]
57 if field_name in example.model_fields: 57 ↛ 60line 57 didn't jump to line 60 because the condition on line 57 was always true
58 setattr(example, field_name, value)
59 else:
60 wxr.wtp.debug(
61 f"Unknown {arg_name=} in example template {template_node}",
62 sortid="ru/example/process_example_template/54",
63 )
65 expanded_node = wxr.wtp.parse(
66 wxr.wtp.node_to_wikitext(template_node), expand_all=True
67 )
68 for span_node in expanded_node.find_html_recursively(
69 "span", attr_name="class", attr_value="example-details"
70 ):
71 example.ref = clean_node(wxr, None, span_node)
72 break
74 if len(example.text) > 0:
75 sense.examples.append(example)
78def process_en_surname_example_template(
79 wxr: WiktextractContext,
80 sense: Sense,
81 template_node: TemplateNode,
82) -> None:
83 # https://ru.wiktionary.org/wiki/Шаблон:english_surname_example
84 expanded_node = wxr.wtp.parse(
85 wxr.wtp.node_to_wikitext(template_node),
86 additional_expand={"english surname example"},
87 pre_expand=True,
88 )
89 for node in expanded_node.find_child(NodeKind.TEMPLATE):
90 if node.template_name == "пример": 90 ↛ 89line 90 didn't jump to line 89 because the condition on line 90 was always true
91 process_пример_template(wxr, sense, node)