Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smiple Question #107

Closed
rickyplc opened this issue Feb 26, 2017 · 6 comments
Closed

Smiple Question #107

rickyplc opened this issue Feb 26, 2017 · 6 comments
Labels

Comments

@rickyplc
Copy link

This is a very sweet project!

I would like to know how can I convert the comparative value to a whole number (percentage 100), where 100 is the most positive, 50 is neutral and 0 is most negative?

This can also be out of 10.

@thisandagain
Copy link
Owner

thisandagain commented Feb 26, 2017

The comparative score is simply: sum of each token / number of tokens. So for example let's take the following:

I love cats, but I am allergic to them.

That string results in the following:

{
    score: 1,
    comparative: 0.1111111111111111,
    tokens: [
        'i',
        'love',
        'cats',
        'but',
        'i',
        'am',
        'allergic',
        'to',
        'them'
    ],
    words: [
        'allergic',
        'love'
    ],
    positive: [
        'love'
    ],
    negative: [
        'allergic'
    ]
}

Each token has a range between 5 (positive) and -5 (negative). In this case, love has a value of 3, allergic has a value of -2, and the remaining tokens are neutral with a value of 0. Because the string has 9 tokens the resulting comparative score looks like:

(3 + -2) / 9 = 0.111111111

This approach leaves you with a mid-point of 0 and the upper and lower bounds are constrained to positive and negative 5 respectively (the same as each token! 😸). For example, let's imagine an incredibly "positive" string with 200 tokens and where each token has an AFINN score of 5. Our resulting comparative score would look like this:

(max positive score * number of tokens) / number of tokens
(5 * 200) / 200 = 5

So – to answer your question – you can scale and offset the comparative score (x) however you like using simple arithmetic. For example:

Scale from 0 to 100
(x + 5) * 10

@rickyplc
Copy link
Author

rickyplc commented Feb 26, 2017

Thank you very much! That was both fast and precisely detailed 👍.

And just so that I am clear. Taking the example you gave "I love cats, but I am allergic to them." would be:

Scale from 0 to 100
(x + max AFINN value) * 10
(0.1111111111111111+5)*10

And the value I am after would be 51.11%.

Where 5 would be the maximum AFINN value. Or is there a way to compute this value (max AFINN value) for all comparative scores?

@thisandagain
Copy link
Owner

thisandagain commented Feb 26, 2017

(0.1111111111111111+5)*10

Yes. That's correct. The maximum AFINN value is always 5. To compute this you could do something like:

function scale (score) {
    return (score + 5) * 100;
}

var result = scale(sentiment('I like cats').comparative);

but I would recommend diving into JS to get a better sense of the possibilities:
http://jsforcats.com/
https://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

@rickyplc
Copy link
Author

Ahh yes, Mr Crockford (read that one already :)). Your awesome thanks! I will be able to build out the functionality, I'm not to shabby at Javascript.

And that function will work for all possibilities, including negatives, and neutral values?

@thisandagain
Copy link
Owner

And that function will work for all possibilities, including negatives, and neutral values?

Yup. You can give it a try yourself to see how it works in all scenarios.

@rickyplc
Copy link
Author

Cool – thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants