sentiment icon indicating copy to clipboard operation
sentiment copied to clipboard

Support smileys

Open elyas-bhy opened this issue 7 years ago • 1 comments

Currently we have no way of processing smileys since they are stripped away before analysis (in tokenize.js). Such a feature serves the same purpose as supporting emojis.

For example, here is a non-exhaustive list of smileys:

  • :)
  • :(
  • :D
  • :-D
  • ;)
  • :p

elyas-bhy avatar Feb 05 '18 17:02 elyas-bhy

I did a function that does that

function getSmileys(sPhrase) {

    let inlineSmileys = new RegExp(/(<[\/\\]?3|[()/|*$][-^]?[:;=]|x[d()]|\^[a-z._-]{0,1}\^['"]{0,1}|[:;=B8][\-^]?[3DOPp@$*\\)(\/|])(?=\s|[!.?]|$)/, 'gim'); // detect smileys like :) ;) :p :/ =/ :-) :( :D xD :-) ^^

    const smileyValues = {
        ':)': 5,
        ';)': 5,
        ':(': -5,
        'x)': 5,
        ':p': 4,
        ':o': -2,
        ':3': -3,
        ':|': -4,
        ':/': -4,
        ':\\': -4,
        ':$': -3,
        ':*': 5,
        ':@': -3,
        ':-)': 5,
        ';-)': 5,
        ':-(': -5,
        ':-p': 4,
        ':-o': -2,
        ':-3': -3,
        ':-|': -4,
        ':-/': -4,
        ':-\\': -4,
        ':-$': -3,
        ':-*': 4,
        ':-@': -2,
        '(:': 5,
        '):': -5,
        '(x': 5,
        '$:': -3,
        '*:': 5,
        '/:': -4,
        '\\:': -4,
        '(-:': 5,
        ')-:': -5,
        '$-:': -3,
        '*-:': 5,
        '/-:': -4,
        '\\-:': -4,
        '<3': 5,
        '</3': -5,
        '<\\3': -5,
        '^^': 3,
        '^.^': 3,
        '^o^': 5,
        '^-^': 3,
        '^_^': 3,
        '^^"': -3,
        "^^'": -3,
        'xd': 4
    };

    let smileyArray;
    while ((smileyArray = inlineSmileys.exec(sentence.toLocaleLowerCase())) !== null) { // convert to lowercase for smileys like :s :S

        const smileyScore = smileyValues[smileyArray[0]]; // get the smiley score
        iGlobalScore += Number(smileyScore); // add the score to the global score

        // add the smiley into the positive/negative arrays
        if (smileyScore > 0) {
            aPositive.push(String(smileyArray[0]));
        } else {
            aNegative.push(String(smileyArray[0]));
        }
    }
}

It's better to call that function after the negation detection, because even in a sentence with a negation, a smiley like :) is still positive.

mathildebuenerd avatar May 08 '18 07:05 mathildebuenerd