3

I have a dataset and ,when I am encountering an emoticons without any space , I want to place space around them but I don't know few things

  • I have this list

    sentences=["HiRob","swiggy",""].
    

    how to compare them as they are stored as strings.

  • How to put spaces around them?

    Desired output

    sentences=["HiRob  ","swiggy","   "]
    

My basic problem is to put spaces around them.

Beginner
  • 721
  • 11
  • 27
  • 2
    Your output is inconsistent: Why is there no space _after_ the emoji in the first string, but both before and after (not just inbetween) the two emojis in the last string? – L3viathan Sep 03 '18 at 09:08
  • My primary purpose is just to put space between emoji and other characters, having space at the end is not a necessary condition ,Let me edit to make it consistent – Beginner Sep 03 '18 at 09:10
  • [this thread](https://stackoverflow.com/questions/33404752/removing-emojis-from-a-string-in-python) seems to be related – Azat Ibrakov Sep 03 '18 at 09:12

1 Answers1

2

You can use the emoji package to simplify a bit your code.

from emoji import UNICODE_EMOJI

# search your emoji
def is_emoji(s, language="en"):
    return s in UNICODE_EMOJI[language]

# add space near your emoji
def add_space(text):
    return ''.join(' ' + char if is_emoji(char) else char for char in text).strip()

sentences=["HiRob","swiggy",""]
results=[add_space(text) for text in sentences]

print(results)

output

['HiRob ', 'swiggy', ' ']

Try it online!

related to: How to extract all the emojis from text?

if add_space looks like black magic, here is a friendlier alternative:

def add_space(text):
  result = ''
  for char in text:
    if is_emoji(char):
      result += ' '
    result += char
  return result.strip()
Maxim Kirilov
  • 2,639
  • 24
  • 49
aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • There is an issue ,it works fine for the emoticons but now,it is removing the white spaces for the regular sentences.The output for `[Hi Rob]` is `[HiRob]` – Beginner Sep 03 '18 at 09:36
  • Thanks, I am new to python so I have another doubt, could you elaborate this line more `return ''.join(' ' + char if is_emoji(char) else char for char in text).strip()` .I am getting ,you are adding a space using a join if it is emoji but I want to see your thought process – Beginner Sep 03 '18 at 09:46
  • 1
    ok I will add a beginner alternative to add_space. Check the update. – aloisdg Sep 03 '18 at 09:51
  • 1
    There is a problem in is_emoji function. you need to change it this way -> from emoji import unicode_codes def is_emoji(s): return s in unicode_codes.UNICODE_EMOJI['en'] – A.Najafi May 13 '21 at 14:01
  • @A.Najafi could you post a link to showcase the error? I cant try this code right now. – aloisdg May 13 '21 at 15:13
  • @aloisdg Sure => https://colab.research.google.com/drive/1CGmHBEYWLU3ayDeTmisOZkbF5WOn6Y5Z?usp=sharing – A.Najafi May 14 '21 at 07:17