Skip to main content

Build status

mwedittypes

Edit diffs and type detection for Wikipedia. The goal is to transform unstructured edits to Wikipedia articles into a structured summary of what actions were taken in the edit.

Installation

You can install mwedittypes with pip:

$ pip install mwedittypes

Example

If one revision of wikitext is as follows:

{{Short description|Austrian painter}}
'''Karl Josef Aigen''' (8 October 1684 – 22 October 1762) was a landscape painter, born at Olomouc.

and a second revision of wikitext is as follows:

{{Short description|Austrian landscape painter}}
'''Karl Josef Aigen''' (8 October 1684 – 22 October 1762) was a landscape painter, born at [[Olomouc]].

The changes that happened would be:

  • The addition of landscape to the short description template -- this would be registered as a Template change.
  • The changing of Olomouc to a Wikilink.
  • Notably, despite this change to the template and addition of a link, the "Text" of the article has not changed.

This repository would return this in the following structure: {'Template':{'change':1}, 'Wikilink':{'insert':1}.

Basic Usage

>>> from mwedittypes import EditTypes
>>> prev_wikitext = '{{Short description|Austrian painter}}'
>>> curr_wikitext = '{{Short description|Austrian [[landscape painter]]}}'
>>> et = EditTypes(prev_wikitext, curr_wikitext, lang='en', timeout=5)
>>> et.get_diff()
{'Wikilink': {'insert': 1}, 'Template': {'change': 1}}

Development

We are happy to receive contributions though will default to keeping the code here relatively general (not overly customized to individual use-cases). Please reach out or open an issue for the changes you would like to merge so that we can discuss beforehand.

Code Summary

The code for computing diffs and running edit-type detection can be found in two files:

  • mwedittypes/tree_differ.py: this is the first stage of the diffing pipeline that detects high-level changes.
  • mwedittypes/node_differ.py: this is the second stage of the diffing pipeline that takes the tree_differ output, further processes it, and counts up the edit types.

While the diffing/counting is not trivial, the trickiest part of the process is correctly parsing the wikitext into nodes (Templates, Wikilinks, etc.). This is almost all done via the amazing mwparserfromhell library with a few tweaks in the tree differ:

  • We use link namespace prefixes -- e.g., Category:, Image: -- to separate out categories and media from other wikilinks.
  • We identify some additional media files that are transcluded via templates -- e.g., infoboxes -- or gallery tags.
  • We also add some custom logic for parsing <gallery> tags to identify nested links, etc., which otherwise are treated as text by mwparserfromhell.
  • We use custom logic for converting wikitext into text to best match what words show up in the text of the article.

To accurately, but efficiently, describe the scale of textual changes in edits, we also use some regexes and heuristics to describe how much text was changed in an edit in the node differ. This is generally the toughest part of diffing text but because we do not need to visually describe the diff, just estimate the scale of how much changed, we can use relatively simple methods. To do this, we break down text changes into five categories and identify how much of each changed: paragraphs, sentences, words, punctuation, and whitespace.

Testing

The tests for node/tree differs are contained within the tests directory. They can be run via pytest. We are not even close to full coverage yet given the numerous node types (template, text, etc.) and four actions (insert/remove/change/move) and varying languages for e.g., Text or Category/Media nodes, but we are working on expanding coverage.

Releases

When a release is ready, there are a few simple steps to take:

  • Update the version number in setup.py and mwedittypes/__init__.py per semantic versioning and push to Github.
  • Create a tag with the same version number (easiest to do this as part of the next step on Github).
  • Make a Github release with the new version and summary of changes. This will trigger the publish to PyPi action.

Troubleshooting:

  • If something fails, you can always rollback the release (Github UI) and delete the associated tag (on your local repository: git tag -d [tag]; git push origin :[tag]) and retry.
  • This relies on a PyPi secret so you must have correct privileges on the Github repo.

Documentation

Release files for mwedittypes 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mwedittypes 1.1.0
File Size Uploaded
mwedittypes-1.1.0.tar.gz 29.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mwedittypes 1.1.0
File Interpreter ABI Platform
mwedittypes-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 57.5 kB

Release files / mwedittypes-1.1.0.tar.gz

Download URL mwedittypes-1.1.0.tar.gz
Size 29.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b24a4d6b618f1c18254f502643cf53ccf3a236d43fcf17ff31b00d71d06d334e
BLAKE2b-256 checksum
How to use checksums
734a14d7286f9b8a2a818d5fabf20b8c13a9443cf6263915b48ff61824aa8d7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release files / mwedittypes-1.1.0-py3-none-any.whl

Download URL mwedittypes-1.1.0-py3-none-any.whl
Size 28.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6298d65bc05f90c70833b2f9199ef22fd6da1fe2f27e47191d02d4a1094da9d0
BLAKE2b-256 checksum
How to use checksums
bbcf88907b66a451c76920ffd1759f46f3e4adbcb64b4c718abec7b24a92d2ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release history Release notifications | RSS feed

3.1.0

2 release files

3.0.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

This release

1.1.0 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release 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