/smart-truncation

An experiment to mimic human truncation by abbreviation instead of ellipsis.

Primary LanguagePython

Smart Truncation

Let’s say you have a container with a fixed width and need to truncate text.

| small diagonal | cross

Traditionally you’d use an ellipsis to truncate the words

| small diago... |

But a human would abbreviate the words.

| sm diag cross  |

Implementations

CSharp

1. CSharp Dictionary Implementation

A simple abbreviator that finds the closest length by replacing words with their known abbreviations.

Abbreviator.cs | AbbreviatorTest.cs

To run tests. dotnet test in the ./csharp directory

Python 3

1. Python Naive Implementation

Naive approach that prioritzes longest-token truncation. Abbreviator limits minimum token word length to 1.

abbreviator.py | test_abbreviator.py

To run tests, pip install pytest and execute pytest in the Python directory.

Ruby

1. Naive Implementation

The first implementation was a proof of concept to shorten the longest words first. This would eventually be combined with #2 to remove any additional characters.

naive.rb | naive_spec.rb

| smal diag cros |

2. Dictionary Implementation

The second approach utilizes a map of known abbreviations. This example has a few more test cases to find the best replacements to match the desired length.

with_dictionary.rb | with_dictionary_spec.rb

// 14 chars
  1..........14
| sm diag cross     |

// 16 chars
  1.............16
| small diag cross  |

// 17 chars
  1..............17
| sm diagonal cross |