Skip to main content

Accurately remove and replace emojis in text strings

Project description

unimoji

Accurately find or remove emojis from a blob of text using the latest data from the Unicode Consortium's emoji code repository.

This library is based on the excellent work of Brad Solomon and has been updated to support the latest Unicode emoji specifications with performance optimizations.

License PyPI Status Python


About

unimoji is an updated and optimized version of the original demoji library by Brad Solomon. This library provides enhanced Unicode emoji support with the latest emoji specifications and improved performance.

Key improvements over demoji:

  • Unicode 17.0 support - Latest emoji specifications (5200+ emojis)
  • Performance optimizations - Faster replace_with_desc() with O(n) complexity
  • Modern Python support - Python 3.8+ with type hints
  • Reduced dependencies - No external runtime dependencies
  • Maintained API compatibility - Easy drop-in replacement

Credits: Special thanks to Brad Solomon for the original demoji library which serves as the foundation for this project.

Installation

pip install unimoji

Migrating from demoji

unimoji is a drop-in replacement for demoji. Simply replace your imports:

# Old
import demoji

# New
import unimoji as demoji  # or
import unimoji

Basic Usage

unimoji exports several text-related functions for find-and-replace functionality with emojis:

>>> tweet = """\
... #startspreadingthenews yankees win great start by 🎅🏾 going 5strong innings with 5k’s🔥 🐂
... solo homerun 🌋🌋 with 2 solo homeruns and👹 3run homerun… 🤡 🚣🏼 👨🏽‍⚖️ with rbi’s … 🔥🔥
... 🇲🇽 and 🇳🇮 to close the game🔥🔥!!!….
... WHAT A GAME!!..
... """
>>> unimoji.findall(tweet)
{
    "🔥": "fire",
    "🌋": "volcano",
    "👨🏽\u200d⚖️": "man judge: medium skin tone",
    "🎅🏾": "Santa Claus: medium-dark skin tone",
    "🇲🇽": "flag: Mexico",
    "👹": "ogre",
    "🤡": "clown face",
    "🇳🇮": "flag: Nicaragua",
    "🚣🏼": "person rowing boat: medium-light skin tone",
    "🐂": "ox",
}

See below for function API.

Command-line Use

You can use unimoji or python -m unimoji to replace emojis in file(s) or stdin with their :code: equivalents:

$ cat out.txt
All done!  🍰 ✨
$ unimoji out.txt
All done! :sparkles: :shortcake: :sparkles:

$ echo 'All done! ✨ 🍰 ✨' | unimoji
All done! :sparkles: :shortcake: :sparkles:

$ unimoji -
we didnt start the 🔥
we didnt start the :fire:

Reference

findall(string: str) -> Dict[str, str]

Find emojis within string. Return a mapping of {emoji: description}.

findall_list(string: str, desc: bool = True) -> List[str]

Find emojis within string. Return a list (with possible duplicates).

If desc is True, the list contains description codes. If desc is False, the list contains emojis.

replace(string: str, repl: str = "") -> str

Replace emojis in string with repl.

replace_with_desc(string: str, sep: str = ":") -> str

Replace emojis in string with their description codes. The codes are surrounded by sep.

last_downloaded_timestamp() -> datetime.datetime

Show the timestamp of last download for the emoji data bundled with the package.

Footnote: Emoji Sequences

Numerous emojis that look like single Unicode characters are actually multi-character sequences. Examples:

  • The keycap 2️⃣ is actually 3 characters, U+0032 (the ASCII digit 2), U+FE0F (variation selector), and U+20E3 (combining enclosing keycap).
  • The flag of Scotland 7 component characters, b'\\U0001f3f4\\U000e0067\\U000e0062\\U000e0073\\U000e0063\\U000e0074\\U000e007f' in full esaped notation.

(You can see any of these through s.encode("unicode-escape").)

unimoji is careful to handle this and should find the full sequences rather than their incomplete subcomponents.

The way it does this it to sort emoji codes by their length, and then compile a concatenated regular expression that will greedily search for longer emojis first, falling back to shorter ones if not found. This is not by any means a super-optimized way of searching as it has O(N2) properties, but the focus is on accuracy and completeness.

>>> from pprint import pprint
>>> seq = """\
... I bet you didn't know that 🙋, 🙋‍♂️, and 🙋‍♀️ are three different emojis.
... """
>>> pprint(seq.encode('unicode-escape'))  # Python 3
(b"I bet you didn't know that \\U0001f64b, \\U0001f64b\\u200d\\u2642\\ufe0f,"
 b' and \\U0001f64b\\u200d\\u2640\\ufe0f are three different emojis.\\n')

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

unimoji-2.0.0.tar.gz (49.6 kB view details)

Uploaded Source

Built Distribution

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

unimoji-2.0.0-py3-none-any.whl (45.8 kB view details)

Uploaded Python 3

File details

Details for the file unimoji-2.0.0.tar.gz.

File metadata

  • Download URL: unimoji-2.0.0.tar.gz
  • Upload date:
  • Size: 49.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for unimoji-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a259663a998ca53612ffdbe62f7a2b525a7f00f7306d2c49357ac4bf9c3ecd23
MD5 d1fa1d7bf1e1c8d56f9451ba275927f8
BLAKE2b-256 7fdf07e139cd4ada8038fce4165fb5817fb267da5d0b27922c834f238474608f

See more details on using hashes here.

File details

Details for the file unimoji-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: unimoji-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 45.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for unimoji-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12afcadfd5f0b7475b1d97a688889efc9e1253a3930a53abc9722be240369493
MD5 323008b6ece2a8dbcac3c415ce4ee51d
BLAKE2b-256 a2e393d744b181c9a9b246774da90236c70f9f9ff45bf69d46ae1194f386cd3a

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