Coverage for src/wiktextract/extractor/ru/translation.py: 93%
72 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 07:03 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 07:03 +0000
1from mediawiki_langcodes import name_to_code
2from wikitextprocessor import HTMLNode, NodeKind, TemplateNode, WikiNode
4from ...page import clean_node
5from ...wxr_context import WiktextractContext
6from .models import Translation, WordEntry
7from .tags import translate_raw_tags
10def extract_translations(
11 wxr: WiktextractContext,
12 word_entry: WordEntry,
13 level3_node: WikiNode,
14):
15 for template_node in level3_node.find_child(NodeKind.TEMPLATE):
16 if template_node.template_name == "перев-блок": 16 ↛ 15line 16 didn't jump to line 15 because the condition on line 16 was always true
17 process_translate_block_template(wxr, word_entry, template_node)
20def process_translate_block_template(
21 wxr: WiktextractContext,
22 word_entry: WordEntry,
23 template_node: TemplateNode,
24) -> None:
25 # https://ru.wiktionary.org/wiki/Шаблон:перев-блок
26 expanded_template = wxr.wtp.parse(
27 wxr.wtp.node_to_wikitext(template_node), expand_all=True
28 )
29 sense = clean_node(wxr, None, template_node.template_parameters.get(1, ""))
30 for list_item in expanded_template.find_child_recursively(
31 NodeKind.LIST_ITEM
32 ):
33 translation = Translation(word="", lang="", sense=sense)
34 process_translate_list_item_nodes(
35 wxr, word_entry, translation, list_item.children
36 )
37 if translation.word != "" and translation.lang != "": 37 ↛ 30line 37 didn't jump to line 30 because the condition on line 37 was always true
38 if translation.lang_code == "": 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true
39 translation.lang_code = name_to_code(translation.lang, "ru")
40 translate_raw_tags(translation)
41 word_entry.translations.append(translation)
44def process_translate_list_item_nodes(
45 wxr: WiktextractContext,
46 word_entry: WordEntry,
47 translation: Translation,
48 nodes: list[WikiNode | str],
49) -> None:
50 for node in nodes:
51 if isinstance(node, WikiNode):
52 if node.kind == NodeKind.HTML:
53 css_class = node.attrs.get("class", "").split()
54 if node.tag == "sub" or (
55 node.tag == "span" and "perev-code" in css_class
56 ):
57 translation.lang_code = clean_node(
58 wxr, None, node.children
59 )
60 elif node.tag == "sup":
61 # language index
62 title = node.attrs.get("title", "")
63 if len(title) > 0: 63 ↛ 50line 63 didn't jump to line 50 because the condition on line 63 was always true
64 translation.raw_tags.append(title)
65 elif node.tag == "span" and "perev-lang" in css_class:
66 # language label, Модуль:languages since 2026-10-13
67 process_translate_list_item_nodes(
68 wxr, word_entry, translation, node.children
69 )
70 elif node.tag == "span":
71 process_translate_list_span_tag(
72 wxr, word_entry, translation, node
73 )
74 elif node.kind == NodeKind.LINK: 74 ↛ 50line 74 didn't jump to line 50 because the condition on line 74 was always true
75 translation.lang = clean_node(wxr, None, node)
76 elif isinstance(node, str) and len(node.strip(" ():\n")) > 0:
77 translation.raw_tags.append(node.strip(" ():\n"))
80def process_translate_list_span_tag(
81 wxr: WiktextractContext,
82 word_entry: WordEntry,
83 translation: Translation,
84 span_node: HTMLNode,
85) -> None:
86 for node in span_node.children:
87 if isinstance(node, WikiNode):
88 if node.kind == NodeKind.LINK:
89 if translation.word == "":
90 translation.word = clean_node(wxr, None, node)
91 else:
92 translation.other = clean_node(wxr, None, node)
93 elif isinstance(node, HTMLNode) and node.tag in ["span", "i"]:
94 # gender tag
95 tag = clean_node(wxr, None, node)
96 if len(tag) > 0: 96 ↛ 86line 96 didn't jump to line 86 because the condition on line 96 was always true
97 translation.raw_tags.append(tag)
98 elif node.kind == NodeKind.ITALIC:
99 translation.raw_tags.append(clean_node(wxr, None, node))
100 elif isinstance(node, str): 100 ↛ 86line 100 didn't jump to line 86 because the condition on line 100 was always true
101 # convert escaped characters like " "
102 text = clean_node(wxr, None, node)
103 if text.endswith((",", ";")):
104 # this list item has multiple translation words
105 striped_text = text.strip(",; ")
106 if striped_text.startswith("(") and striped_text.endswith(")"):
107 translation.roman = striped_text.strip("()")
108 if translation.word != "" and translation.lang != "": 108 ↛ 117line 108 didn't jump to line 117 because the condition on line 108 was always true
109 if translation.lang_code == "":
110 translation.lang_code = name_to_code(
111 translation.lang, "ru"
112 )
113 translate_raw_tags(translation)
114 word_entry.translations.append(
115 translation.model_copy(deep=True)
116 )
117 translation.word = ""
118 translation.roman = ""
119 translation.tags = []
120 translation.raw_tags = []
121 elif text.startswith("(") and text.endswith(")"):
122 translation.roman = text.strip("()")
123 elif (
124 text.startswith(",")
125 and text.endswith(")")
126 and translation.lang_code == "ja"
127 ):
128 translation.roman = text.strip(",() ")