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

[1.1.4] Load a different lexicon #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ console.log(intensity);
// {neg: 0.0, neu: 0.299, pos: 0.701, compound: 0.8545}
```

Using a different lexicon:
```
const customLexicon = {
'$:': -1.4,
'hand': 2.1,
...
};

vader.SentimentIntensityAnalyzer.loadLexicon(customLexicon);
```

#### About the Scoring

* The ``compound`` score is computed by summing the valence scores of each word in the lexicon, adjusted according to the rules, and then normalized to be between -1 (most extreme negative) and +1 (most extreme positive). This is the most useful metric if you want a single unidimensional measure of sentiment for a given sentence. Calling it a 'normalized, weighted composite score' is accurate.
Expand Down
2 changes: 1 addition & 1 deletion build/vaderSentiment.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vader-sentiment",
"version": "1.1.3",
"version": "1.1.4",
"description": "javascript port of vader sentiment tool",
"main": "build/vaderSentiment.bundle.js",
"scripts": {
Expand Down
19 changes: 12 additions & 7 deletions src/vaderSentiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// Constants

const LEXICON = require('./vader_lexicon.js').lexicon;
const DEFAULT_LEXICON = require('./vader_lexicon.js').lexicon;

// (empirically derived mean sentiment intensity rating increase for booster words)
export const B_INCR = 0.293;
Expand Down Expand Up @@ -201,7 +201,8 @@ export const SPECIAL_CASE_IDIOMS = {
'hand to mouth': -2
};

// static methods
// set lexicon
let lexicon = DEFAULT_LEXICON;

export const negated = (input_words, include_nt = true) => {
/**
Expand Down Expand Up @@ -363,6 +364,10 @@ export class SentimentIntensityAnalyzer {
Give a sentiment intensity score to sentences
*/

static loadLexicon(lexiconVal) {
lexicon = lexiconVal;
}

static polarity_scores(text) {
/**
Return a float for sentiment strength based on the input text.
Expand Down Expand Up @@ -404,9 +409,9 @@ export class SentimentIntensityAnalyzer {
const is_cap_diff = sentiText.is_cap_diff;
const words_and_emoticons = sentiText.words_and_emoticons;
const item_lowercase = item.toLowerCase();
if (LEXICON.hasOwnProperty(item_lowercase)) {
if (lexicon.hasOwnProperty(item_lowercase)) {
// get the sentiment valence
valence = LEXICON[item_lowercase];
valence = lexicon[item_lowercase];
// check if sentiment laden word is in ALL CAPS (while others aren't)
if (is_upper_python(item) && is_cap_diff) {
if (valence > 0) {
Expand All @@ -419,7 +424,7 @@ export class SentimentIntensityAnalyzer {
for (let start_i = 0; start_i < 3; start_i++) {
if (
index > start_i &&
LEXICON.hasOwnProperty(words_and_emoticons[index - (start_i + 1)].toLowerCase()) === false
lexicon.hasOwnProperty(words_and_emoticons[index - (start_i + 1)].toLowerCase()) === false
) {
// dampen the scalar modifier of preceding words and emoticons
// (excluding the ones that immediately preceed the item) based
Expand Down Expand Up @@ -453,7 +458,7 @@ export class SentimentIntensityAnalyzer {
if (
index > 1 &&
words_and_emoticons[index - 1].toLowerCase() === 'least' &&
LEXICON.hasOwnProperty(words_and_emoticons[index - 1].toLowerCase()) === false
lexicon.hasOwnProperty(words_and_emoticons[index - 1].toLowerCase()) === false
) {
if (
words_and_emoticons[index - 2].toLowerCase() !== 'at' &&
Expand All @@ -464,7 +469,7 @@ export class SentimentIntensityAnalyzer {
} else if (
index > 0 &&
words_and_emoticons[index - 1].toLowerCase() === 'least' &&
LEXICON.hasOwnProperty(words_and_emoticons[index - 1].toLowerCase()) === false
lexicon.hasOwnProperty(words_and_emoticons[index - 1].toLowerCase()) === false
) {
valence = valence * N_SCALAR;
}
Expand Down