wiki-dump-extractor 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Zulko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,172 @@
1
+ Metadata-Version: 2.4
2
+ Name: wiki_dump_extractor
3
+ Version: 0.1.0
4
+ Summary: A Python package for extracting and processing Wikipedia data
5
+ Author: Zulko
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/zulko/wiki_extractor
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Text Processing :: Markup
12
+ Classifier: Topic :: Scientific/Engineering :: GIS
13
+ Requires-Python: >=3.8
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: lxml>=4.9.0
17
+ Requires-Dist: fastavro>=1.0.0
18
+ Requires-Dist: zstandard>=0.23
19
+ Requires-Dist: pandas>=2.0.0
20
+ Requires-Dist: tqdm>=4.65.0
21
+ Requires-Dist: lmdb>=1.4.1
22
+ Requires-Dist: aiostream>=0.5.0
23
+ Requires-Dist: regex>=2023.12.0
24
+ Provides-Extra: llm
25
+ Requires-Dist: google-cloud-aiplatform; extra == "llm"
26
+ Requires-Dist: google-cloud-storage; extra == "llm"
27
+ Requires-Dist: google-genai; extra == "llm"
28
+ Requires-Dist: pydantic; extra == "llm"
29
+ Requires-Dist: pydantic-ai; extra == "llm"
30
+ Requires-Dist: mwparserfromhell; extra == "llm"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest>=7.0.0; extra == "test"
33
+ Requires-Dist: pytest-cov>=4.0.0; extra == "test"
34
+ Provides-Extra: docs
35
+ Requires-Dist: sphinx>=7.0.0; extra == "docs"
36
+ Requires-Dist: myst-parser>=2.0.0; extra == "docs"
37
+ Requires-Dist: shibuya==2024.10.15; extra == "docs"
38
+ Dynamic: license-file
39
+
40
+ # Wiki dump extractor
41
+
42
+ A python library to extract and analyze pages from a wiki dump.
43
+
44
+ This library is used in particular in the [Landnotes](https://github.com/Zulko/landnotes) project to extract and analyze pages from the Wikipedia dump.
45
+
46
+ The project is hosted on [GitHub](https://github.com/zulko/wiki_dump_extractor) an the HTML documentation is available [here](https://zulko.github.io/wiki_dump_extractor/).
47
+
48
+ ## Scope
49
+
50
+ Make the wikipedia dumps easier to work with:
51
+
52
+ - Extract pages from a wiki dump
53
+ - Be easy to install and run
54
+ - Be fast (can iterate over 50,000 pages / secong using Avro)
55
+ - Be memory efficient
56
+ - Allow for batch processing and parallel processing
57
+
58
+ Provide utilities for page analysis:
59
+
60
+ - Date parsing
61
+ - Section extraction
62
+ - Text cleaning
63
+ - and more.
64
+
65
+ ## Usage
66
+
67
+ To simply iterate over the pages in the dump:
68
+
69
+ ```python
70
+ from wiki_dump_extractor import WikiDumpExtractor
71
+
72
+ dump_file = "enwiki-20220301-pages-articles-multistream.xml.bz2"
73
+ extractor = WikiDumpExtractor(file_path=dump_file)
74
+ for page in extractor.iter_pages(limit=1000):
75
+ print(page.title)
76
+ ```
77
+
78
+ To extract the pages by batches (here we save the pages separate CSV files):
79
+
80
+ ```python
81
+ from wiki_dump_extractor import WikiDumpExtractor
82
+
83
+ dump_file = "enwiki-20220301-pages-articles-multistream.xml.bz2"
84
+ extractor = WikiDumpExtractor(file_path=dump_file)
85
+ batches = extractor.iter_page_batches(batch_size=1000, limit=10)
86
+ for i, batch in enumerate(batches):
87
+ df = pandas.DataFrame([page.to_dict() for page in batch])
88
+ df.to_csv(f"batch_{i}.csv")
89
+ ```
90
+
91
+ ### Converting the dump to Avro
92
+
93
+ There are many reasons why you might want to convert the dump to Avro. The original `xml.bz2` dump is 22Gb but very slow to read from (250/s), the uncompressed dump is 107Gb, relatively fast to read (this library uses lxml which reads thousands of pages per second), however 50% of the pages in there are empty redirect pages.
94
+
95
+ The following code converts the batch to a 28G avro dump that only contains the 12 million real pages, stores redirects in a fast LMDB database, and creates an index for quick page lookups. The operation takes ~40 minutes depending on your machine.
96
+
97
+ ```python
98
+ from wiki_dump_extractor import WikiXmlDumpExtractor
99
+
100
+ file_path = "enwiki-20250201-pages-articles-multistream.xml"
101
+ extractor = WikiXmlDumpExtractor(file_path=file_path)
102
+ ignored_fields = ["timestamp", "page_id", "revision_id", "redirect_title"]
103
+ extractor.extract_pages_to_avro(
104
+ output_file="wiki_dump.avro",
105
+ redirects_db_path="redirects.lmdb", # LMDB database for fast redirect lookups
106
+ ignored_fields=ignored_fields,
107
+ )
108
+ ```
109
+
110
+ Then index the pages for fast lookups:
111
+
112
+ ```python
113
+ from wiki_dump_extractor import WikiAvroDumpExtractor
114
+
115
+ extractor = WikiAvroDumpExtractor(file_path="wiki_dump.avro")
116
+ extractor.index_pages(page_index_db="page_index.lmdb")
117
+ ```
118
+
119
+ Later on, read the Avro file and use redirects and index as follows (reads the 12 million pages in ~3-4 minutes depending on your machine):
120
+
121
+ ```python
122
+ from wiki_dump_extractor import WikiAvroDumpExtractor
123
+
124
+ # Create extractor
125
+ extractor = WikiAvroDumpExtractor(
126
+ file_path="wiki_dump.avro",
127
+ index_dir="page_index.lmdb" # Use the index for faster lookups
128
+ )
129
+
130
+ # Get pages with automatic redirect resolution
131
+ pages = extractor.get_page_batch_by_title(
132
+ ["Page Title 1", "Page Title 2"]
133
+ )
134
+ ```
135
+
136
+ ## Installation
137
+
138
+ ```bash
139
+ pip install wiki-dump-extractor
140
+ ```
141
+
142
+ Or from the source in development mode:
143
+
144
+ ```bash
145
+ pip install -e .
146
+ ```
147
+
148
+ To use the LLM-specific module (that would be mostly if you are on a project like Landnotes), use
149
+
150
+ ```bash
151
+ pip install wiki-dump-extractor[llm]
152
+ ```
153
+
154
+ Or locally:
155
+ ```bash
156
+ pip install -e ".[llm]"
157
+ ```
158
+
159
+ To install with tests, use `pip install -e ".[dev]"` then run the tests with `pytest` in the root directory.
160
+
161
+ ### Requirements for running the LLM utils
162
+
163
+ ```bash
164
+ # Add the Cloud SDK distribution URI as a package source
165
+ echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
166
+
167
+ # Import the Google Cloud public key
168
+ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
169
+
170
+ # Update the package list and install the Cloud SDK
171
+ sudo apt-get update && sudo apt-get install google-cloud-sdk
172
+ ```
@@ -0,0 +1,133 @@
1
+ # Wiki dump extractor
2
+
3
+ A python library to extract and analyze pages from a wiki dump.
4
+
5
+ This library is used in particular in the [Landnotes](https://github.com/Zulko/landnotes) project to extract and analyze pages from the Wikipedia dump.
6
+
7
+ The project is hosted on [GitHub](https://github.com/zulko/wiki_dump_extractor) an the HTML documentation is available [here](https://zulko.github.io/wiki_dump_extractor/).
8
+
9
+ ## Scope
10
+
11
+ Make the wikipedia dumps easier to work with:
12
+
13
+ - Extract pages from a wiki dump
14
+ - Be easy to install and run
15
+ - Be fast (can iterate over 50,000 pages / secong using Avro)
16
+ - Be memory efficient
17
+ - Allow for batch processing and parallel processing
18
+
19
+ Provide utilities for page analysis:
20
+
21
+ - Date parsing
22
+ - Section extraction
23
+ - Text cleaning
24
+ - and more.
25
+
26
+ ## Usage
27
+
28
+ To simply iterate over the pages in the dump:
29
+
30
+ ```python
31
+ from wiki_dump_extractor import WikiDumpExtractor
32
+
33
+ dump_file = "enwiki-20220301-pages-articles-multistream.xml.bz2"
34
+ extractor = WikiDumpExtractor(file_path=dump_file)
35
+ for page in extractor.iter_pages(limit=1000):
36
+ print(page.title)
37
+ ```
38
+
39
+ To extract the pages by batches (here we save the pages separate CSV files):
40
+
41
+ ```python
42
+ from wiki_dump_extractor import WikiDumpExtractor
43
+
44
+ dump_file = "enwiki-20220301-pages-articles-multistream.xml.bz2"
45
+ extractor = WikiDumpExtractor(file_path=dump_file)
46
+ batches = extractor.iter_page_batches(batch_size=1000, limit=10)
47
+ for i, batch in enumerate(batches):
48
+ df = pandas.DataFrame([page.to_dict() for page in batch])
49
+ df.to_csv(f"batch_{i}.csv")
50
+ ```
51
+
52
+ ### Converting the dump to Avro
53
+
54
+ There are many reasons why you might want to convert the dump to Avro. The original `xml.bz2` dump is 22Gb but very slow to read from (250/s), the uncompressed dump is 107Gb, relatively fast to read (this library uses lxml which reads thousands of pages per second), however 50% of the pages in there are empty redirect pages.
55
+
56
+ The following code converts the batch to a 28G avro dump that only contains the 12 million real pages, stores redirects in a fast LMDB database, and creates an index for quick page lookups. The operation takes ~40 minutes depending on your machine.
57
+
58
+ ```python
59
+ from wiki_dump_extractor import WikiXmlDumpExtractor
60
+
61
+ file_path = "enwiki-20250201-pages-articles-multistream.xml"
62
+ extractor = WikiXmlDumpExtractor(file_path=file_path)
63
+ ignored_fields = ["timestamp", "page_id", "revision_id", "redirect_title"]
64
+ extractor.extract_pages_to_avro(
65
+ output_file="wiki_dump.avro",
66
+ redirects_db_path="redirects.lmdb", # LMDB database for fast redirect lookups
67
+ ignored_fields=ignored_fields,
68
+ )
69
+ ```
70
+
71
+ Then index the pages for fast lookups:
72
+
73
+ ```python
74
+ from wiki_dump_extractor import WikiAvroDumpExtractor
75
+
76
+ extractor = WikiAvroDumpExtractor(file_path="wiki_dump.avro")
77
+ extractor.index_pages(page_index_db="page_index.lmdb")
78
+ ```
79
+
80
+ Later on, read the Avro file and use redirects and index as follows (reads the 12 million pages in ~3-4 minutes depending on your machine):
81
+
82
+ ```python
83
+ from wiki_dump_extractor import WikiAvroDumpExtractor
84
+
85
+ # Create extractor
86
+ extractor = WikiAvroDumpExtractor(
87
+ file_path="wiki_dump.avro",
88
+ index_dir="page_index.lmdb" # Use the index for faster lookups
89
+ )
90
+
91
+ # Get pages with automatic redirect resolution
92
+ pages = extractor.get_page_batch_by_title(
93
+ ["Page Title 1", "Page Title 2"]
94
+ )
95
+ ```
96
+
97
+ ## Installation
98
+
99
+ ```bash
100
+ pip install wiki-dump-extractor
101
+ ```
102
+
103
+ Or from the source in development mode:
104
+
105
+ ```bash
106
+ pip install -e .
107
+ ```
108
+
109
+ To use the LLM-specific module (that would be mostly if you are on a project like Landnotes), use
110
+
111
+ ```bash
112
+ pip install wiki-dump-extractor[llm]
113
+ ```
114
+
115
+ Or locally:
116
+ ```bash
117
+ pip install -e ".[llm]"
118
+ ```
119
+
120
+ To install with tests, use `pip install -e ".[dev]"` then run the tests with `pytest` in the root directory.
121
+
122
+ ### Requirements for running the LLM utils
123
+
124
+ ```bash
125
+ # Add the Cloud SDK distribution URI as a package source
126
+ echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
127
+
128
+ # Import the Google Cloud public key
129
+ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
130
+
131
+ # Update the package list and install the Cloud SDK
132
+ sudo apt-get update && sudo apt-get install google-cloud-sdk
133
+ ```
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["setuptools>=42", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "wiki_dump_extractor"
7
+ version = "0.1.0"
8
+ description = "A Python package for extracting and processing Wikipedia data"
9
+ readme = "README.md"
10
+ authors = [{ name = "Zulko" }]
11
+ license = { text = "MIT" }
12
+ classifiers = [
13
+ "Programming Language :: Python :: 3",
14
+ "License :: OSI Approved :: MIT License",
15
+ "Operating System :: OS Independent",
16
+ "Topic :: Text Processing :: Markup",
17
+ "Topic :: Scientific/Engineering :: GIS",
18
+ ]
19
+ requires-python = ">=3.8"
20
+ dependencies = [
21
+ "lxml>=4.9.0",
22
+ "fastavro>=1.0.0",
23
+ "zstandard>=0.23",
24
+ "pandas>=2.0.0",
25
+ "tqdm>=4.65.0",
26
+ "lmdb>=1.4.1",
27
+ "aiostream>=0.5.0",
28
+ "regex>=2023.12.0",
29
+ ]
30
+
31
+ [project.optional-dependencies]
32
+ llm = [
33
+ "google-cloud-aiplatform",
34
+ "google-cloud-storage",
35
+ "google-genai",
36
+ "pydantic",
37
+ "pydantic-ai",
38
+ "mwparserfromhell",
39
+ ]
40
+ test = ["pytest>=7.0.0", "pytest-cov>=4.0.0"]
41
+
42
+ docs = ["sphinx>=7.0.0", "myst-parser>=2.0.0", "shibuya==2024.10.15"]
43
+
44
+ [project.urls]
45
+ "Homepage" = "https://github.com/zulko/wiki_extractor"
46
+
47
+ [tool.setuptools]
48
+ package-dir = { "" = "src" }
49
+ packages = ["wiki_dump_extractor"]
50
+
51
+ [tool.pytest.ini_options]
52
+ testpaths = ["test"]
53
+ python_files = "test_*.py"
54
+ python_functions = "test_*"
55
+ addopts = "--cov=wiki_dump_extractor"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,14 @@
1
+ """My Project package."""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from .wiki_dump_extractor import WikiXmlDumpExtractor, WikiAvroDumpExtractor
6
+ from .wiki_sql_extractor import WikiSqlExtractor
7
+ from .download_utils import download_file
8
+
9
+ __all__ = [
10
+ "WikiXmlDumpExtractor",
11
+ "WikiAvroDumpExtractor",
12
+ "WikiSqlExtractor",
13
+ "download_file",
14
+ ]