-
CSharp
-
Python 3
-
Ruby
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 |
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
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.
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.
| smal diag cros |
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 |