Re: Javascript Client can't parse emojis (1271 Views)
Reply
Level 1
Posts: 3
Registered: ‎12-28-2016
Message 1 of 6 (1,410 Views)
Accepted Solution

Javascript Client can't parse emojis

I am using the node client for the HavenOnDemand API, which can be found here. More specifically, I am using the sentiment analyzer. If I use the client to analyze a text containing an emoji such as:

 

I love dogs ❤️

I get an error message that reads something like this

Unexpected token \u000f

 

When I go to the HavenOnDemand website and try the API via the web application, I don't get an error.

Is there a workaround/special string encoding that I have to do to ensure that the node client does not fail on an emoji? I want the emoji because it can add more clarity to the sentiment.

Thanks!

Original Stackoverflow question: link

Community Manager
Posts: 82
Registered: ‎06-18-2015
Message 2 of 6 (1,403 Views)

Re: Javascript Client can't parse emojis

Hi,

 

Let me know how do you pass the text to the API so I can help. I could call the API from Terminal with the text "I love dogs ❤️" and it works well.

 

Kind regards,

Paco - HPE Developer Evangelist

Level 1
Posts: 3
Registered: ‎12-28-2016
Message 3 of 6 (1,380 Views)

Re: Javascript Client can't parse emojis

I use this Twitter Wrapper Client to retrieve tweets and then I use the Haven On Demand Node Client to pass the text of those tweets to the batchjob function

During debugging I hardcoded the string '❤︎' as the text for the tweets only to get that error message.

I am also using ES6/7 syntax via babel-core/register if that makes a difference.

Community Manager
Posts: 82
Registered: ‎06-18-2015
Message 4 of 6 (1,377 Views)

Re: Javascript Client can't parse emojis

Hi Again,

 

I think you have to use UTF-8 encoding for that and this is the one for the emoji heart.

 

Try this at your end

var callback = function(err,resp,body){
  console.log(body)

}

var data = {'text', 'this is emoji heart \xE2\x9D\xA4 ' }

client.get('analizesentiment', data, false, callback)

 

Kind regards,

Paco

Level 1
Posts: 3
Registered: ‎12-28-2016
Message 5 of 6 (1,280 Views)

Re: Javascript Client can't parse emojis

[ Edited ]

Hello Paco,

Something unrelated but useful in this context is how convert between the two? For instance, how do I turn 

 

this is emoji heart ❤️️

 

into

 

this is emoji heart \xE2\x9D\xA4

 

I tried using Javascript's encodeURI function but it doesn't produce the output that would work. Thanks!

 

Highlighted
Community Manager
Posts: 82
Registered: ‎06-18-2015
Message 6 of 6 (1,271 Views)

Re: Javascript Client can't parse emojis

Hi lawrencetvo,

 

That encodeURI won't work. It's more complecated than that. I found this website which lets you try all types of conversions in JavaScrip that you may find it useful for your usecase. I think the conversionfunctions.js is GNU license so you can modify and use it.

 

Check it out at: https://r12a.github.io/apps/conversion/

 

Kind regards,

Paco   

† The opinions expressed above are the personal opinions of the authors, not of HPE. By using this site, you accept the Terms of Use and Rules of Participation