shyster
The problem this package is trying to solve is that while I can set
hyphens: auto; in CSS, many browsers do a poor job of hyphenating
Finnish. Even if they have Finnish hyphenation patterns, they often fail
to recognise compound words, which should be hyphenated at compound
boundaries (saippua-kauppias, not saip-pua-kaup-pias). One solution is
to set hyphens: manual; and add soft hyphens at acceptable hyphenation
spots.
Install
pip install shyster
How to use
One top-level function does it all:
import shyster
shyster.hyphenate_html_file('input.html', 'output.html', 'patterns/hyphen.tex')
If more control is needed:
hyph_fi = hyphenator('patterns/hyph-fi.tex', righthyphenmin=2)
[hyph_fi(word) for word in
'Jukolan talo, eteläisessä Hämeessä, seisoo erään mäen pohjaisella rinteellä, liki Toukolan kylää'\
.replace(',','').split()]
['Ju-ko-lan',
'ta-lo',
'ete-läi-ses-sä',
'Hä-mees-sä',
'sei-soo',
'erään',
'mäen',
'poh-jai-sel-la',
'rin-teel-lä',
'li-ki',
'Tou-ko-lan',
'ky-lää']
html = """
<!doctype html><title>Seitsemän veljestä</title>
<script>var veljekset = 7;</script>
<body>
<p style="margin-top: 2em">Jukolan talo, eteläisessä Hämeessä, seisoo erään mäen pohjaisella
rinteellä, liki Toukolan kylää. Sen läheisin ympäristö on kivinen
tanner, mutta alempana alkaa pellot, joissa, ennenkuin talo oli häviöön
mennyt, aaltoili teräinen vilja.</p>
</body>
"""
soup = BeautifulSoup(html, 'lxml')
hyphenate_soup(soup, hyph_fi)
print(str(soup))
<!DOCTYPE html>
<html><head><title>Seit-se-män vel-jes-tä</title>
<script>var veljekset = 7;</script>
</head><body>
<p style="margin-top: 2em">Ju-ko-lan ta-lo, ete-läi-ses-sä Hä-mees-sä, sei-soo erään mäen poh-jai-sel-la
rin-teel-lä, li-ki Tou-ko-lan ky-lää. Sen lä-hei-sin ym-pä-ris-tö on ki-vi-nen
tan-ner, mut-ta alem-pa-na al-kaa pel-lot, jois-sa, en-nen-kuin ta-lo oli hä-vi-öön
men-nyt, aal-toi-li te-räi-nen vil-ja.</p>
</body>
</html>
pat, ex = read_patterns(open('patterns/hyphen.tex').readlines())
trie = convert_patterns(pat)
ex = convert_exceptions(ex)
del ex['present'] # remove an exception
ex['shyster'] = ('shy', 'ster') # add or alter an exception
ex['lawyer'] = ('l', 'a', 'w', 'y', 'e', 'r') # exceptions even override {left,right}hyphenmin
hyph_en = hyphenator(None, hyphen='•')
hyph_en.trie = trie
hyph_en.exceptions = ex
import textwrap
textwrap.wrap(' '.join(hyph_en(match.group(0))
for match in re.finditer(r'[\w]+', '''
shyster: noun; 1. someone, possibly a lawyer, who behaves in an unscrupulous way;
2. the present Python library
''')))
['shy•ster noun 1 some•one pos•si•bly a l•a•w•y•e•r who be•haves in an',
'un•scrupu•lous way 2 the pre•sent Python li•brary']
Copying
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
The above does not apply to the files in patterns/, which are
distributed with this program as example input files. The Finnish
patterns are covered by the terms “Patterns may be freely distributed”
and the English ones by “Unlimited copying and redistribution of this
file are permitted as long as this file is not modified.”
Metadata
Release files for shyster 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| shyster-0.0.3.tar.gz | 22.8 kB | Without provenance | Details for shyster-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| shyster-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for shyster-0.0.3-py3-none-any.whl |
Total release size: 44.6 kB
Release files / shyster-0.0.3.tar.gz
| Download URL | shyster-0.0.3.tar.gz |
|---|---|
| Size | 22.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f8e7a47f6386886a931d1d3efdd43fd2de2870addcc2a9d38d7120d5dfca65db
|
|
BLAKE2b-256 checksum How to use checksums |
004c0ec11c77cb7a9c288e482e8c1492bf7b3f4599cb7c8bdb58e66ee7abaed9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Release files / shyster-0.0.3-py3-none-any.whl
| Download URL | shyster-0.0.3-py3-none-any.whl |
|---|---|
| Size | 21.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
699fb5c92744845a9b9b0b31c6c9cdf5eb7fb6abeb14927d65d9226d20990a2d
|
|
BLAKE2b-256 checksum How to use checksums |
520b8bf5f4c649d26d6a3019b0dcb7b6adc519c0f5802f897b839a651f128328
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.5
|
Security for shyster 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.