For a given string, I'm trying to count the number of appearances of each word and emoji. I did it already here for emojis that consists only from 1 emoji. The problem is that a lot of the current emojis are composed from a few emojis.
Like the emoji šØāš©āš¦āš¦ consists of four emojis - šØā š©ā š¦ā š¦, and emojis with human skin color, for example š š½ is š š½ etc.
The problem boils down to how to split the string in the right order, and then counting them is easy.
There are some good questions that addressed the same thing, like link1 and link2 , but none of them applies to the general solution (or the solution is outdated or I just can't figure it out).
For example, if the string would be hello š©š¾āš emoji hello šØāš©āš¦āš¦
, then I'll have {'hello':2, 'emoji':1, 'šØāš©āš¦āš¦':1, 'š©š¾āš':1}
My strings are from Whatsapp, and all were encoded in utf8.
I had many bad attempts. Help would be appreciated.
Use the 3rd party regex module, which supports recognizing grapheme clusters (sequences of Unicode codepoints rendered as a single character):
>>> import regex
>>> s='šØāš©āš¦āš¦š
š½'
>>> regex.findall(r'\X',s)
['šØ\u200dš©\u200dš¦\u200dš¦', 'š
š½']
>>> for c in regex.findall('\X',s):
... print(c)
...
šØāš©āš¦āš¦
š
š½
To count them:
>>> data = regex.findall(r'\X',s)
>>> from collections import Counter
>>> Counter(data)
Counter({'šØ\u200dš©\u200dš¦\u200dš¦': 1, 'š
š½': 1})