-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 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,85 @@ | ||
import re | ||
|
||
from mediawiki_langcodes import name_to_code | ||
from wikitextprocessor import LevelNode, NodeKind, TemplateNode, WikiNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .models import Translation, WordEntry | ||
|
||
|
||
def extract_translation_section( | ||
wxr: WiktextractContext, | ||
page_data: list[WordEntry], | ||
level_node: LevelNode, | ||
) -> None: | ||
sense = "" | ||
translations = [] | ||
cats = {} | ||
for node in level_node.children: | ||
if isinstance(node, TemplateNode) and node.template_name == "Trad1": | ||
sense = clean_node(wxr, cats, node.template_parameters.get(1, "")) | ||
elif isinstance(node, WikiNode) and node.kind == NodeKind.LIST: | ||
for list_item in node.find_child(NodeKind.LIST_ITEM): | ||
translations.extend( | ||
extract_translation_list_item(wxr, list_item, sense) | ||
) | ||
|
||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.translations.extend(translations) | ||
data.categories.extend(cats.get("categories", [])) | ||
|
||
|
||
TR_GENDER_TAGS = { | ||
"c": "common", | ||
"f": "feminine", | ||
"m": "masculine", | ||
"n": "neuter", | ||
} | ||
|
||
|
||
def extract_translation_list_item( | ||
wxr: WiktextractContext, list_item: WikiNode, sense: str | ||
) -> list[Translation]: | ||
translations = [] | ||
lang_name = "unknown" | ||
lang_code = "unknown" | ||
before_colon = True | ||
for index, node in enumerate(list_item.children): | ||
if before_colon and isinstance(node, str) and ":" in node: | ||
before_colon = False | ||
lang_name = clean_node(wxr, None, list_item.children[:index]) | ||
for n in list_item.children[:index]: | ||
if isinstance(n, TemplateNode): | ||
lang_code = n.template_name | ||
break | ||
if lang_code == "unknown": | ||
new_lang_code = name_to_code(lang_name, "it") | ||
if new_lang_code != "": | ||
lang_code = new_lang_code | ||
elif not before_colon and isinstance(node, WikiNode): | ||
match node.kind: | ||
case NodeKind.LINK: | ||
word = clean_node(wxr, None, node) | ||
if word != "": | ||
translations.append( | ||
Translation( | ||
word=word, | ||
sense=sense, | ||
lang=lang_name, | ||
lang_code=lang_code, | ||
) | ||
) | ||
case NodeKind.ITALIC: | ||
raw_tag = clean_node(wxr, None, node) | ||
if raw_tag in TR_GENDER_TAGS and len(translations) > 0: | ||
translations[-1].tags.append(TR_GENDER_TAGS[raw_tag]) | ||
elif raw_tag != "" and len(translations) > 0: | ||
translations[-1].raw_tags.append(raw_tag) | ||
elif not before_colon and isinstance(node, str): | ||
m = re.search(r"\((.+)\)", node) | ||
if m is not None and len(translations) > 0: | ||
translations[-1].roman = m.group(1) | ||
|
||
return translations |
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,54 @@ | ||
from unittest import TestCase | ||
|
||
from wikitextprocessor import Wtp | ||
|
||
from wiktextract.config import WiktionaryConfig | ||
from wiktextract.extractor.it.page import parse_page | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
class TestItGloss(TestCase): | ||
maxDiff = None | ||
|
||
def setUp(self) -> None: | ||
self.wxr = WiktextractContext( | ||
Wtp(lang_code="it"), | ||
WiktionaryConfig( | ||
dump_file_lang_code="it", capture_language_codes=None | ||
), | ||
) | ||
|
||
def test_common_lists(self): | ||
self.wxr.wtp.add_page("Template:-it-", 10, "Italiano") | ||
self.wxr.wtp.add_page("Template:ar", 10, "arabo") | ||
data = parse_page( | ||
self.wxr, | ||
"cane", | ||
"""== {{-it-}} == | ||
===Sostantivo=== | ||
# [[animale]] | ||
===Traduzione=== | ||
{{Trad1|animale}} | ||
:*{{ar}}: [[كَلْب]] (kalb) ''m'' | ||
:*[[romagnolo]]: [[chèn]] ''m''""", | ||
) | ||
self.assertEqual( | ||
data[0]["translations"], | ||
[ | ||
{ | ||
"word": "كَلْب", | ||
"lang_code": "ar", | ||
"lang": "arabo", | ||
"roman": "kalb", | ||
"tags": ["masculine"], | ||
"sense": "animale", | ||
}, | ||
{ | ||
"word": "chèn", | ||
"lang_code": "rgn", | ||
"lang": "romagnolo", | ||
"tags": ["masculine"], | ||
"sense": "animale", | ||
}, | ||
], | ||
) |