Coverage for src/wiktextract/extractor/es/pronunciation.py: 90%
86 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-04 10:58 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-04 10:58 +0000
1from wikitextprocessor import HTMLNode, NodeKind, TemplateNode, WikiNode
3from ...page import clean_node
4from ...wxr_context import WiktextractContext
5from ..share import set_sound_file_url_fields
6from .models import Sound, WordEntry
7from .tags import translate_raw_tags
9# translate table row header to sound model field
10PRON_GRAF_HEADER_MAP = {
11 "rimas": "rhymes",
12 "rima": "rhymes",
13}
16def process_pron_graf_template(
17 wxr: WiktextractContext, word_entry: WordEntry, template_node: TemplateNode
18) -> None:
19 # https://es.wiktionary.org/wiki/Plantilla:pron-graf
20 # this template could create sound data without any parameter
21 # it expands to a two columns table
22 expanded_node = wxr.wtp.parse(
23 wxr.wtp.node_to_wikitext(template_node), expand_all=True
24 )
25 table_nodes = list(expanded_node.find_child(NodeKind.TABLE))
26 if len(table_nodes) == 0: 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true
27 return
28 table_node = table_nodes[0]
29 extra_sounds = {} # not translated
30 for table_row in table_node.find_child(NodeKind.TABLE_ROW):
31 table_cells = list(table_row.find_child(NodeKind.TABLE_CELL))
32 if len(table_cells) != 2:
33 continue
34 header_node, value_node = table_cells
35 header_text = clean_node(wxr, None, header_node)
36 value_text = clean_node(wxr, None, value_node)
37 if header_text.endswith(" (AFI)"): # IPA
38 process_pron_graf_ipa_cell(wxr, word_entry, value_node, header_text)
39 elif header_text == "silabación":
40 word_entry.hyphenation = value_text
41 elif header_text in PRON_GRAF_HEADER_MAP:
42 sound = Sound()
43 setattr(sound, PRON_GRAF_HEADER_MAP[header_text], value_text)
44 word_entry.sounds.append(sound)
45 elif (
46 header_text.endswith(" alternativas") or header_text == "variantes"
47 ):
48 process_pron_graf_link_cell(
49 wxr, word_entry, value_node, header_text, "alternative"
50 )
51 elif header_text == "homófonos":
52 process_pron_graf_link_cell(
53 wxr, word_entry, value_node, header_text, "homophone"
54 )
55 elif header_text == "transliteraciones":
56 process_pron_graf_text_cell(wxr, word_entry, value_node, "roman")
57 elif header_text == "transcripciones silábicas": 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true
58 process_pron_graf_text_cell(wxr, word_entry, value_node, "syllabic")
59 else:
60 extra_sounds[header_text] = value_text
62 if len(extra_sounds) > 0:
63 word_entry.extra_sounds = extra_sounds
64 clean_node(wxr, word_entry, expanded_node)
67def process_pron_graf_ipa_cell(
68 wxr: WiktextractContext,
69 word_entry: WordEntry,
70 cell_node: WikiNode,
71 header_text: str,
72) -> None:
73 sound = Sound()
74 for node in cell_node.children:
75 if isinstance(node, str) and len(node.strip()) > 0:
76 sound.ipa += node.strip()
77 elif isinstance(node, HTMLNode) and node.tag == "phonos":
78 sound_file = node.attrs.get("file", "")
79 set_sound_file_url_fields(wxr, sound_file, sound)
80 for small_tag in node.find_html("small"):
81 location = clean_node(wxr, None, small_tag)
82 sound.raw_tags.append(location)
83 elif (
84 isinstance(node, HTMLNode) and node.tag == "br" and sound != Sound()
85 ):
86 if not header_text.startswith("pronunciación"): # location
87 sound.raw_tags.append(header_text.removesuffix(" (AFI)"))
88 translate_raw_tags(sound)
89 word_entry.sounds.append(sound.model_copy(deep=True))
90 sound = Sound()
91 if sound != Sound(): 91 ↛ 92line 91 didn't jump to line 92 because the condition on line 91 was never true
92 if not header_text.startswith("pronunciación"):
93 sound.raw_tags.append(header_text.removesuffix(" (AFI)"))
94 translate_raw_tags(sound)
95 word_entry.sounds.append(sound)
98def process_pron_graf_link_cell(
99 wxr: WiktextractContext,
100 word_entry: WordEntry,
101 cell_node: WikiNode,
102 header_text: str,
103 field_name: str,
104) -> None:
105 for link_index, link_node in cell_node.find_child(
106 NodeKind.LINK, with_index=True
107 ):
108 sound = Sound()
109 setattr(sound, field_name, clean_node(wxr, None, link_node))
110 if (
111 link_index + 1 < len(cell_node.children)
112 and isinstance(cell_node.children[link_index + 1], HTMLNode)
113 and cell_node.children[link_index + 1].tag == "ref"
114 ):
115 # nest "ref" tag is note text
116 sound.note = clean_node(
117 wxr, None, cell_node.children[link_index + 1].children
118 )
119 if header_text == "variantes": 119 ↛ 120line 119 didn't jump to line 120 because the condition on line 119 was never true
120 sound.not_same_pronunciation = True
121 word_entry.sounds.append(sound)
124def process_pron_graf_text_cell(
125 wxr: WiktextractContext,
126 word_entry: WordEntry,
127 cell_node: WikiNode,
128 field_name: str,
129) -> None:
130 sound = Sound()
131 for node_index, node in enumerate(cell_node.children):
132 if isinstance(node, str) and len(node.strip()) > 0:
133 node = node.strip()
134 if node.startswith(", "):
135 node = node.removeprefix(", ")
136 word_entry.sounds.append(sound.model_copy(deep=True))
137 sound = Sound()
138 setattr(sound, field_name, node.strip())
139 elif isinstance(node, HTMLNode) and node.tag == "ref":
140 sound.note = clean_node(wxr, None, node.children)
141 if len(getattr(sound, field_name)) > 0: 141 ↛ exitline 141 didn't return from function 'process_pron_graf_text_cell' because the condition on line 141 was always true
142 word_entry.sounds.append(sound)