-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1012 from xxyzz/ku
[ku] extract sound section
- Loading branch information
Showing
6 changed files
with
240 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from wikitextprocessor import LevelNode, NodeKind, TemplateNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from ..share import set_sound_file_url_fields | ||
from .models import Sound, WordEntry | ||
|
||
|
||
def extract_sound_section( | ||
wxr: WiktextractContext, word_entry: WordEntry, level_node: LevelNode | ||
) -> None: | ||
for list_node in level_node.find_child(NodeKind.LIST): | ||
for list_item in list_node.find_child(NodeKind.LIST_ITEM): | ||
for t_node in list_item.find_child(NodeKind.TEMPLATE): | ||
if t_node.template_name == "ku-IPA": | ||
extract_ku_ipa_template(wxr, word_entry, t_node) | ||
elif t_node.template_name == "deng": | ||
extract_deng_template(wxr, word_entry, t_node) | ||
elif t_node.template_name == "ku-kîte": | ||
extract_ku_kîte(wxr, word_entry, t_node) | ||
|
||
|
||
def extract_ku_ipa_template( | ||
wxr: WiktextractContext, word_entry: WordEntry, t_node: TemplateNode | ||
) -> None: | ||
expanded_node = wxr.wtp.parse( | ||
wxr.wtp.node_to_wikitext(t_node), expand_all=True | ||
) | ||
for span_tag in expanded_node.find_html( | ||
"span", attr_name="class", attr_value="IPA" | ||
): | ||
sound = Sound(ipa=clean_node(wxr, None, span_tag)) | ||
if sound.ipa != "": | ||
word_entry.sounds.append(sound) | ||
clean_node(wxr, word_entry, expanded_node) | ||
|
||
|
||
def extract_deng_template( | ||
wxr: WiktextractContext, word_entry: WordEntry, t_node: TemplateNode | ||
) -> None: | ||
sound = Sound( | ||
ipa=clean_node(wxr, None, t_node.template_parameters.get("ipa", "")) | ||
) | ||
raw_tag = clean_node( | ||
wxr, | ||
None, | ||
t_node.template_parameters.get( | ||
4, t_node.template_parameters.get("dever", "") | ||
), | ||
) | ||
if raw_tag != "": | ||
sound.raw_tags.append(raw_tag) | ||
filename = clean_node(wxr, None, t_node.template_parameters.get(2, "")) | ||
if filename != "": | ||
set_sound_file_url_fields(wxr, filename, sound) | ||
word_entry.sounds.append(sound) | ||
clean_node(wxr, word_entry, t_node) | ||
|
||
|
||
def extract_ku_kîte( | ||
wxr: WiktextractContext, word_entry: WordEntry, t_node: TemplateNode | ||
) -> None: | ||
expanded_node = wxr.wtp.parse( | ||
wxr.wtp.node_to_wikitext(t_node), expand_all=True | ||
) | ||
for index, node in enumerate(expanded_node.children): | ||
if isinstance(node, str) and ":" in node: | ||
hyphenation = clean_node( | ||
wxr, | ||
None, | ||
[node[node.index(":") + 1 :]] | ||
+ expanded_node.children[index + 1 :], | ||
).strip() | ||
if hyphenation != "": | ||
word_entry.hyphenation = hyphenation | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from unittest import TestCase | ||
|
||
from wikitextprocessor import Wtp | ||
|
||
from wiktextract.config import WiktionaryConfig | ||
from wiktextract.extractor.ku.page import parse_page | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
class TestKuSound(TestCase): | ||
maxDiff = None | ||
|
||
def setUp(self) -> None: | ||
self.wxr = WiktextractContext( | ||
Wtp(lang_code="ku"), | ||
WiktionaryConfig( | ||
dump_file_lang_code="ku", capture_language_codes=None | ||
), | ||
) | ||
|
||
def tearDown(self): | ||
self.wxr.wtp.close_db_conn() | ||
|
||
def test_ku_ipa(self): | ||
self.wxr.wtp.add_page("Şablon:ziman", 10, "Kurmancî") | ||
self.wxr.wtp.add_page( | ||
"Şablon:ku-IPA", | ||
10, | ||
"""[[IPA]]<sup>([[Wîkîferheng:IPA kurdî|kilîd]])</sup>: <span class="IPA">/ɑːv/</span>[[Kategorî:Bilêvkirina IPAyê bi kurmancî]]""", | ||
) | ||
page_data = parse_page( | ||
self.wxr, | ||
"av", | ||
"""== {{ziman|ku}} == | ||
=== Bilêvkirin === | ||
* {{ku-IPA}} | ||
=== Navdêr === | ||
# [[vexwarin|Vexwarin]]a bê[[reng]]""", | ||
) | ||
self.assertEqual( | ||
page_data[0]["categories"], ["Bilêvkirina IPAyê bi kurmancî"] | ||
) | ||
self.assertEqual(page_data[0]["sounds"], [{"ipa": "/ɑːv/"}]) | ||
|
||
def test_deng(self): | ||
self.wxr.wtp.add_page("Şablon:ziman", 10, "Kurmancî") | ||
self.wxr.wtp.add_page( | ||
"Şablon:deng", | ||
10, | ||
"""<phonos lang="ku" text="" wikibase="" file="LL-Q36368 (kur)-Dildadil-av.wav">Deng (Amed)</phonos></span>[[Kategorî:Dengên kurmancî ji Amedê]][[Kategorî:Deng bi kurmancî]]""", | ||
) | ||
page_data = parse_page( | ||
self.wxr, | ||
"av", | ||
"""== {{ziman|ku}} == | ||
=== Bilêvkirin === | ||
* {{deng|ku|LL-Q36368 (kur)-Dildadil-av.wav|Deng|Amed}} | ||
=== Navdêr === | ||
# [[vexwarin|Vexwarin]]a bê[[reng]]""", | ||
) | ||
self.assertEqual( | ||
page_data[0]["categories"], | ||
["Dengên kurmancî ji Amedê", "Deng bi kurmancî"], | ||
) | ||
self.assertEqual( | ||
page_data[0]["sounds"][0]["audio"], | ||
"LL-Q36368 (kur)-Dildadil-av.wav", | ||
) | ||
|
||
def test_ku_kîte(self): | ||
self.wxr.wtp.add_page("Şablon:ziman", 10, "Kurmancî") | ||
self.wxr.wtp.add_page( | ||
"Şablon:ku-kîte", | ||
10, | ||
"""[[kîte#ku|Kîtekirin]]: lê·ker""", | ||
) | ||
page_data = parse_page( | ||
self.wxr, | ||
"lêker", | ||
"""== {{ziman|ku}} == | ||
=== Bilêvkirin === | ||
* {{ku-kîte}} | ||
=== Navdêr 1 === | ||
# [[peyv|Peyvên]]""", | ||
) | ||
self.assertEqual(page_data[0]["hyphenation"], "lê·ker") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters