-
Notifications
You must be signed in to change notification settings - Fork 87
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 #945 from xxyzz/it
[it] extract zh and ja example lists, extract translation section
- Loading branch information
Showing
8 changed files
with
360 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"Template:-trad1-": { | ||
"body": "===Traduzione===\n", | ||
"namespace_id": 10, | ||
"need_pre_expand": true | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,98 @@ | ||
from wikitextprocessor import NodeKind, WikiNode | ||
from wikitextprocessor import NodeKind, TemplateNode, WikiNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from ..ruby import extract_ruby | ||
from .models import Example, Sense | ||
|
||
|
||
def extract_example_list_item( | ||
wxr: WiktextractContext, sense: Sense, list_item: WikiNode | ||
wxr: WiktextractContext, sense: Sense, list_item: WikiNode, lang_code: str | ||
) -> None: | ||
example = Example() | ||
for node in list_item.children: | ||
if isinstance(node, WikiNode): | ||
examples = [] | ||
before_italic = True | ||
text_nodes = [] | ||
roman = "" | ||
translation = "" | ||
for index, node in enumerate(list_item.children): | ||
if ( | ||
isinstance(node, TemplateNode) | ||
and node.template_name == "zh-tradsem" | ||
): | ||
examples.extend(extract_zh_tradsem(wxr, node)) | ||
elif isinstance(node, WikiNode): | ||
match node.kind: | ||
case NodeKind.ITALIC: | ||
example.text = clean_node(wxr, sense, node) | ||
if lang_code in ["zh", "ja"]: | ||
if before_italic: | ||
roman = clean_node(wxr, sense, node) | ||
before_italic = False | ||
else: | ||
examples.append( | ||
Example(text=clean_node(wxr, sense, node)) | ||
) | ||
case NodeKind.LIST: | ||
for tr_list_item in node.find_child(NodeKind.LIST_ITEM): | ||
example.translation = clean_node( | ||
translation = clean_node( | ||
wxr, sense, tr_list_item.children | ||
) | ||
case _ if lang_code in ["zh", "ja"]: | ||
if before_italic: | ||
text_nodes.append(node) | ||
elif ( | ||
isinstance(node, str) and lang_code in ["zh", "ja"] and "-" in node | ||
): | ||
translation = clean_node( | ||
wxr, | ||
sense, | ||
wxr.wtp.node_to_wikitext( | ||
[node[node.index("-") + 1 :]] | ||
+ list_item.children[index + 1 :] | ||
), | ||
) | ||
break | ||
elif lang_code in ["zh", "ja"] and len(examples) == 0 and before_italic: | ||
text_nodes.append(node) | ||
|
||
if lang_code in ["zh", "ja"] and len(examples) == 0 and len(text_nodes) > 0: | ||
expanded_nodes = wxr.wtp.parse( | ||
wxr.wtp.node_to_wikitext(text_nodes), expand_all=True | ||
) | ||
example = Example() | ||
example.ruby, node_without_ruby = extract_ruby( | ||
wxr, expanded_nodes.children | ||
) | ||
example.text = ( | ||
clean_node(wxr, sense, node_without_ruby) | ||
.replace(" ", "") | ||
.strip("(") | ||
) | ||
examples.append(example) | ||
|
||
for example in examples: | ||
if roman != "": | ||
example.roman = roman | ||
if translation != "": | ||
example.translation = translation | ||
if example.text != "": | ||
sense.examples.append(example) | ||
|
||
|
||
def extract_zh_tradsem( | ||
wxr: WiktextractContext, t_node: TemplateNode | ||
) -> list[Example]: | ||
# https://it.wiktionary.org/wiki/Template:zh-tradsem | ||
examples = [] | ||
for arg_index in [1, 2]: | ||
arg_value = clean_node( | ||
wxr, None, t_node.template_parameters.get(arg_index, "") | ||
).replace(" ", "") | ||
if arg_value != "": | ||
example = Example(text=arg_value) | ||
if arg_index == 1: | ||
example.tags.append("Traditional Chinese") | ||
elif arg_index == 2: | ||
example.tags.append("Simplified Chinese") | ||
examples.append(example) | ||
|
||
if example.text != "": | ||
sense.examples.append(example) | ||
return examples |
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
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
Oops, something went wrong.