Coverage for src/wiktextract/extractor/vi/etymology.py: 49%
54 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-07 00:55 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-07 00:55 +0000
1from wikitextprocessor import (
2 HTMLNode,
3 LevelNode,
4 NodeKind,
5 TemplateNode,
6 WikiNode,
7)
9from ...page import clean_node
10from ...wxr_context import WiktextractContext
11from .models import Form, WordEntry
12from .tags import translate_raw_tags
15def extract_etymology_section(
16 wxr: WiktextractContext, base_data: WordEntry, level_node: LevelNode
17):
18 e_nodes = []
19 for node in level_node.children:
20 if isinstance(node, TemplateNode) and (
21 node.template_name.endswith("-kanjitab")
22 or node.template_name == "ja-kt"
23 ):
24 extract_ja_kanjitab_template(wxr, node, base_data)
25 elif isinstance(node, WikiNode) and node.kind == NodeKind.LIST:
26 for list_item in node.find_child(NodeKind.LIST_ITEM):
27 e_links: list[tuple[str, str]] = []
28 e_text = clean_node(
29 wxr, base_data, list_item.children, link_collector=e_links
30 )
31 if e_text != "": 31 ↛ 26line 31 didn't jump to line 26 because the condition on line 31 was always true
32 base_data.etymology_texts.append(e_text)
33 base_data.etymology_links.extend(e_links)
34 elif isinstance(node, LevelNode):
35 break
36 else:
37 e_nodes.append(node)
39 if len(e_nodes) > 0: 39 ↛ exitline 39 didn't return from function 'extract_etymology_section' because the condition on line 39 was always true
40 e_links: list[tuple[str, str]] = []
41 e_text = clean_node(wxr, base_data, e_nodes, link_collector=e_links)
42 if e_text != "":
43 base_data.etymology_texts.append(e_text)
44 base_data.etymology_links.extend(e_links)
47def extract_ja_kanjitab_template(
48 wxr: WiktextractContext, t_node: TemplateNode, base_data: WordEntry
49):
50 expanded_node = wxr.wtp.parse(
51 wxr.wtp.node_to_wikitext(t_node), expand_all=True
52 )
53 for table in expanded_node.find_child(NodeKind.TABLE): 53 ↛ 54line 53 didn't jump to line 54 because the loop on line 53 never started
54 is_alt_form_table = False
55 for row in table.find_child(NodeKind.TABLE_ROW):
56 for header_node in row.find_child(NodeKind.TABLE_HEADER_CELL):
57 header_text = clean_node(wxr, None, header_node)
58 if header_text == "Cách viết khác":
59 is_alt_form_table = True
60 if not is_alt_form_table:
61 continue
62 forms = []
63 for row in table.find_child(NodeKind.TABLE_ROW):
64 for cell_node in row.find_child(NodeKind.TABLE_CELL):
65 for child_node in cell_node.children:
66 if isinstance(child_node, HTMLNode):
67 if child_node.tag == "span":
68 word = clean_node(wxr, None, child_node)
69 if word != "":
70 forms.append(
71 Form(
72 form=word, tags=["alternative", "kanji"]
73 )
74 )
75 elif child_node.tag == "small":
76 raw_tag = clean_node(wxr, None, child_node).strip(
77 "()"
78 )
79 if raw_tag != "" and len(forms) > 0:
80 forms[-1].raw_tags.append(raw_tag)
81 translate_raw_tags(forms[-1])
82 base_data.forms.extend(forms)
83 for link_node in expanded_node.find_child(NodeKind.LINK):
84 clean_node(wxr, base_data, link_node)