Pyphen

0.17.2 · active · verified Sun Apr 05

Pyphen is a pure Python module designed for text hyphenation, leveraging existing Hunspell hyphenation dictionaries. It bundles a wide array of dictionaries sourced from the LibreOffice project. The library is currently at version 0.17.2 and maintains an active development status with regular releases, often including updates to dictionaries and broader Python version compatibility.

Warnings

Install

Imports

Quickstart

Initialize a `Pyphen` object for a desired language. The quickstart demonstrates basic hyphenation using `inserted()`, word wrapping with `wrap()`, and iterating through all possible hyphenation points with `iterate()`. It also shows how to check for available languages using `pyphen.LANGUAGES`. Dictionaries are automatically loaded upon instantiation.

import pyphen

# Instantiate Pyphen for a specific language (e.g., French)
dic = pyphen.Pyphen(lang='fr_FR')

# Check if a language is available
print(f"Is 'fr_FR' available? {'fr_FR' in pyphen.LANGUAGES}")

# Hyphenate a word, inserting hyphens
word = 'fromage'
hyphenated_word = dic.inserted(word)
print(f"'{word}' hyphenated: {hyphenated_word}") # Expected: 'fro-mage'

# Wrap a word to a certain width
long_word = 'autobandventieldopje'
wrapped_word = dic.wrap(long_word, 11)
print(f"'{long_word}' wrapped to 11 chars: {wrapped_word}") # Expected: ('autoband-', 'ventieldopje')

# Iterate over all possible hyphenation points
print(f"Hyphenation iterations for 'Amsterdam':")
for pair in dic.iterate('Amsterdam'):
    print(pair)

view raw JSON →