Skip to main content

Unicode grapheme helpers

Project description

grapheme

A Python package for working with user perceived characters. More specifically, string manipulation and calculation functions for workong with grapheme cluster groups (graphemes) as defined by the Unicode Standard Annex #29.

documentation

pip install grapheme

What? Why?

Unicode strings are made up of a series of unicode characters, but a unicode character does not always map to a user perceived character. Some human perceived characters are represented as two or more unicode characters.

However, all built in python string functions and string methods work with single unicode characters without considering their connection to each other.

>>> string = 'u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲'
>>> len(string)
20
>>> grapheme.length(string)
10
>>> string[:3]
'u̲n'
>>> grapheme.substr(string, 0, 3)
'u̲n̲d̲'

This library implements the unicode default rules for extended grapheme clusters, and provides a set of functions for string manipulation based on graphemes.

Documentation

See https://grapheme.readthedocs.io/en/latest/.

When should I consider graphemes instead of unicode characters?

You should consider working with graphemes over unicode code points when:

  • You wish to count characters as perceived by users.

  • You wish to split or truncate text at some user perceived lengths.

  • You wish to split or truncate text without risk of corrupting the characters.

  • Formatting text by length, such as creating text based tables in monospaced fonts

You should work with normal python string functions when:

  • You wish to count or split by unicode codepoints for compliance with storage limitations (such as database maximum length)

  • When working with systems that put limits on strings by unicode character lengths

  • When you prioritize performance over correctness (see performance notes below)

  • When working with very long strings (see performance notes below)

  • If simplicity is more important than accuracy

Performance

Calculating graphemes require traversing the string and checking each character against a set of rules and the previous character(s). Because of this, all functions in this module will scale linearly to the string length.

Whenever possible, they will only traverse the string for as long as needed and return early as soon as the requested output is generated. For example, the grapheme.slice function only has to traverse the string until the last requested grapheme is found, and does not care about the rest of the string.

You should probably only use this package for testing/manipulating fairly short strings or with the beginning of long strings.

When testing with a string of 10 000 ascii characters, and a 3.1 GHz processor, the execution time is for some possible calls is roughly:

Code

Approximate execution time

len(long_ascii_string)

8.1e-10 seconds

grapheme.length(long_ascii_string)

1.5e-04 seconds

grapheme.length(long_ascii_string, 500)

8.7e-06 seconds

long_ascii_string[0:100]

1.3e-09 seconds

grapheme.slice(long_ascii_string, 0, 100)

2.5e-06 seconds

long_ascii_string[:100] in long_ascii_string

4.0e-09 seconds

grapheme.contains(long_ascii_string, long_ascii_string[:100])

3.9e-06 seconds

long_ascii_string[-100:] in long_ascii_string

2.1e-07 seconds

grapheme.contains(long_ascii_string, long_ascii_string[-100:])

1.9e-04 seconds

Execution times may improve in later releases, but calculating graphemes is and will continue to be notably slower than just counting unicode code points.

Examples of grapheme cluster groups

This is not a complete list, but a some examples of when graphemes use multiple characters:

  • CR+LF

  • Hangul (korean)

  • Emoji with modifiers

  • Combining marks

  • Zero Width Join

Development quick start

If you wish to contribute or edit this package, create a fork and clone it.

Then install in locally editable (-e) mode and run the tests.

pip install -e .[test]
py.test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grapheme-0.2.1.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grapheme-0.2.1-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file grapheme-0.2.1.tar.gz.

File metadata

  • Download URL: grapheme-0.2.1.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for grapheme-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a3de9b3ad931fcb9b0e0242d85016b087186bb0227f451df92f22955bbc11573
MD5 58bfa07a0e819995e26764b2a994899a
BLAKE2b-256 4db52acc3cef834c7d0ae3e884935c63e8fe49727cd75f3f50d747afae1da65d

See more details on using hashes here.

File details

Details for the file grapheme-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for grapheme-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 523ff346c06be3b7f089ed5d48e964a6eabeaa218ae821d0ae1eb2eecd5d9a4e
MD5 71f83a61e749499b7bd52e72bfbfb11e
BLAKE2b-256 c48c6fbb385d2a4da1c0c130cd910a5b10be9f3d5c3ef21bef2e2e3e35e0d5a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page