Git Product home page Git Product logo

python-abbreviate's Introduction

Abbreviate

This filter attempts to automatically and intelligently abbreviate strings.

This library contains a dictionary of known abbreviations. Some words have multiple abbreviations, e.g. Thursday could be abbreviated as any of Thurs, Thur, Thr, Th, or T depending on context. With no other information, each word has a "preferred abbreviation" (Thr), however options can push things one way or another. For words without known abbreviations, a series of heuristics are applied to shorten them as needed.

The basic abbreviate method will only apply preferred abbreviations and no heuristics. For more advanced applications, the library can be given a target length and effort, and will attempt to generate the best string possible. Length can be supplied either as a simple character count, or with a custom length function. The latter is useful in many graphics applications without monospaced fonts or constant kerning. By default, abbreviate will not shorten any exisiting abbreviations (e.g. Thur -> Th), assuming that any explicit abbreviation was passed as such for a reason.

Issues, updates, pull requests, etc should be directed to github.

State

This tool was written to scratch an immediate itch and is thus quite incomplete, but with extensibility and somewhat grander ideas in mind. Architectural thoughts, radical changes to methodology, or just greater abbreviations are all welcome.

Installation

The easiest method is to simply use pip:

(sudo) pip install abbreviate

Usage

>>> import abbreviate
>>> abbr = abbreviate.Abbreviate()

>>> abbr.abbreviate("This library does nothing without pressure to make shorter")
'This library does nothing without pressure to make shorter'

>>> abbr.abbreviate("By default, it will treat length as simple character count", 20)
'By dflt, it wll trt lngth as smple chrctr cnt'

>>> from reportlab.lib.units import inch
>>> from reportlab.pdfgen import canvas
>>> from reportlab.lib.pagesizes import letter
>>> c = canvas.Canvas('/tmp/example.pdf', pagesize=letter)
>>> def calculate_rendered_length(text):
...     return c.stringWidth(text, "Helvetica", 8)
>>> abbr.abbreviate("A custom length function is useful for rendered text", target_len=2.5*inch, len_fn=calculate_rendered_length)
'A cstm length function is useful for rendered text'

python-abbreviate's People

Contributors

ppannuto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

python-abbreviate's Issues

How it works?

It is very confusing to use. For example, if I have the sentence "he doesn't go" then I need output as "he does not go". But the use of abbreviate here is very complex. So please help me out.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.