Search code examples
pythonpython-3.xunicodeemoji

How to identify same emoji of different colors/skin tone in python?


How to identify same emoji with different colours?

Example: šŸ‘šŸ» šŸ‘šŸ½ šŸ‘ the should be considered as being the same

Edit: Currently I am using emoji package

import regex
import emoji
exm = "poli kariku fans adi like šŸ‘šŸ» šŸ‘šŸ½ šŸ‘ sub tharamo"
characters = regex.findall("\X",exm)
for char in character:
  if char in emoji.UNICODE_EMOJI:
     #do something

Solution

  • I don't think you need to use regex for your use case you can just use the length of the emoji:

    import emoji
    
    NUM_COLUMNS = 4
    TABLE_COLUMN_WIDTH = 18
    
    def is_emoji(s):
        return s in emoji.UNICODE_EMOJI
    
    def is_default_emoji(s):
        return len(s) == 1 and s in emoji.UNICODE_EMOJI
    
    def get_default_emoji(s):
        return s[0] if s in emoji.UNICODE_EMOJI else None
    
    def pretty_print_line(line):
        print(''.join(word.ljust(TABLE_COLUMN_WIDTH) for word in line.split()))
    
    text = "poli kariku fans adi like šŸ‘šŸ» šŸ‘šŸ¼ šŸ‘šŸ½ šŸ‘šŸ¾ šŸ‘šŸæ šŸ‘ sub tharamo"
    
    pretty_print_line("string is_emoji is_default_emoji get_default_emoji")
    print("=" * NUM_COLUMNS * TABLE_COLUMN_WIDTH)
    for s in text.split():
        pretty_print_line(f'{s} {is_emoji(s)} {is_default_emoji(s)} {get_default_emoji(s)}')
    

    Output:

    string            is_emoji          is_default_emoji  get_default_emoji 
    ========================================================================
    poli              False             False             None              
    kariku            False             False             None              
    fans              False             False             None              
    adi               False             False             None              
    like              False             False             None              
    šŸ‘šŸ»                True              False             šŸ‘                 
    šŸ‘šŸ¼                True              False             šŸ‘                 
    šŸ‘šŸ½                True              False             šŸ‘                 
    šŸ‘šŸ¾                True              False             šŸ‘                 
    šŸ‘šŸæ                True              False             šŸ‘                 
    šŸ‘                True              True              šŸ‘                 
    sub               False             False             None              
    tharamo           False             False             None  
    

    You could use some logic similar to get_default_emoji for your use case since it returns the same emoji regardless of the presence or lack of presence of a skin tone modifier.