https://github.com/tomconder/linebreak
An app to demonstrate line-breaking algorithms
https://github.com/tomconder/linebreak
golang greedy-algorithm knuth-plass-breaking line-breaking
Last synced: 5 months ago
JSON representation
An app to demonstrate line-breaking algorithms
- Host: GitHub
- URL: https://github.com/tomconder/linebreak
- Owner: tomconder
- License: mit
- Created: 2025-06-27T02:40:11.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-06-27T02:59:52.000Z (5 months ago)
- Last Synced: 2025-06-27T03:34:37.551Z (5 months ago)
- Topics: golang, greedy-algorithm, knuth-plass-breaking, line-breaking
- Language: Go
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# linebreak
A simple app to demonstrate line-breaking algorithms.
## Features
- **Dynamic Programming:** Efficiently computes optimal line breaks. It uses a penalty for trailing spaces to promote balanced text.
- **Unit Tested:** Includes unit tests to ensure quality and to allow future changes with confidence.
## Installation and Running
To run the app:
```bash
go run github.com/tomconder/linebreak/cmd/linebreak
```
To use the package in your project, you can get it using Go modules:
```bash
go get github.com/tomconder/linebreak/pkg/linebreak
```
## Algorithms
### Greedy
The [greedy algorithm](https://en.wikipedia.org/wiki/Greedy_algorithm) breaks a sequence of words into lines. At each step it fits as many words as possible on each line within the given width.
For example, the following text with a given width of 14:
```
The lazy yellow dog was caught by the slow red fox as he lay sleeping in the sun
```
gives the following result
```
The lazy
yellow dog was
caught by the
slow red fox
as he lay
sleeping in
the sun
```
### Knuth-Plass
The [Knuth-Plass line breaking algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Plass_line-breaking_algorithm) breaks a sequence of words into lines that do not exceed the given width. It uses [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming) to determine optimal breakpoints based on a penalty function that discourages trailing empty spaces.
For example, the following text with a given width of 14:
```
The lazy yellow dog was caught by the slow red fox as he lay sleeping in the sun
```
gives the following result
```
The lazy
yellow dog
was caught by
the slow red
fox as he lay
sleeping in
the sun
```