forked from goodmami/wn
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: merge Wikidata extensions into base lexicons + add function-word POS #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| """Map verbose Wikidata POS labels to short WN-LMF POS codes.""" | ||
| from wn.constants import ( | ||
| ADP, | ||
| ADV, | ||
| CONJ, | ||
| DET, | ||
| INTERROGATIVE, | ||
| INTJ, | ||
| NUM, | ||
| OTHER, | ||
| PART, | ||
| PRON, | ||
| ) | ||
|
|
||
| POS_MAP: dict[str, str] = { | ||
| "pronoun": PRON, | ||
| "personal pronoun": PRON, | ||
| "possessive determiner": PRON, | ||
| "reflexive personal pronoun": PRON, | ||
| "reflexive pronoun": PRON, | ||
| "reciprocal pronoun": PRON, | ||
| "interrogative pronoun": PRON, | ||
| "indefinite pronoun": PRON, | ||
| "relative pronoun": PRON, | ||
| "demonstrative pronoun": PRON, | ||
| "definite pronoun": PRON, | ||
| "pronominal locution": PRON, | ||
| "determiner": DET, | ||
| "definite article": DET, | ||
| "indefinite article": DET, | ||
| "demonstrative determiner": DET, | ||
| "adverbial phrase": ADV, | ||
| "adverbial locution": ADV, | ||
| "adverbial particle": ADV, | ||
| "interrogative adverb": ADV, | ||
| "prepositional adverb": ADV, | ||
| "preposition": ADP, | ||
| "prepositional locution": ADP, | ||
| "conjunction": CONJ, | ||
| "numeral": NUM, | ||
| "interjection": INTJ, | ||
| "interrogative word": INTERROGATIVE, | ||
| "interrogative expression": INTERROGATIVE, | ||
| "grammatical particle": PART, | ||
| "infinitive marker": PART, | ||
| "acronym": OTHER, | ||
| } |
This file contains hidden or 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 hidden or 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,187 @@ | ||
| #!/usr/bin/env python3 | ||
| """Merge a WN-LMF lexicon extension into its base lexicon. | ||
|
|
||
| `wn.add()` stores an extension as a separate lexicon row, which means | ||
| queries against the base lexicon won't see the extension's entries. | ||
| This script loads the extension normally and then rewrites the | ||
| ``lexicon_rowid`` columns of every content table so the extension's | ||
| rows appear under the base lexicon — leaving one lexicon per language | ||
| in the database. | ||
|
|
||
| See https://github.com/goodmami/wn/issues/304 for the original motivation. | ||
|
|
||
| Usage:: | ||
|
|
||
| python merge_extension.py path/to/extension.xml [more.xml ...] | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import re | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
|
|
||
| import wn | ||
| from wn import lmf | ||
| from wn._db import connect | ||
| from wn._util import format_lexicon_specifier | ||
|
|
||
| log = logging.getLogger("wn") | ||
|
|
||
| _FIRST_ENTRY_ID_RE = re.compile(rb'<LexicalEntry\b[^>]*\bid="([^"]+)"') | ||
|
|
||
| # Tables with a ``lexicon_rowid`` column. Keep in sync with wn/schema.sql. | ||
| _LEXICON_OWNED_TABLES = ( | ||
| "entries", | ||
| "forms", | ||
| "pronunciations", | ||
| "tags", | ||
| "synsets", | ||
| "synset_relations", | ||
| "definitions", | ||
| "synset_examples", | ||
| "senses", | ||
| "sense_relations", | ||
| "sense_synset_relations", | ||
| "sense_examples", | ||
| "counts", | ||
| "syntactic_behaviours", | ||
| ) | ||
|
|
||
| # Tables in _LEXICON_OWNED_TABLES that lack an index on lexicon_rowid in | ||
| # the shipped schema. We create a temporary index over the bulk merge so | ||
| # each UPDATE is O(rows-to-update) instead of O(table-size). | ||
| _UNINDEXED_TABLES = ( | ||
| "entries", | ||
| "pronunciations", | ||
| "tags", | ||
| "synsets", | ||
| "synset_relations", | ||
| "definitions", | ||
| "synset_examples", | ||
| "senses", | ||
| "sense_relations", | ||
| "sense_synset_relations", | ||
| "sense_examples", | ||
| "counts", | ||
| ) | ||
|
|
||
|
|
||
| def _get_lexicon_rowid(cur, specifier: str) -> int | None: | ||
| row = cur.execute( | ||
| "SELECT rowid FROM lexicons WHERE specifier = ?", | ||
| (specifier,), | ||
| ).fetchone() | ||
| return row[0] if row else None | ||
|
|
||
|
|
||
| def _first_entry_id(xml_path: Path) -> str | None: | ||
| with open(xml_path, "rb") as fh: | ||
| match = _FIRST_ENTRY_ID_RE.search(fh.read()) | ||
| return match.group(1).decode("utf-8") if match else None | ||
|
|
||
|
|
||
| def _is_already_merged(cur, xml_path: Path, base_rowid: int) -> bool: | ||
| entry_id = _first_entry_id(xml_path) | ||
| if entry_id is None: | ||
| return False | ||
| return ( | ||
| cur.execute( | ||
| "SELECT 1 FROM entries WHERE id = ? AND lexicon_rowid = ?", | ||
| (entry_id, base_rowid), | ||
| ).fetchone() | ||
| is not None | ||
| ) | ||
|
|
||
|
|
||
| def merge_extension(xml_path: Path) -> None: | ||
| """Add *xml_path* and absorb it into its base lexicon.""" | ||
| infos = lmf.scan_lexicons(xml_path) | ||
| if not infos: | ||
| log.warning("%s: no lexicons found, skipping", xml_path) | ||
| return | ||
|
|
||
| info = infos[0] | ||
| extends = info.get("extends") | ||
| if not extends: | ||
| log.info("%s: not a lexicon extension, loading as-is", xml_path) | ||
| wn.add(xml_path, progress_handler=None) | ||
| return | ||
|
|
||
| ext_spec = format_lexicon_specifier(info["id"], info["version"]) | ||
| base_spec = format_lexicon_specifier(extends["id"], extends["version"]) | ||
|
|
||
| conn = connect() | ||
| cur = conn.cursor() | ||
|
|
||
| base_rowid = _get_lexicon_rowid(cur, base_spec) | ||
| if base_rowid is None: | ||
| log.info( | ||
| "skipping %s: base lexicon %s is not in the database", | ||
| ext_spec, | ||
| base_spec, | ||
| ) | ||
| return | ||
|
|
||
| if _is_already_merged(cur, xml_path, base_rowid): | ||
| log.info("%s already merged into %s", ext_spec, base_spec) | ||
| return | ||
|
|
||
| wn.add(xml_path, progress_handler=None) | ||
|
|
||
| ext_rowid = _get_lexicon_rowid(cur, ext_spec) | ||
| if ext_rowid is None: | ||
| raise RuntimeError(f"failed to add extension {ext_spec}") | ||
|
|
||
| with conn: | ||
| for table in _LEXICON_OWNED_TABLES: | ||
| cur.execute( | ||
| f"UPDATE {table} SET lexicon_rowid = ? WHERE lexicon_rowid = ?", | ||
| (base_rowid, ext_rowid), | ||
| ) | ||
| cur.execute( | ||
| "DELETE FROM lexicon_extensions WHERE extension_rowid = ?", | ||
| (ext_rowid,), | ||
| ) | ||
| cur.execute( | ||
| "DELETE FROM lexicon_dependencies WHERE dependent_rowid = ?", | ||
| (ext_rowid,), | ||
| ) | ||
| cur.execute("DELETE FROM lexicons WHERE rowid = ?", (ext_rowid,)) | ||
|
|
||
| log.info("Merged %s into %s", ext_spec, base_spec) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _bulk_merge_indexes(conn): | ||
| for table in _UNINDEXED_TABLES: | ||
| conn.execute( | ||
| f"CREATE INDEX IF NOT EXISTS " | ||
| f"tmp_merge_{table}_lex ON {table}(lexicon_rowid)" | ||
| ) | ||
| try: | ||
| yield | ||
| finally: | ||
| for table in _UNINDEXED_TABLES: | ||
| conn.execute(f"DROP INDEX IF EXISTS tmp_merge_{table}_lex") | ||
|
|
||
|
|
||
| def main(argv: list[str]) -> int: | ||
| if not argv: | ||
| print(__doc__) | ||
| return 1 | ||
|
|
||
| with _bulk_merge_indexes(connect()): | ||
| for arg in argv: | ||
| path = Path(arg) | ||
| if not path.exists(): | ||
| log.warning("skipping missing path: %s", path) | ||
| continue | ||
| merge_extension(path) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| logging.basicConfig(level=logging.INFO, format="%(message)s") | ||
| sys.exit(main(sys.argv[1:])) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-merge duplicates base entries
High Severity
After a successful merge the extension lexicon row is deleted, so a later
merge_extensionrun callswn.addagain and re-inserts the same entry ids under a new extension lexicon. Updatinglexicon_rowidto the base then collides with rows already merged, violatingentriesuniqueness on(id, lexicon_rowid).Reviewed by Cursor Bugbot for commit d856879. Configure here.