github.com/speedata/hyphenation
go get github.com/speedata/hyphenation

github.com/speedata/hyphenation

A port of TeX's hyphenation algorithm to Go

by speedata

v1.0.1 (see all)License:CC0-1.0
go get github.com/speedata/hyphenation
Readme

GoDoc CircleCI

A port of TeX's hyphenation algorithm to Go

Installation

go get github.com/speedata/hyphenation

Prerequisites

Download a hyphenation pattern file from CTAN, for example from https://ctan.math.utah.edu/ctan/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/

Usage

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/speedata/hyphenation"
)

func main() {
    filename := "hyph-en-us.pat.txt"
    r, err := os.Open(filename)
    if err != nil {
        log.Fatal(err)
    }
    l, err := hyphenation.New(r)
    if err != nil {
        log.Fatal(err)
    }

    var h []int
    for _, v := range []string{"Computer", "developers"} {
        h = l.Hyphenate(v)
        fmt.Println(v, h) // [3 6] and [2 5 7 9]
    }
}

Debugging hyphenation patterns

Similar to getting the hyphenation slice, you can get a detailed view of the hyphenation patterns used in a word:

str := l.DebugHyphenate("developers")
fmt.Println(str)

results in

   .   d   e   v   e   l   o   p   e   r   s   .
     0   0   1   0   |   |   |   |   |   |   |    de1v
     |   0   0   0   0   3   0   |   |   |   |    evel3o
     |   |   0   0   4   0   0   |   |   |   |    ve4lo
     |   |   |   |   |   0   0   1   0   0   |    op1er
     |   |   |   |   |   |   |   0   0   1   0    er1s
     |   |   |   |   |   |   |   |   4   0   2    4rs2
max: 0   0   1   0   4   3   0   1   4   1   2
final: d   e - v   e   l - o   p - e   r - s

Other

Contact: gundlach@speedata.de
Twitter: @speedata
License: cc0 / public domain (https://creativecommons.org/publicdomain/zero/1.0/)
Status: Just an example hack, never used it in production yet

GitHub Stars

11

LAST COMMIT

1yr ago

MAINTAINERS

0

CONTRIBUTORS

1

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v1.0.1-0.20210613130129-8c23c53dd3d9
1yr ago
v1.0.1-0.20210613072227-2b5122faec19
1yr ago
v1.0.1-0.20210611141615-81991906970a
1yr ago
v1.0.1
1yr ago
No alternatives found
No tutorials found
Add a tutorial