Skip to main content

WikiPrism

Parse wiki pages and create dictionaries, fast, with Python

Introduction

WikiPrism is a Python library designed to:

  1. Parse a wiki XML file in order to extract its pages - contained within <page> tags

  2. Parse each page so as to extract terms from it - that will be added to a custom Dictionary instance

The above tasks can be easily combined in a sophisticated but user-friendly automated process called extraction pipeline - configurable via a dedicated descriptor object.

WikiPrism focuses on speed - a goal achieved by introducing careful design choices, including the transparent use of parallelism via both multithreading and multiprocessing, while orchestrating several elements provided by the Eos library and without losing architectural simplicity.

In this guide, we are going to see an overview of the library.

Installation

To install WikiPrism, just run:

pip install info.gianlucacosta.wikiprism

or, if you are using Poetry:

poetry add info.gianlucacosta.wikiprism

Then, you'll be able to access the info.gianlucacosta.wikiprism package and its subpackages.

Extracting pages from a wiki source

To perform blazing-fast parsing, WikiPrism relies on customized SAX; more precisely, it expects a wiki file having whatever structure one prefers, as long as its pages comply with the following schema:

<page>
  ...
  <title>The page title</title>
  ...
  <text>The page content</text>
  ...
</page>

Autrement dit, as long as the XML file contains - anywhere - one or more <page> tags having the described structure, WikiPrism will be able to detect them.

In practice

Parsing can be performed via Python's standard functions within the xml.sax namespace - especially:

  • parse() - to parse an XML file

  • parseString() - to parse an XML string

Both functions require a ContentHandler and an optional ErrorHandler - and that's precisely why WikiPrism provides two dedicated classes:

  • WikiContentHandler

  • WikiErrorHandler

In particular, WikiContentHandler's constructor expects:

  • a callback, that receives a Page object - containing just the title and text attributes - as soon as a valid page is found by the SAX parser

  • a ContinuationProvider, that is a () -> bool function periodically called: should it return False, the parsing would end by raising a WikiSaxCanceledException

For more details, please consult the docstrings.

Creating dictionaries

Python has dictionaries - intended as hash maps - but WikiPrism introduces the abstract class named Dictionary[TTerm] as a generic container of terms, which can be language elements like nouns, verbs, conjunctions, or even anything else, according to one's linguistic model... the exact purpose of each dictionary is up to the developer.

Dictionary[TTerm] is a sort of generic repository for terms, via its 2 main abstract methods:

  • add_term(): adds a term to the dictionary - actually writing to the storage technology and maybe preventing duplication

  • execute_command(): runs a command string on the (arbitrary) underlying storage and returns a Result[DictionaryView] - that is, à la Rust, either a DictionaryView object (a table-like DTO with both headers and data rows) or an Exception

Dictionary also has other abstract methods to implement, but it never makes assumptions about its internal data storage; consequently, for convenience, WikiPrism also provides concrete subclasses:

  • InMemoryDictionary[TTerm] - adding terms to a Python set, but unable to perform commands

  • SqliteDictionary[TTerm] - dictionary backed by a SQLite db and passing commands to the related SQL interpreter

For further details, please consult the docstrings.

The extraction pipeline

To combine wiki page extraction and dictionary creation into a performant, automated and easy-to-use process, WikiPrism defines extraction pipelines.

Running an extraction pipeline basically resolves to:

  1. Creating a custom subclass of PipelineStrategy[TTerm] - or its SQLite-oriented subclass, SqlitePipelineStrategy[TTerm]

  2. Invoking the run_extraction_pipeline() function, which only expects an instance of the strategy

run_extraction_pipeline() executes the pipeline in a separate thread (plus its own subthreads and a process pool), therefore it returns a PipelineHandle - an object with the following methods:

  • join() - to wait for its completion - and supporting the same parameters as Thread's join() method

  • request_cancel() - to stop the pipeline in a clean way, as soon as possible

For more details, please consult the docstrings, the tests, and possibly the whole open source Cervantes project.

Related projects

  • Cervantes - WikiPrism applied to Wikcionario in order to explore Spanish morphology with Python

  • eos-core - type-checked and dependency-free modern utility library for Python

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

info_gianlucacosta_wikiprism-2.0.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

info_gianlucacosta_wikiprism-2.0.0-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file info_gianlucacosta_wikiprism-2.0.0.tar.gz.

File metadata

  • Download URL: info_gianlucacosta_wikiprism-2.0.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.28.2 CPython/3.12.3 Linux/6.17.0-1022-azure

File hashes

Hashes for info_gianlucacosta_wikiprism-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f0325a937968737b4bb00a0c99a19d69726a106fd117a19d4887a8bb3b441198
MD5 1cd948d4fef667d87472127af05b3553
BLAKE2b-256 aa42a9592c0995fc19b52bb1b4db0fe41aaca14e853778df26a94d78f43464ee

See more details on using hashes here.

File details

Details for the file info_gianlucacosta_wikiprism-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for info_gianlucacosta_wikiprism-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f8e5915bb108ccc34ac2f5074a73ec4a9e190193b283d9275ce7e01eb33e4c5
MD5 da104acbfb165fd0e42509c649e551b4
BLAKE2b-256 a67fe93fff52e16565df3a43bff7862d6b7340d422a31ca361df35267b8c644c

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.1

2 files

This release

2.0.0 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page