To use, only one function needs to be imported:
from replace_emojis import replace_with_emoji
"""
Replaces words in a sentence with corresponding emojis if available.
Args:
sentence (str): The input sentence to process.
debug (bool): Flag to toggle debug print statements. Default is False.
Returns:
str: The sentence with words replaced by emojis where applicable.
"""
Emoji data is taken from the EMOJI_DATA dictionary of the emoji library.
Three match cases are used:
- Exact match of a word to emoji 'EN' language description
- Emoji 'EN' description matches the beggining of a word
- Exact match of a word with emoji 'alias'
Example:
I love omnipotent a Pizza pizza, cats, bobcats, ants, pigs, croissants, and bread thumbs_up thumbsup PIzzaaaa re-Pizzaaaaa!
I love ποΈnipotent
--
For additional debugging print statements:
Setting keyword arg debug=True will output information about the words (tokens) extracted from the text, and the emoji matches and substitutions.
Example:
Tokens: {'re-pizzaaaaa', 'bobcats', 'a', 'i', 'love', 'thumbs_up', 'croissants', 'omnipotent', 'cats', 'ants', 'pigs', 'bread', 'pizzaaaa', 'pizza', 'thumbsup', 'and'}
Emoji Found:
Emoji Found: π, Token: thumbs_up, Match: :thumbs_up:, Sub: :thumbs_up:
Emoji Found: π₯, Token: croissants, Match: π₯, Sub: π₯s
Emoji Found: ποΈ, Token: omnipotent, Match: ποΈ, Sub: ποΈnipotent
Emoji Found: π, Token: cats, Match: π±, Sub: π±s
Emoji Found: π, Token: ants, Match: π, Sub: πs
Emoji Found: π, Token: pigs, Match: π·, Sub: π·s
Emoji Found: π, Token: bread, Match: π, Sub: π
Emoji Found: π, Token: pizzaaaa, Match: π, Sub: πaaa
Emoji Found: π, Token: pizza, Match: π, Sub: π
Emoji Found: π, Token: thumbsup, Match: π, Sub: π
I love omnipotent a Pizza pizza, cats, bobcats, ants, pigs, croissants, and bread thumbs_up thumbsup PIzzaaaa re-Pizzaaaaa!
I love ποΈnipotent