corpora.wikicorpus – Corpus from a Wikipedia dump — gensim
Description: Efficient topic modelling in Python

### Navigation

- index
- modules |
- next |
- previous |
- Gensim home|
- Tutorials|
- Support|
- Contribute|
- API reference»
- API Reference »

# `corpora.wikicorpus` – Corpus from a Wikipedia dump

USAGE: %(program)s WIKI_XML_DUMP OUTPUT_PREFIX [VOCABULARY_SIZE]

> Convert articles from a Wikipedia dump to (sparse) vectors. The input is a bz2-compressed dump of Wikipedia articles, in XML format.

This actually creates three files:

- OUTPUT_PREFIX_wordids.txt: mapping between words and their integer ids
- OUTPUT_PREFIX_bow.mm: bag-of-words (word counts) representation, in Matrix Matrix format
- OUTPUT_PREFIX_tfidf.mm: TF-IDF representation

The output Matrix Market files can then be compressed (e.g., by bzip2) to save disk space; gensim’s corpus iterators can work with compressed input, too.

VOCABULARY_SIZE controls how many of the most frequent words to keep (after removing tokens that appear in more than 10%% of all documents). Defaults to 50,000.

If you have the pattern package installed, this script will use a fancy lemmatization to get a lemma of each token (instead of plain alphabetic tokenizer).

Example: ./wikicorpus.py ~/gensim/results/enwiki-latest-pages-articles.xml.bz2 ~/gensim/results/wiki_en

class `gensim.corpora.wikicorpus.` `VocabTransform`(old2new, id2token=None)

  Remap feature ids to new values.

  Given a mapping between old ids and new ids (some old ids may be missing = these features are to be discarded), this will wrap a corpus so that iterating over VocabTransform[corpus] returns the same vectors but with the new ids.

  Old features that have no counterpart in the new ids are discarded. This can be used to filter vocabulary of a corpus “online”:

      >>> old2new = dict((oldid, newid) for newid, oldid in enumerate(ids_you_want_to_keep))
      >>> vt = VocabTransform(old2new)
      >>> for vec_with_new_ids in vt[corpus_with_old_ids]:
      >>>     ...

  classmethod `load`(fname)

    Load a previously saved object from file (also see save).

  `save`(fname)

    Save the object to file via pickling (also see load).

class `gensim.corpora.wikicorpus.` `WikiCorpus`(fname, no_below=20, keep_words=50000, dictionary=None)

  Treat a wikipedia articles dump (*articles.xml.bz2) as a (read-only) corpus.

  The documents are extracted on-the-fly, so that the whole (massive) dump can stay compressed on disk.

      >>> wiki = WikiCorpus('enwiki-20100622-pages-articles.xml.bz2') # create word->word_id mapping, takes almost 8h
      >>> wiki.saveAsText('wiki_en_vocab200k') # another 8h, creates a file in MatrixMarket format plus file with id->word

  Initialize the corpus. This scans the corpus once, to determine its vocabulary (only the first keep_words most frequent words that appear in at least noBelow documents are kept).

  `get_texts`(return_raw=False)

    Iterate over the dump, returning text version of each article.

    Only articles of sufficient length are returned (short articles & redirects etc are ignored).

    Note that this iterates over the texts; if you want vectors, just use the standard corpus interface instead of this function:

        >>> for vec in wiki_corpus:
        >>>     print vec

  classmethod `load`(fname)

    Load a previously saved object from file (also see save).

  `save`(fname)

    Save the object to file via pickling (also see load).

  static `save_corpus`(fname, corpus, id2word=None)

    Save an existing corpus to disk.

    Some formats also support saving the dictionary (feature_id->word mapping), which can in this case be provided by the optional id2word parameter.

        >>> MmCorpus.save_corpus('file.mm', corpus)

    Some corpora also support an index of where each document begins, so that the documents on disk can be accessed in O(1) time (see the corpora.IndexedCorpus base class). In this case, save_corpus is automatically called internally by serialize, which does save_corpus plus saves the index at the same time, so you want to store the corpus with:

        >>> MmCorpus.serialize('file.mm', corpus) # stores index as well, allowing random access to individual documents

    Calling serialize() is preferred to calling save_corpus().

`gensim.corpora.wikicorpus.` `filter_wiki`(raw)

  Filter out wiki mark-up from raw, leaving only text. raw is either unicode or utf-8 encoded string.

`gensim.corpora.wikicorpus.` `tokenize`(content)

  Tokenize a piece of text from wikipedia. The input string content is assumed to be mark-up free (see filter_wiki()).

  Return list of tokens as utf8 bytestrings. Ignore words shorted than 2 or longer that 15 characters (not bytes!).

### Navigation

- index
- modules |
- next |
- previous |
- Gensim home|
- Tutorials|
- Support|
- Contribute|
- API reference»
- API Reference »