Difference between revisions of "Wikipedia Extractor"

From Apertium
Jump to navigation Jump to search
(31 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TOCD}}
 
== Goal ==
 
== Goal ==
   
This tool extracts main text from Wikipedia, producing a text corpus, which is useful for training unsupervised part-of-speech taggers, n-gram language models, etc.
+
This tool extracts main text from xml [[Wikipedia dump]] files (at http://dumps.wikimedia.org/backup-index.html, ideally the "pages-articles" file), producing a text corpus, which is useful for training unsupervised part-of-speech taggers, n-gram language models, etc.
   
  +
It was modified by a number of people, including by BenStobaugh during Google Code-In 2013, and can be cloned from GitHub at [https://github.com/apertium/WikiExtractor https://github.com/apertium/WikiExtractor].
== Tool ==
 
http://code.google.com/p/natural-language-qa/source/browse/MakeCorpus/WikiExtractor.py
 
   
  +
This version is much simpler than the old version. This version auto-removes any formatting and only outputs the text to one file. To use it, simply use the following command in your terminal, where dump.xml is the Wikipedia dump
License GPL-V3.
 
   
  +
$ python3 WikiExtractor.py --infn dump.xml.bz2
== Usage ==
 
   
  +
(Note: If you are on a Mac, make sure that <tt>--</tt> is really two hyphens and not an em-dash like this: &mdash;).
=== 1. Get the script ===
 
   
  +
This will run through all of the articles, get all of the text and put it in wiki.txt. This version also supports compression (BZip2 and Gzip), so you can use <code>dump.xml.bz2</code> or <code>dump.xml.gz</code> instead of <code>dump.xml.</code> You can also compress (Bzip2) the output file by adding <code>--compress</code> to the command.
http://code.google.com/p/natural-language-qa/source/browse/MakeCorpus/WikiExtractor.py
 
   
  +
You can also run <code>python3 WikiExtractor.py --help</code> to get more details.
=== 2. Download the Wikipedia dump file ===
 
   
 
== Steps ==
http://dumps.wikimedia.org/backup-index.html
 
   
  +
Here's a simple step-by-step guide to the above.
Take Chinese as an example, download the file '''zhwiki-20130625-pages-articles.xml.bz2''' on this page http://dumps.wikimedia.org/zhwiki/20130625/. Alternatively, we can download the latest version on this page, http://dumps.wikimedia.org/zhwiki/lastest/
 
   
  +
# Get the <code>WikiExtractor.py</code> script from https://github.com/apertium/WikiExtractor:
=== 3. Use the script ===
 
  +
#: <code>$ wget https://raw.githubusercontent.com/apertium/WikiExtractor/master/WikiExtractor.py</code>
<pre>
 
  +
# Download the Wikipedia dump for the language in question from http://dumps.wikimedia.org/backup-index.html. The below uses a hypothetical file name for a language with the <tt>xyz</tt> code:
mkdir output
 
  +
#: <code>$ wget https://dumps.wikimedia.org/xyzwiki/20210620/xyzwiki-20210620-pages-articles.xml.bz2</code>
 
# Run the script on the Wikipedia dump file:
  +
#: <code>$ python3 WikiExtractor.py --infn xyzwiki-20210620-pages-articles.xml.bz2 --compress</code>
   
  +
This will output a file called <code>wiki.txt.bz2</code>. You will probably want to rename it to something like <code>xyz.wikipedia.20210620.txt.bz2</code>.
bzcat zhwiki-20130625-pages-articles.xml.bz2 | ./WikiExtractor -o output
 
   
  +
==See also==
cat output/*/* > zhwiki.text
 
  +
* [[ Wikipedia dumps ]]
   
</pre>
 
   
  +
[[Category:Resources]]
Optionally, we can use
 
  +
[[Category:Development]]
 
  +
[[Category:Corpora]]
"-c" for compression for saving disk space, and
 
  +
[[Category:Documentation in English]]
 
"-b" for setting specified bytes per output file.
 
 
More information please type "./WikiExtractor --help".
 
 
Ok, let's have a cup of tea and come back an hour later. The output should be '''output/AA/wikiXX''', where wikiXX are the extracted texts.
 
 
=== 4. clean up "<>" tags ===
 
 
We are only one step away from the final text corpus, because there are still links in wikiXX files. Let's use the following tiny script to filter out "<>" tags.
 
 
<pre>
 
 
#! /usr/bin/python
 
# -*- coding:utf-8 -*-
 
import sys
 
import re
 
regex = re.compile(ur"<.*?>")
 
line = sys.stdin.readline()
 
while line != "":
 
line = regex.sub("", line)[ : -1]
 
print line
 
line = sys.stdin.readline()
 
</pre>
 
 
Save the above in a file '''filter.py''', and:
 
 
<pre>
 
python filter.py < zhwiki.text > zhwiki.filter.text
 
</pre>
 
 
=== 5. done :) ===
 

Revision as of 15:51, 25 August 2021

Goal

This tool extracts main text from xml Wikipedia dump files (at http://dumps.wikimedia.org/backup-index.html, ideally the "pages-articles" file), producing a text corpus, which is useful for training unsupervised part-of-speech taggers, n-gram language models, etc.

It was modified by a number of people, including by BenStobaugh during Google Code-In 2013, and can be cloned from GitHub at https://github.com/apertium/WikiExtractor.

This version is much simpler than the old version. This version auto-removes any formatting and only outputs the text to one file. To use it, simply use the following command in your terminal, where dump.xml is the Wikipedia dump

$ python3 WikiExtractor.py --infn dump.xml.bz2

(Note: If you are on a Mac, make sure that -- is really two hyphens and not an em-dash like this: —).

This will run through all of the articles, get all of the text and put it in wiki.txt. This version also supports compression (BZip2 and Gzip), so you can use dump.xml.bz2 or dump.xml.gz instead of dump.xml. You can also compress (Bzip2) the output file by adding --compress to the command.

You can also run python3 WikiExtractor.py --help to get more details.

Steps

Here's a simple step-by-step guide to the above.

  1. Get the WikiExtractor.py script from https://github.com/apertium/WikiExtractor:
    $ wget https://raw.githubusercontent.com/apertium/WikiExtractor/master/WikiExtractor.py
  2. Download the Wikipedia dump for the language in question from http://dumps.wikimedia.org/backup-index.html. The below uses a hypothetical file name for a language with the xyz code:
    $ wget https://dumps.wikimedia.org/xyzwiki/20210620/xyzwiki-20210620-pages-articles.xml.bz2
  3. Run the script on the Wikipedia dump file:
    $ python3 WikiExtractor.py --infn xyzwiki-20210620-pages-articles.xml.bz2 --compress

This will output a file called wiki.txt.bz2. You will probably want to rename it to something like xyz.wikipedia.20210620.txt.bz2.

See also