Morfessor 2.0

repository·master·Indexed 42 days ago

https://github.com/aalto-speech/morfessor

A Python-based implementation and extension of the Morfessor baseline used for morphological segmentation. It provides tools for training models via batch and online methods, segmenting test data, and evaluating models against gold standards. The toolkit includes command-line utilities such as `morfessor-train`, `morfessor-segment`, and `morfessor-evaluate`, and supports multiple model formats including binary, reduced binary, and legacy Morfessor 1.0 style text models.

Tokens
4.4K
Snippets
15
Records
23
Agent score
53%

What's inside Morfessor

  1. Morfessor Library Interface Overview

    master

    Morfessor 2.0 provides a Python library interface for integration into other Python applications. The interface is organized into three main functional areas:

    • IO (morfessor.io): Handles reading and writing data, including corpus files, annotation files (gold standards), and binary model files.
    • Model classes (morfessor.baseline): Contains the core model implementations (e.g., BaselineModel) used for training and segmentation.
    • Evaluation classes (morfessor.evaluation): Provides tools for measuring model performance and conducting statistical significance tests (e.g., MorfessorEvaluation, WilcoxonSignedRank).
  2. Understand Morfessor 2.0 terminology

    master

    Morfessor 2.0 is designed for general string segmentation tasks. To use the tool correctly, understand its core abstractions which are independent of the specific task (e.g., morphology or chunking):

    • Compounds: The largest sequences that a single construction can hold (e.g., word forms in morphology, or sentences in chunking).
    • Constructions: The set of patterns found by the algorithm that describe the training corpus efficiently and accurately (e.g., morphs in morphology, or phrases in chunking).
    • Atoms: The smallest constituent pieces of both constructions and compounds (e.g., characters in morphology, or words in chunking).
  3. Understand Morfessor 2.0 model file formats

    master

    Morfessor 2.0 uses three primary model formats depending on your needs for storage size and functionality:

    1. Binary model: The standard format. It is a pickled BaselineModel object containing all training data, annotation data, and weights.
      • Warning: Pickled models are sensitive to bitrot and Python version incompatibilities. Future versions of Morfessor may not support older binary models.
    2. Reduced Binary model: A significantly smaller version of the binary model. It contains only the information required for (nbest) Viterbi segmentation of new words. Note that you cannot perform model-modifying actions on a reduced model.
    3. Morfessor 1.0 style text model: A legacy text-based format supported for backward compatibility. Each line contains a count followed by one or more constructions separated by +.
    10 kahvi + kakku
    5 kahvi + kilo + n
    24 kahvi + kone + emme
  4. Use Random Initialization for training

    master
    Random initialization splits all compounds randomly, where each possible boundary is treated as a split with a specified probability. Selecting an appropriate random initialization parameter is crucial for finding local optima; ensure the split probability is sufficiently high.
  5. Understand Morfessor training methods

    master

    Morfessor supports different training paradigms depending on your data and goals:

    • Batch Training: Each epoch is an iteration over the full training data. Epochs repeat until model cost converges. All data must be loaded before training starts.
    • Online Training: The model is updated while data is being read. This is useful for rapid prototyping. Data is processed only once, so it is recommended to run batch training after online training.
    • Recursive Training: For each compound, the current split is removed, and all possible splits are tried recursively. The best split is then selected.
    • Local Viterbi Training: Compounds are processed sequentially. Each compound is removed from the corpus, segmented using Viterbi segmentation, and the result is put back into the model. Requires a non-zero --viterbi-smoothing parameter to allow new constructions.
  6. Speed up training with Random Skips

    master
    To accelerate the training process, you can use the 'Random skips' feature. This method skips frequently seen compounds during training based on a random probability. According to the technical report, this significantly increases training speed with only a minor impact on model performance.
  7. Tune Corpusweight (alpha) for the Morfessor Baseline model

    master

    The corpusweight ($\alpha$) balances the cost of the lexicon against the cost of the corpus. Morfessor provides several methods to tune this parameter during training:

    • Fixed weight: Set a constant weight at the start of training using --corpusweight.
    • Development set: Use a development set via --develset to balance the weight so that precision and recall on the development set are equal.
    • Morph length: Tune the weight via --morph-length so the average length of morphs in the lexicon matches a target value.
    • Num morph types: Tune the weight via --num-morph-types to target a specific approximate number of morph types in the lexicon.
  8. Install Morfessor 2.0 via PyPI

    master

    The easiest way to install Morfessor 2.0 and its associated tools is via the Python Package Index (PyPI) using pip or easy_install. This method automatically handles downloading and installing the latest version.

    It is recommended to run Morfessor within a virtual environment.

    pip install morfessor
    # or
    easy_install morfessor
  9. Install Morfessor 2.0 from a tarball or zip file

    master

    If you have downloaded a Morfessor 2.0 tarball or zip file from the Morpho project homepage or the Morfessor GitHub releases page, you can install it using one of the following two methods:

    1. Using setup.py: Unpack the file and run the installation script directly.
    2. Using pip: Point pip directly at the compressed archive.

    Morfessor 2.0 uses standard Python packaging and is compatible with virtual environments.

    # Method 1: Unpack and run setup.py
    python setup.py install
    
    # Method 2: Install directly from the archive
    pip install morfessor-VERSION.tar.gz
  10. Install dependencies for generating documentation

    master

    To build the Morfessor 2.0 documentation from source, you need the sphinx and sphinxcontrib-napoleon packages. You can install these along with the project's documentation dependencies using pip with the [docs] extra:

    pip install -e .[docs]
  11. Build Morfessor documentation in different formats

    master

    Once Sphinx is installed, navigate to the docs directory to generate documentation in various formats using the provided Makefile (Linux/macOS) or make.bat (Windows).

    Common commands include:

    • Generate a PDF: make latexpdf
    • Generate a single HTML file: make singlehtml
    • List all available formats: make help
    # Example: Generate a single HTML file
    make singlehtml