-
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 #935 from xxyzz/pt
[pt] extract synonyms, cognates and etymology sections
- Loading branch information
Showing
8 changed files
with
267 additions
and
14 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,43 @@ | ||
from wikitextprocessor.parser import ( | ||
LEVEL_KIND_FLAGS, | ||
LevelNode, | ||
NodeKind, | ||
WikiNode, | ||
) | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .models import WordEntry | ||
|
||
|
||
def extract_etymology_section( | ||
wxr: WiktextractContext, | ||
page_data: list[WordEntry], | ||
level_node: LevelNode, | ||
) -> None: | ||
cats = {} | ||
e_nodes = [] | ||
e_texts = [] | ||
for node in level_node.children: | ||
if isinstance(node, WikiNode) and node.kind in LEVEL_KIND_FLAGS: | ||
break | ||
elif isinstance(node, WikiNode) and node.kind == NodeKind.LIST: | ||
e_text = clean_node(wxr, cats, e_nodes).lstrip(": ") | ||
if e_text != "": | ||
e_texts.append(e_text) | ||
e_nodes.clear() | ||
for list_item in node.find_child(NodeKind.LIST_ITEM): | ||
e_text = clean_node(wxr, cats, list_item.children) | ||
if e_text != "": | ||
e_texts.append(e_text) | ||
else: | ||
e_nodes.append(node) | ||
|
||
if len(e_nodes) > 0: | ||
e_text = clean_node(wxr, cats, e_nodes).lstrip(": ") | ||
if e_text != "": | ||
e_texts.append(e_text) | ||
for data in page_data: | ||
if data.lang_code == page_data[-1].lang_code: | ||
data.etymology_texts.extend(e_texts) | ||
data.categories.extend(cats.get("categories", [])) |
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
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,49 @@ | ||
from unittest import TestCase | ||
|
||
from wikitextprocessor import Wtp | ||
|
||
from wiktextract.config import WiktionaryConfig | ||
from wiktextract.extractor.pt.page import parse_page | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
class TestPtEtymology(TestCase): | ||
maxDiff = None | ||
|
||
def setUp(self) -> None: | ||
conf = WiktionaryConfig( | ||
dump_file_lang_code="pt", | ||
capture_language_codes=None, | ||
) | ||
self.wxr = WiktextractContext( | ||
Wtp( | ||
lang_code="pt", | ||
parser_function_aliases=conf.parser_function_aliases, | ||
), | ||
conf, | ||
) | ||
|
||
def test_list(self): | ||
self.wxr.wtp.add_page("Predefinição:-pt-", 10, "Português") | ||
self.wxr.wtp.add_page( | ||
"Predefinição:etimo2", | ||
10, | ||
""":[[Categoria:Entrada de étimo latino (Português)]]Do [[latim]] ''[[oculus#la|oculus]]''<small><sup> ([[:la:oculus|<span title="ver no Wikcionário em latim">la</span>]])</sup></small>.""", | ||
) | ||
data = parse_page( | ||
self.wxr, | ||
"olho", | ||
"""={{-pt-}}= | ||
==Substantivo== | ||
# órgão | ||
==Etimologia== | ||
{{etimo2|la|oculus|pt}} | ||
:* '''Datação''': [[w:século XIII|século XIII]]""", | ||
) | ||
self.assertEqual( | ||
data[0]["etymology_texts"], | ||
["Do latim oculus⁽ˡᵃ⁾.", "Datação: século XIII"], | ||
) | ||
self.assertEqual( | ||
data[0]["categories"], ["Entrada de étimo latino (Português)"] | ||
) |
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