# demoji
Accurately find or remove [emojis](https://en.wikipedia.org/wiki/Emoji) from a blob of text using
data from the Unicode Consortium's [emoji code repository](https://unicode.org/Public/emoji/).
[![License](https://img.shields.io/github/license/bsolomon1124/demoji.svg)](https://github.com/bsolomon1124/demoji/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/demoji.svg)](https://pypi.org/project/demoji/)
[![Status](https://img.shields.io/pypi/status/demoji.svg)](https://pypi.org/project/demoji/)
[![Python](https://img.shields.io/pypi/pyversions/demoji.svg)](https://pypi.org/project/demoji)
-------
## Major Changes in Version 1.x
Version 1.x of `demoji` now bundles Unicode data in the package at install time rather than requiring
a download of the codes from unicode.org at runtime. Please see the [CHANGELOG.md](CHANGELOG.md)
for detail and be familiar with the changes before updating from 0.x to 1.x.
To report any regressions, please [open a GitHub issue](https://github.com/bsolomon1124/demoji/issues/new?assignees=&labels=&template=bug_report.md&title=).
## Basic Usage
`demoji` exports several text-related functions for find-and-replace functionality with emojis:
```python
>>> 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!!..
... """
>>> demoji.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](#reference) for function API.
## Command-line Use
You can use `demoji` or `python -m demoji` to replace emojis
in file(s) or stdin with their `:code:` equivalents:
```bash
$ cat out.txt
All done! โจ ๐ฐ โจ
$ demoji out.txt
All done! :sparkles: :shortcake: :sparkles:
$ echo 'All done! โจ ๐ฐ โจ' | demoji
All done! :sparkles: :shortcake: :sparkles:
$ demoji -
we didnt start the ๐ฅ
we didnt start the :fire:
```
## Reference
```python
findall(string: str) -> Dict[str, str]
```
Find emojis within `string`. Return a mapping of `{emoji: description}`.
```python
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.
```python
replace(string: str, repl: str = "") -> str
```
Replace emojis in `string` with `repl`.
```python
replace_with_desc(string: str, sep: str = ":") -> str
```
Replace emojis in `string` with their description codes. The codes are surrounded by `sep`.
```python
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")`.)
`demoji` 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(N<sup>2</sup>) properties, but the focus is on accuracy and completeness.
```python
>>> 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')
```
# Changelog
## 1.1.0
- Add a `__main.py__` to allow running `python -m demoji`;
add an entry-point `demoji` command;
permit stdin (`-`), file name(s), or piped stdin.
Contribution by @jap.
## 1.0.0
**This is a backwards-incompatible release with several substantial changes.**
The largest change is that `demoji` now bundles a static copy of Unicode
emoji data with the package at install time, rather than requiring a runtime
download of the codes from unicode.org.
Changes below are grouped by their corresponding
[Semantic Versioning](https://semver.org/) identifier.
SemVer MAJOR:
- Drop support for Python 2 and Python 3.5
- The `demoji` package now bundles emoji data that is distributed with the
package at install time, rather than requiring a download of the codes
from the unicode.org site at runtime (closes #23)
- As a result of the above change, the following functions are **removed**
from the `demoji` API:
- `download_codes()`
- `parse_unicode_sequence()`
- `parse_unicode_range()`
- `stream_unicodeorg_emojifile()`
SemVer MINOR:
- The `demoji.DIRECTORY` and `demoji.CACHEPATH` attributes are deprecated
due to no longer being functionally in used by the package. Accessing them
will warn with a `FutureWarning`, and these attributes may be removed
completely in a future release
- `demoji` can now be installed with optional `ujson` support for faster loading
of emoji data from file (versus the standard library's `json`, which is the
default); use `python -m pip install demoji[ujson]`
- The dependencies `requests` and `colorama` have been removed completely
- `importlib_resources` (a backport module) is now required for Python < 3.7
- The `EMOJI_VERSION` attribute, newly added to `demoji`, is a `str` denoting
the Unicode database version in use
SemVer PATCH:
- Fix a typo in `demoji.__all__` to properly include `demoji.findall_list()`
- Internal change: Functions that call `set_emoji_pattern()` are now decorated
with a `@cache_setter` to set the cache
- Some unit tests have been removed to update the change in behavior from
downloading codes to bundling codes with install
- Update README to reflect bundling behavior
## 0.4.0
- Update emoji source list to version 13.1. (See 5090eb5.)
- Formally support Python 3.9. (See 6e9c34c.)
- Bugfix: ensure that `demoji.last_downloaded_timestamp()` returns correct UTC time.
(See 6c8ad15.)
## 0.3.0
- Feature: add `findall_list()` and `replace_with_desc()` functions. (See 7cea333.)
- Modernize setup config to use `setup.cfg`. (See 8f141e7.)
## 0.2.1
- Tox: formally add Python 3.8 tests.
## 0.2.0
- Windows: use the [colorama] package to support printing ANSI escape sequences on Windows;
this introduces colorama as a dependency. (See cd343c1.)
- Setup: Fix a bug in `setup.py` that would require dependencies to be installed
_prior to_ installation of `demoji` in order to find the `__version__`.
(See d5f429c.)
- Python 2 + Windows support: use `io.open(..., encoding='utf-8')` consistently in `setup.py`.
(See 1efec5d.)
- Distribution: use a universal wheel in PyPI release. (See 8636a32.)
[colorama]: https://github.com/tartley/colorama
## 0.1.5
- Performance improvement: use `re.escape()` rather than failing to compile a small subset of codes.
- Remove an unused constant in `__init__.py`.
Raw data
{
"_id": null,
"home_page": "https://github.com/bsolomon1124/demoji",
"name": "demoji",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "emoji,emojis,nlp,natural langauge processing,unicode",
"author": "Brad Solomon",
"author_email": "bsolomon@protonmail.com",
"download_url": "https://files.pythonhosted.org/packages/9d/62/e6de96cf1ef2c6ac91b84a51af151d791f874529d8b146d3587771d05727/demoji-1.1.0.tar.gz",
"platform": "",
"description": "# demoji\n\nAccurately find or remove [emojis](https://en.wikipedia.org/wiki/Emoji) from a blob of text using\ndata from the Unicode Consortium's [emoji code repository](https://unicode.org/Public/emoji/).\n\n[![License](https://img.shields.io/github/license/bsolomon1124/demoji.svg)](https://github.com/bsolomon1124/demoji/blob/master/LICENSE)\n[![PyPI](https://img.shields.io/pypi/v/demoji.svg)](https://pypi.org/project/demoji/)\n[![Status](https://img.shields.io/pypi/status/demoji.svg)](https://pypi.org/project/demoji/)\n[![Python](https://img.shields.io/pypi/pyversions/demoji.svg)](https://pypi.org/project/demoji)\n\n-------\n\n## Major Changes in Version 1.x\n\nVersion 1.x of `demoji` now bundles Unicode data in the package at install time rather than requiring\na download of the codes from unicode.org at runtime. Please see the [CHANGELOG.md](CHANGELOG.md)\nfor detail and be familiar with the changes before updating from 0.x to 1.x.\n\nTo report any regressions, please [open a GitHub issue](https://github.com/bsolomon1124/demoji/issues/new?assignees=&labels=&template=bug_report.md&title=).\n\n## Basic Usage\n\n`demoji` exports several text-related functions for find-and-replace functionality with emojis:\n\n```python\n>>> tweet = \"\"\"\\\n... #startspreadingthenews yankees win great start by \ud83c\udf85\ud83c\udffe going 5strong innings with 5k\u2019s\ud83d\udd25 \ud83d\udc02\n... solo homerun \ud83c\udf0b\ud83c\udf0b with 2 solo homeruns and\ud83d\udc79 3run homerun\u2026 \ud83e\udd21 \ud83d\udea3\ud83c\udffc \ud83d\udc68\ud83c\udffd\u200d\u2696\ufe0f with rbi\u2019s \u2026 \ud83d\udd25\ud83d\udd25\n... \ud83c\uddf2\ud83c\uddfd and \ud83c\uddf3\ud83c\uddee to close the game\ud83d\udd25\ud83d\udd25!!!\u2026.\n... WHAT A GAME!!..\n... \"\"\"\n>>> demoji.findall(tweet)\n{\n \"\ud83d\udd25\": \"fire\",\n \"\ud83c\udf0b\": \"volcano\",\n \"\ud83d\udc68\ud83c\udffd\\u200d\u2696\ufe0f\": \"man judge: medium skin tone\",\n \"\ud83c\udf85\ud83c\udffe\": \"Santa Claus: medium-dark skin tone\",\n \"\ud83c\uddf2\ud83c\uddfd\": \"flag: Mexico\",\n \"\ud83d\udc79\": \"ogre\",\n \"\ud83e\udd21\": \"clown face\",\n \"\ud83c\uddf3\ud83c\uddee\": \"flag: Nicaragua\",\n \"\ud83d\udea3\ud83c\udffc\": \"person rowing boat: medium-light skin tone\",\n \"\ud83d\udc02\": \"ox\",\n}\n```\n\nSee [below](#reference) for function API.\n\n## Command-line Use\n\nYou can use `demoji` or `python -m demoji` to replace emojis\nin file(s) or stdin with their `:code:` equivalents:\n\n```bash\n$ cat out.txt\nAll done! \u2728 \ud83c\udf70 \u2728\n$ demoji out.txt\nAll done! :sparkles: :shortcake: :sparkles:\n\n$ echo 'All done! \u2728 \ud83c\udf70 \u2728' | demoji\nAll done! :sparkles: :shortcake: :sparkles:\n\n$ demoji -\nwe didnt start the \ud83d\udd25\nwe didnt start the :fire:\n```\n\n## Reference\n\n```python\nfindall(string: str) -> Dict[str, str]\n```\n\nFind emojis within `string`. Return a mapping of `{emoji: description}`.\n\n```python\nfindall_list(string: str, desc: bool = True) -> List[str]\n```\n\nFind emojis within `string`. Return a list (with possible duplicates).\n\nIf `desc` is True, the list contains description codes. If `desc` is False, the list contains emojis.\n\n```python\nreplace(string: str, repl: str = \"\") -> str\n```\n\nReplace emojis in `string` with `repl`.\n\n```python\nreplace_with_desc(string: str, sep: str = \":\") -> str\n```\n\nReplace emojis in `string` with their description codes. The codes are surrounded by `sep`.\n\n```python\nlast_downloaded_timestamp() -> datetime.datetime\n```\n\nShow the timestamp of last download for the emoji data bundled with the package.\n\n## Footnote: Emoji Sequences\n\nNumerous emojis that look like single Unicode characters are actually multi-character sequences. Examples:\n\n- The keycap 2\ufe0f\u20e3 is actually 3 characters, U+0032 (the ASCII digit 2), U+FE0F (variation selector), and U+20E3 (combining enclosing keycap).\n- The flag of Scotland 7 component characters, `b'\\\\U0001f3f4\\\\U000e0067\\\\U000e0062\\\\U000e0073\\\\U000e0063\\\\U000e0074\\\\U000e007f'` in full esaped notation.\n\n(You can see any of these through `s.encode(\"unicode-escape\")`.)\n\n`demoji` is careful to handle this and should find the full sequences rather than their incomplete subcomponents.\n\nThe 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(N<sup>2</sup>) properties, but the focus is on accuracy and completeness.\n\n```python\n>>> from pprint import pprint\n>>> seq = \"\"\"\\\n... I bet you didn't know that \ud83d\ude4b, \ud83d\ude4b\u200d\u2642\ufe0f, and \ud83d\ude4b\u200d\u2640\ufe0f are three different emojis.\n... \"\"\"\n>>> pprint(seq.encode('unicode-escape')) # Python 3\n(b\"I bet you didn't know that \\\\U0001f64b, \\\\U0001f64b\\\\u200d\\\\u2642\\\\ufe0f,\"\n b' and \\\\U0001f64b\\\\u200d\\\\u2640\\\\ufe0f are three different emojis.\\\\n')\n```\n\n# Changelog\n\n## 1.1.0\n\n- Add a `__main.py__` to allow running `python -m demoji`;\n add an entry-point `demoji` command;\n permit stdin (`-`), file name(s), or piped stdin.\n Contribution by @jap.\n\n## 1.0.0\n\n**This is a backwards-incompatible release with several substantial changes.**\n\nThe largest change is that `demoji` now bundles a static copy of Unicode\nemoji data with the package at install time, rather than requiring a runtime\ndownload of the codes from unicode.org.\n\nChanges below are grouped by their corresponding\n[Semantic Versioning](https://semver.org/) identifier.\n\nSemVer MAJOR:\n\n- Drop support for Python 2 and Python 3.5\n- The `demoji` package now bundles emoji data that is distributed with the\n package at install time, rather than requiring a download of the codes\n from the unicode.org site at runtime (closes #23)\n- As a result of the above change, the following functions are **removed**\n from the `demoji` API:\n - `download_codes()`\n - `parse_unicode_sequence()`\n - `parse_unicode_range()`\n - `stream_unicodeorg_emojifile()`\n\nSemVer MINOR:\n\n- The `demoji.DIRECTORY` and `demoji.CACHEPATH` attributes are deprecated\n due to no longer being functionally in used by the package. Accessing them\n will warn with a `FutureWarning`, and these attributes may be removed\n completely in a future release\n- `demoji` can now be installed with optional `ujson` support for faster loading\n of emoji data from file (versus the standard library's `json`, which is the\n default); use `python -m pip install demoji[ujson]`\n- The dependencies `requests` and `colorama` have been removed completely\n- `importlib_resources` (a backport module) is now required for Python < 3.7\n- The `EMOJI_VERSION` attribute, newly added to `demoji`, is a `str` denoting\n the Unicode database version in use\n\nSemVer PATCH:\n\n- Fix a typo in `demoji.__all__` to properly include `demoji.findall_list()`\n- Internal change: Functions that call `set_emoji_pattern()` are now decorated\n with a `@cache_setter` to set the cache\n- Some unit tests have been removed to update the change in behavior from\n downloading codes to bundling codes with install\n- Update README to reflect bundling behavior\n\n## 0.4.0\n\n- Update emoji source list to version 13.1. (See 5090eb5.)\n- Formally support Python 3.9. (See 6e9c34c.)\n- Bugfix: ensure that `demoji.last_downloaded_timestamp()` returns correct UTC time.\n (See 6c8ad15.)\n\n## 0.3.0\n\n- Feature: add `findall_list()` and `replace_with_desc()` functions. (See 7cea333.)\n- Modernize setup config to use `setup.cfg`. (See 8f141e7.)\n\n## 0.2.1\n\n- Tox: formally add Python 3.8 tests.\n\n## 0.2.0\n\n- Windows: use the [colorama] package to support printing ANSI escape sequences on Windows;\n this introduces colorama as a dependency. (See cd343c1.)\n- Setup: Fix a bug in `setup.py` that would require dependencies to be installed\n _prior to_ installation of `demoji` in order to find the `__version__`.\n (See d5f429c.)\n- Python 2 + Windows support: use `io.open(..., encoding='utf-8')` consistently in `setup.py`.\n (See 1efec5d.)\n- Distribution: use a universal wheel in PyPI release. (See 8636a32.)\n\n[colorama]: https://github.com/tartley/colorama\n\n## 0.1.5\n\n- Performance improvement: use `re.escape()` rather than failing to compile a small subset of codes.\n- Remove an unused constant in `__init__.py`.\n\n\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Accurately remove and replace emojis in text strings",
"version": "1.1.0",
"project_urls": {
"Bug Reports": "https://github.com/bsolomon1124/demoji/issues",
"Documentation": "https://github.com/bsolomon1124/demoji/blob/master/README.md",
"Homepage": "https://github.com/bsolomon1124/demoji",
"Source": "https://github.com/bsolomon1124/demoji"
},
"split_keywords": [
"emoji",
"emojis",
"nlp",
"natural langauge processing",
"unicode"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "03669dc4b6d57f3a74ad8cf79f0cc4e965165871bfb3f612db77ccd4e0200b38",
"md5": "51e0d6c9d964248ab65c8dda565da1a9",
"sha256": "6d3256c909aea299e97fe984f827a2a060c2a8f8bfcbafa7ec9659967c5df50f"
},
"downloads": -1,
"filename": "demoji-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "51e0d6c9d964248ab65c8dda565da1a9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 42851,
"upload_time": "2021-08-29T19:22:40",
"upload_time_iso_8601": "2021-08-29T19:22:40.780926Z",
"url": "https://files.pythonhosted.org/packages/03/66/9dc4b6d57f3a74ad8cf79f0cc4e965165871bfb3f612db77ccd4e0200b38/demoji-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9d62e6de96cf1ef2c6ac91b84a51af151d791f874529d8b146d3587771d05727",
"md5": "de7bc1c03d0b947a445b7de4e4ac7ed3",
"sha256": "072efaeca725e6f63ab59d83abeb55b178842538ed9256455a82ebbd055ff216"
},
"downloads": -1,
"filename": "demoji-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "de7bc1c03d0b947a445b7de4e4ac7ed3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 46253,
"upload_time": "2021-08-29T19:22:42",
"upload_time_iso_8601": "2021-08-29T19:22:42.695493Z",
"url": "https://files.pythonhosted.org/packages/9d/62/e6de96cf1ef2c6ac91b84a51af151d791f874529d8b146d3587771d05727/demoji-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-08-29 19:22:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bsolomon1124",
"github_project": "demoji",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"requirements": [],
"tox": true,
"lcname": "demoji"
}