Skip to content

Commit

Permalink
Allow dicts to be open from filenames and Paths
Browse files Browse the repository at this point in the history
Fix #68.
  • Loading branch information
liZe committed Dec 15, 2024
1 parent 6bc5e38 commit c00863f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pyphen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,18 @@ class Pyphen:
def __init__(self, filename=None, lang=None, left=2, right=2, cache=True):
"""Create an hyphenation instance for given lang or filename.
:param filename: filename of hyph_*.dic to read
:param filename: filename or Path of hyph_*.dic to read
:param lang: lang of the included dict to use if no filename is given
:param left: minimum number of characters of the first syllabe
:param right: minimum number of characters of the last syllabe
:param cache: if ``True``, use cached copy of the hyphenation patterns
"""
if not filename:
filename = LANGUAGES[language_fallback(lang)]
self.left = left
self.right = right
if not cache or filename not in hdcache:
hdcache[filename] = HyphDict(filename)
self.hd = hdcache[filename]
self.left, self.right = left, right
path = Path(filename) if filename else LANGUAGES[language_fallback(lang)]
if not cache or path not in hdcache:
hdcache[path] = HyphDict(path)
self.hd = hdcache[path]

def positions(self, word):
"""Get a list of positions where the word can be hyphenated.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_pyphen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"""


from pathlib import Path

import pyphen


Expand Down Expand Up @@ -57,6 +59,20 @@ def test_personal_dict():
assert dic.inserted('autobandventieldopje') == 'au-to-band-ven-tiel-dop-je'


def test_dict_from_filename():
"""Test a dict open from filename."""
dic_path = Path(__file__).parents[1] / 'pyphen' / 'dictionaries' / 'hyph_fr.dic'
dic = pyphen.Pyphen(filename=str(dic_path))
assert dic.inserted('bonjour') == 'bon-jour'


def test_dict_from_path():
"""Test a dict open from path."""
dic_path = Path(__file__).parents[1] / 'pyphen' / 'dictionaries' / 'hyph_fr.dic'
dic = pyphen.Pyphen(filename=dic_path)
assert dic.inserted('bonjour') == 'bon-jour'


def test_left_right():
"""Test the ``left`` and ``right`` parameters."""
dic = pyphen.Pyphen(lang='nl_NL')
Expand Down

0 comments on commit c00863f

Please sign in to comment.