-
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 #946 from xxyzz/it
[it] extract forms line, etymology, pronunciation sections
- Loading branch information
Showing
11 changed files
with
394 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from wikitextprocessor.parser import LEVEL_KIND_FLAGS, LevelNode, NodeKind | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .models import Example, WordEntry | ||
|
||
|
||
def extract_etymology_section( | ||
wxr: WiktextractContext, page_data: list[WordEntry], level_node: LevelNode | ||
) -> None: | ||
etymology_texts = [] | ||
for list_node in level_node.find_child(NodeKind.LIST): | ||
for list_item in list_node.find_child(NodeKind.LIST_ITEM): | ||
e_str = clean_node(wxr, None, list_item.children) | ||
if e_str != "": | ||
etymology_texts.append(e_str) | ||
|
||
if len(etymology_texts) == 0: | ||
e_str = clean_node( | ||
wxr, None, list(level_node.invert_find_child(LEVEL_KIND_FLAGS)) | ||
) | ||
if e_str != "": | ||
etymology_texts.append(e_str) | ||
|
||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.etymology_texts.extend(etymology_texts) | ||
|
||
|
||
def extract_citation_section( | ||
wxr: WiktextractContext, page_data: list[WordEntry], level_node: LevelNode | ||
) -> None: | ||
examples = [] | ||
for t_node in level_node.find_child(NodeKind.TEMPLATE): | ||
if t_node.template_name.lower() == "quote": | ||
example = Example() | ||
example.text = clean_node( | ||
wxr, None, t_node.template_parameters.get(1, "") | ||
) | ||
example.ref = clean_node( | ||
wxr, None, t_node.template_parameters.get(2, "") | ||
) | ||
if example.text != "": | ||
examples.append(example) | ||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.etymology_examples.extend(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from wikitextprocessor import TemplateNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .models import Form, WordEntry | ||
|
||
|
||
def extract_tabs_template( | ||
wxr: WiktextractContext, word_entry: WordEntry, node: TemplateNode | ||
) -> None: | ||
# https://it.wiktionary.org/wiki/Template:Tabs | ||
tags = [ | ||
["masculine", "singular"], | ||
["masculine", "plural"], | ||
["feminine", "singular"], | ||
["feminine", "plural"], | ||
] | ||
for arg_name in range(1, 5): | ||
arg_value = clean_node( | ||
wxr, None, node.template_parameters.get(arg_name, "") | ||
) | ||
if arg_value not in ["", wxr.wtp.title]: | ||
form = Form(form=arg_value, tags=tags[arg_name - 1]) | ||
word_entry.forms.append(form) |
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,47 @@ | ||
from wikitextprocessor import LevelNode, NodeKind | ||
|
||
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_hyphenation_section( | ||
wxr: WiktextractContext, page_data: list[WordEntry], level_node: LevelNode | ||
) -> None: | ||
hyphenation = "" | ||
for list_node in level_node.find_child(NodeKind.LIST): | ||
for list_item in list_node.find_child(NodeKind.LIST_ITEM): | ||
hyphenation = clean_node(wxr, None, list_item.children) | ||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.hyphenation = hyphenation | ||
|
||
|
||
def extract_pronunciation_section( | ||
wxr: WiktextractContext, page_data: list[WordEntry], level_node: LevelNode | ||
) -> None: | ||
sounds = [] | ||
for t_node in level_node.find_child(NodeKind.TEMPLATE): | ||
match t_node.template_name.lower(): | ||
case "ipa": | ||
ipa = clean_node( | ||
wxr, None, t_node.template_parameters.get(1, "") | ||
) | ||
if ipa != "": | ||
sounds.append(Sound(ipa=ipa)) | ||
case "audio": | ||
sound_file = clean_node( | ||
wxr, None, t_node.template_parameters.get(1, "") | ||
) | ||
if sound_file != "": | ||
if len(sounds) > 0: | ||
set_sound_file_url_fields(wxr, sound_file, sounds[-1]) | ||
else: | ||
sound = Sound() | ||
set_sound_file_url_fields(wxr, sound_file, sound) | ||
sounds.append(sound) | ||
|
||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.sounds.extend(sounds) |
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,52 @@ | ||
from wikitextprocessor import NodeKind, TemplateNode, WikiNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .inflection import extract_tabs_template | ||
from .models import Form, WordEntry | ||
|
||
|
||
def extract_tag_form_line_nodes( | ||
wxr: WiktextractContext, word_entry: WordEntry, nodes: list[WikiNode | str] | ||
) -> None: | ||
# https://it.wiktionary.org/wiki/Wikizionario:Manuale_di_stile#Genere_e_numero,_declinazione_o_paradigma | ||
for node in nodes: | ||
if isinstance(node, WikiNode) and node.kind == NodeKind.ITALIC: | ||
extract_italic_tag_node(wxr, word_entry, node) | ||
elif isinstance(node, TemplateNode): | ||
match node.template_name.lower(): | ||
case "tabs": | ||
extract_tabs_template(wxr, word_entry, node) | ||
case "linkp": | ||
form = clean_node( | ||
wxr, None, node.template_parameters.get(1, "") | ||
) | ||
if form != "": | ||
word_entry.forms.append( | ||
Form(form=form, tags=["plural"]) | ||
) | ||
|
||
|
||
ITALIC_TAGS = { | ||
"c": "common", | ||
"coll": "collective", | ||
"f": "feminine", | ||
"m": "masculine", | ||
"n": "neuter", | ||
"pl": "plural", | ||
"sing": "singular", | ||
"prom": "common", | ||
"inv": "invariable", | ||
} | ||
|
||
|
||
def extract_italic_tag_node( | ||
wxr: WiktextractContext, word_entry: WordEntry, node: WikiNode | ||
) -> None: | ||
# https://it.wiktionary.org/wiki/Wikizionario:Genere | ||
italic_str = clean_node(wxr, None, node) | ||
for raw_tag in italic_str.split(): | ||
if raw_tag in ITALIC_TAGS: | ||
word_entry.tags.append(ITALIC_TAGS[raw_tag]) | ||
else: | ||
word_entry.raw_tags.append(raw_tag) |
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,62 @@ | ||
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_quote_template(self): | ||
self.wxr.wtp.add_page("Template:-it-", 10, "Italiano") | ||
data = parse_page( | ||
self.wxr, | ||
"cane", | ||
"""== {{-it-}} == | ||
===Sostantivo=== | ||
# {{Term|mammalogia|it}} [[animale]] | ||
===Etimologia / Derivazione=== | ||
dal latino canis | ||
====Citazione==== | ||
{{Quote | ||
|Cane affamato non teme bastone | ||
|[[q:Giovanni Verga|Giovanni Verga]]}}""", | ||
) | ||
self.assertEqual(data[0]["etymology_texts"], ["dal latino canis"]) | ||
self.assertEqual( | ||
data[0]["etymology_examples"], | ||
[ | ||
{ | ||
"text": "Cane affamato non teme bastone", | ||
"ref": "Giovanni Verga", | ||
} | ||
], | ||
) | ||
|
||
def test_list(self): | ||
self.wxr.wtp.add_page("Template:-la-", 10, "Latino") | ||
data = parse_page( | ||
self.wxr, | ||
"cane", | ||
"""== {{-it-}} == | ||
===Sostantivo, forma flessa=== | ||
# {{Term|mammalogia|it}} [[animale]] | ||
===Etimologia / Derivazione=== | ||
* (sostantivo) vedi [[canis#Latino|canis]] | ||
* (voce verbale) vedi [[cano#Latino|canō]]""", | ||
) | ||
self.assertEqual( | ||
data[0]["etymology_texts"], | ||
["(sostantivo) vedi canis", "(voce verbale) vedi canō"], | ||
) |
Oops, something went wrong.