-
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 #374 from xxyzz/fr
Extract French Wiktionary notes section
- Loading branch information
Showing
14 changed files
with
102 additions
and
12 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
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,47 @@ | ||
from typing import Any, Dict, List | ||
|
||
from wikitextprocessor import NodeKind, WikiNode | ||
from wikitextprocessor.parser import TemplateNode | ||
from wiktextract.page import clean_node | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
def extract_note( | ||
wxr: WiktextractContext, | ||
page_data: List[Dict[str, Any]], | ||
level_node: WikiNode, | ||
) -> None: | ||
# Save paragraph and list item texts to a list of string. | ||
note_paragraph_nodes = [] | ||
for child in level_node.children: | ||
if isinstance(child, TemplateNode) and child.template_name.startswith( | ||
"note-" | ||
): | ||
process_note_template(wxr, page_data, child) | ||
continue | ||
if isinstance(child, WikiNode) and child.kind == NodeKind.LIST: | ||
for list_item_node in child.find_child(NodeKind.LIST_ITEM): | ||
note_text = clean_node( | ||
wxr, page_data[-1], list_item_node.children | ||
) | ||
if len(note_text) > 0: | ||
page_data[-1]["notes"].append(note_text) | ||
continue | ||
|
||
note_paragraph_nodes.append(child) | ||
if isinstance(child, str) and child.endswith("\n"): | ||
note_text = clean_node(wxr, page_data[-1], note_paragraph_nodes) | ||
if len(note_text) > 0: | ||
page_data[-1]["notes"].append(note_text) | ||
note_paragraph_nodes.clear() | ||
|
||
|
||
def process_note_template( | ||
wxr: WiktextractContext, | ||
page_data: List[Dict[str, Any]], | ||
template_node: TemplateNode, | ||
) -> None: | ||
expaned_template = wxr.wtp.parse( | ||
wxr.wtp.node_to_wikitext(template_node), expand_all=True | ||
) | ||
extract_note(wxr, page_data, expaned_template) |
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,38 @@ | ||
import unittest | ||
from collections import defaultdict | ||
|
||
from wikitextprocessor import Wtp | ||
|
||
from wiktextract.config import WiktionaryConfig | ||
from wiktextract.extractor.fr.note import extract_note | ||
from wiktextract.wxr_context import WiktextractContext | ||
|
||
|
||
class TestNotes(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.wxr = WiktextractContext( | ||
Wtp(lang_code="fr"), WiktionaryConfig(dump_file_lang_code="fr") | ||
) | ||
|
||
def tearDown(self) -> None: | ||
self.wxr.wtp.close_db_conn() | ||
|
||
def test_list_notes(self): | ||
# list created from template "note-féminisation" | ||
# https://fr.wiktionary.org/wiki/autrice | ||
self.wxr.wtp.add_page("Modèle:note-féminisation", 10, "* list 1\n* list 2") | ||
self.wxr.wtp.start_page("autrice") | ||
nodes = self.wxr.wtp.parse("""==== {{S|notes}} ==== | ||
paragrapy 1 | ||
{{note-féminisation}}""") | ||
page_data = [defaultdict(list)] | ||
extract_note(self.wxr, page_data, nodes.children[0]) | ||
self.assertEqual(page_data, [ | ||
{ | ||
"notes": [ | ||
"paragrapy 1", | ||
"list 1", | ||
"list 2" | ||
] | ||
} | ||
]) |