-
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 #744 from xxyzz/pl
[pl] extract linkage sections and fix translation code
- Loading branch information
Showing
6 changed files
with
236 additions
and
39 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,85 @@ | ||
import re | ||
from collections import defaultdict | ||
|
||
from wikitextprocessor.parser import NodeKind, TemplateNode, WikiNode | ||
|
||
from ...page import clean_node | ||
from ...wxr_context import WiktextractContext | ||
from .models import Linkage, WordEntry | ||
from .tags import translate_raw_tags | ||
|
||
LINKAGE_TYPES = { | ||
"antonimy": "antonyms", | ||
"hiperonimy": "hypernyms", | ||
"hiponimy": "hyponyms", | ||
"holonimy": "holonyms", | ||
"meronimy": "meronyms", | ||
"synonimy": "synonyms", | ||
"wyrazy pokrewne": "related", | ||
"związki frazeologiczne": "proverbs", | ||
} | ||
|
||
|
||
def extract_linkage_section( | ||
wxr: WiktextractContext, | ||
page_data: list[WordEntry], | ||
level_node: WikiNode, | ||
linkage_type: str, | ||
lang_code: str, | ||
) -> None: | ||
linkages = defaultdict(list) | ||
for list_item in level_node.find_child_recursively(NodeKind.LIST_ITEM): | ||
process_linakge_list_item(wxr, list_item, linkages) | ||
|
||
for data in page_data: | ||
if data.lang_code == lang_code: | ||
for sense in data.senses: | ||
if sense.sense_index in linkages: | ||
getattr(data, linkage_type).extend( | ||
linkages[sense.sense_index] | ||
) | ||
del linkages[sense.sense_index] | ||
getattr(data, linkage_type).extend(linkages.get("", [])) | ||
|
||
if "" in linkages: | ||
del linkages[""] | ||
for data in page_data: | ||
if data.lang_code == lang_code: | ||
for linkage_list in linkages.values(): | ||
getattr(data, linkage_type).extend(linkage_list) | ||
break | ||
|
||
|
||
def process_linakge_list_item( | ||
wxr: WiktextractContext, | ||
list_item: WikiNode, | ||
linkages: dict[str, list[Linkage]], | ||
) -> None: | ||
raw_tags = [] | ||
sense_index = "" | ||
last_linkage = None | ||
for node in list_item.children: | ||
if isinstance(node, str): | ||
m = re.search(r"\(\d+\.\d+\)", node) | ||
if m is not None: | ||
sense_index = m.group(0).strip("()") | ||
if ";" in node or "•" in node: | ||
raw_tags.clear() | ||
last_linkage = None | ||
elif isinstance(node, TemplateNode): | ||
raw_tag = clean_node(wxr, None, node) | ||
if raw_tag.endswith("."): | ||
if last_linkage is None: | ||
raw_tags.append(raw_tag) | ||
else: | ||
last_linkage.raw_tags.append(raw_tag) | ||
translate_raw_tags(last_linkage) | ||
elif isinstance(node, WikiNode) and node.kind == NodeKind.LINK: | ||
linkage = Linkage( | ||
word=clean_node(wxr, None, node), | ||
sense_index=sense_index, | ||
raw_tags=raw_tags, | ||
) | ||
translate_raw_tags(linkage) | ||
linkages[sense_index].append(linkage) | ||
last_linkage = linkage |
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,60 @@ | ||
from unittest import TestCase | ||
|
||
from wikitextprocessor import Wtp | ||
from wiktextract.config import WiktionaryConfig | ||
from wiktextract.extractor.pl.linkage import extract_linkage_section | ||
from wiktextract.extractor.pl.models import Linkage, Sense, WordEntry | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
class TestPlLinkage(TestCase): | ||
maxDiff = None | ||
|
||
def setUp(self) -> None: | ||
self.wxr = WiktextractContext( | ||
Wtp(lang_code="pl"), | ||
WiktionaryConfig( | ||
dump_file_lang_code="pl", | ||
capture_language_codes=None, | ||
), | ||
) | ||
|
||
def tearDown(self) -> None: | ||
self.wxr.wtp.close_db_conn() | ||
|
||
def test_pies(self): | ||
self.wxr.wtp.start_page("pies") | ||
self.wxr.wtp.add_page("Szablon:neutr", 10, "neutr.") | ||
|
||
root = self.wxr.wtp.parse(""": (1.1) [[czworonożny przyjaciel]] | ||
: (2.1) [[pała]]; {{neutr}} [[policjant]]""") | ||
page_data = [ | ||
WordEntry( | ||
word="pies", | ||
lang="język polski", | ||
lang_code="pl", | ||
pos="noun", | ||
senses=[Sense(sense_index="1.1")], | ||
), | ||
WordEntry( | ||
word="pies", | ||
lang="język polski", | ||
lang_code="pl", | ||
pos="noun", | ||
senses=[Sense(sense_index="2.1")], | ||
), | ||
] | ||
extract_linkage_section(self.wxr, page_data, root, "synonyms", "pl") | ||
self.assertEqual( | ||
page_data[0].synonyms, | ||
[Linkage(word="czworonożny przyjaciel", sense_index="1.1")], | ||
) | ||
self.assertEqual( | ||
page_data[1].synonyms, | ||
[ | ||
Linkage(word="pała", sense_index="2.1"), | ||
Linkage( | ||
word="policjant", raw_tags=["neutr."], sense_index="2.1" | ||
), | ||
], | ||
) |