3

I am trying to create a function such that I pass a full string into it and it finds and returns any emojis present. For instance, if there are 2 emojis, it should return both. How can I do so?

Currently, I can only understand how to check for one particular emoji. This is a function I use to check it:

def check(string):
    if '✅' in string:
        print('found', string)

I want to do this without specifying any emoji and just look for all. I have considered from emoji import UNICODE_EMOJI.

import emoji
import regex

def split_count(text):
    emoji_counter = 0
    data = regex.findall(r'\X', text)
    for word in data:
        if any(char in emoji.UNICODE_EMOJI for char in word):
            emoji_counter += 1
            # Remove from the given text the emojis
            text = text.replace(word, '') 

    words_counter = len(text.split())

    return emoji_counter, words_counter

Although this gives us a count, I am not sure how to modify it to get all emojis.

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67

2 Answers2

2

You can check if the letter is in emoji.UNICODE_EMOJI:

import emoji

def get_emoji_list(text):
    return [letter for letter in text if letter in emoji.UNICODE_EMOJI]

print(get_emoji_list('✅aze✅'))
# ['✅', '✅']

If you want a set of unique emoji, change your comprehension in the function to create a set instead of a list:

import emoji

def get_emoji_set(text):
    return {letter for letter in text if letter in emoji.UNICODE_EMOJI}

print(get_emoji_list('✅aze✅'))
# {'✅'}
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
  • 2
    A warning to the OP: Looks like `UNICODE_EMOJI` is going to be a `dict` of `dict`s in a future release (the outer `dict` maps language codes to the per-language `dict` you're currently using) thanks to [this pull request](https://github.com/carpedm20/emoji/pull/137). You might want to do something like `from emoji import UNICODE_EMOJI`, then do `try:`, `UNICODE_EMOJI = UNICODE_EMOJI['en']`, `except KeyError: pass` to ensure it works as expected in the future (the `try`/`except` will fire and do nothing now, and look up the English `dict` in the future). – ShadowRanger Nov 30 '20 at 20:25
  • Instead of `UNICODE_EMOJI["en*]`, you can use `.get(...)` and avoid using try except – Dorian Turba Dec 01 '20 at 08:01
1

This emoji_finder method yields the words where an emoji is found. So the generator object can be converted to a list and used wherever you want.

import emoji
import regex

def emoji_finder(text):
    emoji_counter = 0
    data = regex.findall(r'\X', text)
    for word in data:
        if any(char in emoji.UNICODE_EMOJI for char in word):
            emoji_counter += 1
            text = text.replace(word, '') 
            yield word

print(list(split_count(stringWithEmoji))) #prints all the emojis in stringWithEmoji
theWellHopeErr
  • 1,856
  • 7
  • 22