Problemintro
Hyphenation
Also called word hyphenation.
Find the positions inside a word where it may be split across two lines with a hyphen, by the rules of a language.
Hyphenation is the problem of finding the places inside a word where a line may end with a hyphen. A typesetter needs these places when a paragraph has narrow lines: without them, long words leave large gaps between the other words. The correct places are set by the conventions of a language and by its dictionaries, not by a formula, so a program can only approximate them. The usual methods are a dictionary of hyphenated words, rules on letter classes, and the patterns of Liang's method, which TeX uses.
In plain words
A long word at the end of a line may not fit. If you can cut it into two parts, with a hyphen after the first part, the line can be full and the spaces stay even. You cannot cut anywhere. In English "hy-phen-ation" may be cut after "hy" or after "hyphen", but not after "hyp". The rules depend on the language, on how the word is said, and sometimes on its meaning.
A program gets a word and must give the allowed cut points. It can look the word up in a list, but no list holds every word. So most programs learn short letter patterns from a list and apply them to any word. They try to find as many correct cut points as they can and to make no wrong one.
Definition
Fix an alphabet and a language. A word is a string over . A position , with , is the gap between and ; a hyphen at splits into - and .
The reference of the language is a function that maps each word to its set of correct positions . In practice is known only on a finite word list , for example a dictionary with the hyphens marked.
A hyphenation method is a procedure that maps every word to a set . On a word, its result splits the positions into three groups:
- found: ;
- errors (bad hyphens): ;
- missed: .
The problem is to build an that is small and fast, finds a large share of , and makes almost no errors. A typesetter also applies two limits after the method: it keeps only the positions with and , where and are the least numbers of letters before and after the hyphen (\lefthyphenmin and \righthyphenmin in TeX; plain TeX sets and ).
Errors cost more than misses. A missed position only makes the spacing of one paragraph a little worse; a wrong hyphen is visible to every reader. So methods are tuned for few errors first.
Variants
- Exact lookup. Store and answer for . This is a static dictionary problem on strings, solved with a trie or a hash table. It cannot hyphenate words that are not in , such as new words, names and many derived forms.
- Rules on letter classes. Split between two consonants in a vowel-consonant-consonant-vowel group, split before known suffixes, and keep a short exception list. The hyphenation of the first TeX worked this way.
- Patterns. Store short strings with a digit in each gap, and decide each gap by the largest digit of all patterns that cover it, as in Liang's method. The patterns are learned from by pattern generation, and an exception list fixes the rest.
- Weighted positions. Some languages and some publishers rank the positions: a break between the parts of a compound word is better than a break inside a part. A method then returns a value per position, not a yes or a no.
- Breaks that change the spelling. In some languages the letters change at a hyphen. In German spelling before 1996, "Zucker" became "Zuk-ker". TeX models this with a discretionary break that has a text before the break, a text after it, and a text with no break.
Examples
| word | correct positions | written |
|---|---|---|
| hyphenation | 2, 6, 7 | hy-phen-a-tion |
| table | 2 | ta-ble |
| record (noun) | 3 | rec-ord |
| record (verb) | 2 | re-cord |
The two rows for "record" show that is not a function of the letters alone in American English: the noun and the verb are spelled the same and hyphenated differently. Every method that looks only at the letters makes an error or a miss on one of them.
With the patterns of TeXbook Appendix H (hy3ph, he2n, hena4, hen5at, 1na, n2at, 1tio, 2io and o2n), Liang's rule gives hy-phen-ation: it finds the positions 2 and 6 and misses 7. A miss is the safe kind of mistake.
With the limits of plain TeX (, ), "table" can break at 2 ("ta-ble"), because and . A word of four letters would need and , which no meets, so plain TeX never hyphenates a word of four letters.
Known algorithms
- Liang's hyphenation algorithm: pattern matching with a trie of patterns; trie steps for a word of letters in the worst case. TeX uses it.
- Hyphenation pattern generation (patgen): builds the patterns from a hyphenated word list, level by level.
- Packed trie: the compact table that holds the patterns in TeX82, with one table index per letter.
- Dictionary matching with Aho-Corasick finds all occurrences of a set of patterns in one pass over the word, which gives the same values as Liang's rule.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Hyphenation pattern generation | O(P N L^2) worst | O(N L + Q) extra | deterministicofflineiterative | 1 language |
| Liang's hyphenation algorithm | O(k l^2) worst | Θ(k) extra | deterministicofflineiterative | 1 language |
Bounds
For a fixed set of patterns, any method must read the word, so time is needed in the word RAM. Liang's walk takes trie steps, and an automaton of the patterns takes , where is the number of pattern occurrences; for real words is small and both are fast.
There is no bound on accuracy that holds for all languages. The "record" example shows that a method based on the spelling alone cannot be correct on all of when holds two words with the same letters and different positions. The numbers that are known are measurements on one word list. Liang reports that the TeX82 patterns find 89.3 percent of the hyphens of his copy of Webster's Pocket Dictionary with essentially no errors, while the old TeX rules found about 40 percent with an error rate of about 1 percent of the hyphen points.
Reductions
- Hyphenation by patterns reduces to dictionary matching: the value of each gap is the largest digit over all occurrences of the patterns in ".u.", where "." marks the word edges.
- Exact lookup reduces to a static dictionary on strings (trie, packed trie or hash table).
Applications
- Line breaking. In the box, glue and penalty model, each allowed position becomes a flagged penalty with the width of the hyphen. Greedy line breaking and the Knuth-Plass algorithm then choose which of them to use.
- Narrow columns. Newspapers and mobile screens have short lines, where words without hyphens give very loose lines.
- Spelling and word lists. Liang's thesis treats hyphenation and spelling checking together, because both need a compact store of many words.
References
- Franklin Mark Liang, Word Hy-phen-a-tion by Com-put-er, PhD thesis, Stanford University, August 1983, report STAN-CS-83-977, Chapter 1 (the problem, the dictionary and the rule-based methods, and the old TeX algorithm) and Chapter 3 (hyphenation by patterns)
- Donald E. Knuth, The TeXbook, Computers and Typesetting volume A, Addison-Wesley, Appendix H, Hyphenation
- Donald E. Knuth and Michael F. Plass, Breaking paragraphs into lines, Software: Practice and Experience 11(11), 1119-1184, 1981, the use of hyphens as flagged penalties in the box, glue and penalty model
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag ZMG689 Edit this page History Report an error Markdown