Main Content

Analyze Text Data Containing Emojis

This example shows how to analyze text data containing emojis.

Emojis are pictorial symbols that appear inline in text. When writing text on mobile devices such as smartphones and tablets, people use emojis to keep the text short and convey emotion and feelings.

You also can use emojis to analyze text data. For example, use them to identify relevant strings of text or to visualize the sentiment or emotion of the text.

When working with text data, emojis can behave unpredictably. Depending on your system fonts, your system might not display some emojis correctly. Therefore, if an emoji is not displayed correctly, then the data is not necessarily missing. Your system might be unable to display the emoji in the current font.

Composing Emojis

In most cases, you can read emojis from a file (for example, by using extractFileText, extractHTMLText, or readtable) or by copying and pasting them directly into MATLAB®. Otherwise, you must compose the emoji using Unicode UTF16 code units.

Some emojis consist of multiple Unicode UTF16 code units. For example, the "smiling face with sunglasses" emoji (😎 with code point U+1F60E) is a single glyph but comprises two UTF16 code units "D83D" and "DE0E". Create a string containing this emoji using the compose function, and specify the two code units with the prefix "\x".

emoji = compose("\xD83D\xDE0E")
emoji = 
"😎"

First get the Unicode UTF16 code units of an emoji. Use char to get the numeric representation of the emoji, and then use dec2hex to get the corresponding hex value.

codeUnits = dec2hex(char(emoji))
codeUnits = 2×4 char array
    'D83D'
    'DE0E'

Reconstruct the composition string using the strjoin function with the empty delimiter "".

formatSpec = strjoin("\x" + codeUnits,"")
formatSpec = 
"\xD83D\xDE0E"
emoji = compose(formatSpec)
emoji = 
"😎"

Import Text Data

Extract the text data in the file weekendUpdates.xlsx using readtable. The file weekendUpdates.xlsx contains status updates containing the hashtags "#weekend" and "#vacation".

filename = "weekendUpdates.xlsx";
tbl = readtable(filename,'TextType','string');
head(tbl)
ans=8×2 table
    ID                                         TextData                                     
    __    __________________________________________________________________________________

    1     "Happy anniversary! ❤ Next stop: Paris! ✈ #vacation"                              
    2     "Haha, BBQ on the beach, engage smug mode! 😍 😎 ❤ 🎉 #vacation"                  
    3     "getting ready for Saturday night 🍕 #yum #weekend 😎"                            
    4     "Say it with me - I NEED A #VACATION!!! ☹"                                        
    5     "😎 Chilling 😎 at home for the first time in ages…This is the life! 👍 #weekend"
    6     "My last #weekend before the exam 😢 👎."                                         
    7     "can’t believe my #vacation is over 😢 so unfair"                                 
    8     "Can’t wait for tennis this #weekend 🎾🍓🥂 😀"                                   

Extract the text data from the field TextData and view the first few status updates.

textData = tbl.TextData;
textData(1:5)
ans = 5×1 string
    "Happy anniversary! ❤ Next stop: Paris! ✈ #vacation"
    "Haha, BBQ on the beach, engage smug mode! 😍 😎 ❤ 🎉 #vacation"
    "getting ready for Saturday night 🍕 #yum #weekend 😎"
    "Say it with me - I NEED A #VACATION!!! ☹"
    "😎 Chilling 😎 at home for the first time in ages…This is the life! 👍 #weekend"

Visualize the text data in a word cloud.

figure
wordcloud(textData);

Filter Text Data by Emoji

Identify the status updates containing a particular emoji using the contains function. Find the indices of the documents containing the "smiling face with sunglasses" emoji (😎 with code U+1F60E). This emoji comprises the two Unicode UTF16 code units "D83D" and "DE0E".

emoji = compose("\xD83D\xDE0E");
idx = contains(textData,emoji);
textDataSunglasses = textData(idx);
textDataSunglasses(1:5)
ans = 5×1 string
    "Haha, BBQ on the beach, engage smug mode! 😍 😎 ❤ 🎉 #vacation"
    "getting ready for Saturday night 🍕 #yum #weekend 😎"
    "😎 Chilling 😎 at home for the first time in ages…This is the life! 👍 #weekend"
    "🎉 Check the out-of-office crew, we are officially ON #VACATION!! 😎"
    "Who needs a #vacation when the weather is this good ☀ 😎"

Visualize the extracted text data in a word cloud.

figure
wordcloud(textDataSunglasses);

Extract and Visualize Emojis

Visualize all the emojis in text data using a word cloud.

Extract the emojis. First tokenize the text using tokenizedDocument, and then view the first few documents.

documents = tokenizedDocument(textData);
documents(1:5)
ans = 
  5×1 tokenizedDocument:

    11 tokens: Happy anniversary ! ❤ Next stop : Paris ! ✈ #vacation
    16 tokens: Haha , BBQ on the beach , engage smug mode ! 😍 😎 ❤ 🎉 #vacation
     9 tokens: getting ready for Saturday night 🍕 #yum #weekend 😎
    13 tokens: Say it with me - I NEED A #VACATION ! ! ! ☹
    19 tokens: 😎 Chilling 😎 at home for the first time in ages … This is the life ! 👍 #weekend

The tokenizedDocument function automatically detects emoji and assigns the token type "emoji". View the first few token details of the documents using the tokenDetails function.

tdetails = tokenDetails(documents);
head(tdetails)
ans=8×5 table
        Token        DocumentNumber    LineNumber       Type        Language
    _____________    ______________    __________    ___________    ________

    "Happy"                1               1         letters           en   
    "anniversary"          1               1         letters           en   
    "!"                    1               1         punctuation       en   
    "❤"                    1               1         emoji             en   
    "Next"                 1               1         letters           en   
    "stop"                 1               1         letters           en   
    ":"                    1               1         punctuation       en   
    "Paris"                1               1         letters           en   

Visualize the emojis in a word cloud by extracting the tokens with token type "emoji" and inputting them into the wordcloud function.

idx = tdetails.Type == "emoji";
tokens = tdetails.Token(idx);
figure
wordcloud(tokens);
title("Emojis")

See Also

| |

Related Topics