Source: https://eventregistry.org/documentation?tab=sentiment

# Sentiment

Sentiment detection using a vocabulary or neural-network based approach.

Compute the sentiment expressed in the text. Depending on your preference you can use the vocabulary based approach, which assigns sentiment based on the sentiment values that are preassigned to individual words, or the neural-network based approach. The returned response contains an average sentiment of the text as well as sentence by sentence sentiment. The sentiment will range between -1 (very negative) and 1 (very positive). Please note that the sentiment classification only works on documents in English language.

**Endpoint:** GET | POST `http://analytics.eventregistry.org/api/v1/sentiment`

**Headers:** Content-Type: application/json

#### `Parameters`

#### `apiKey`

string (required)

Your API key

#### `text`

string (required)

The text that will be used to calculate sentiment.

#### `method`

string

Approach used for detecting sentiment. The text that will be used to calculate sentiment.

Available values: `vocabulary`, `rnn`

Default value: `vocabulary`

#### `sentences`

integer

Number of sentences on which to compute the sentiment.

Default value: `10`

## Request examples

### Example 1

Use the vocabulary based approach to detecting the sentiment mentioned in the input text.

Request body:

```json
{
  "text": "Residents and tourists enjoy holiday weekend even as waves start to pound; beaches remain closed due to dangerous rip currents. Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding, residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm. Cloudy skies in the morning gave way to sunshine in the afternoon, and despite winds that already were kicking up sand and carving the beach, people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach, where children rode amusement rides and teens enjoyed ice cream cones.",
  "method": "vocabulary",
  "sentences": 10,
  "apiKey": "YOUR_API_KEY"
}
```

```python
from eventregistry import *
er = EventRegistry(apiKey = YOUR_API_KEY)
analytics = Analytics(er)

text = """
    Residents and tourists enjoy holiday weekend even as waves start to pound;
    beaches remain closed due to dangerous rip currents.
    Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
    residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
    Cloudy skies in the morning gave way to sunshine in the afternoon,
    and despite winds that already were kicking up sand and carving the beach,
    people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
    where children rode amusement rides and teens enjoyed ice cream cones.
"""

annInfo = analytics.sentiment(text, method = "vocabulary")

print("Average sentiment: %.3f" % (annInfo.get("avgSent", 0)))
print("Sentiment per sentence: " + str(annInfo.get("sentimentPerSent", [])))
```

```javascript
var er = new erBase.EventRegistry();
                    var analytics = new erBase.Analytics(er);
                    var text = `
                        Residents and tourists enjoy holiday weekend even as waves start to pound;
                        beaches remain closed due to dangerous rip currents.
                        Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
                        residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
                        Cloudy skies in the morning gave way to sunshine in the afternoon,
                        and despite winds that already were kicking up sand and carving the beach,
                        people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
                        where children rode amusement rides and teens enjoyed ice cream cones.
                    `;
                    analytics.sentiment(text).then((annInfo) => {
                        console.log(annInfo);
                    });
```

```typescript
const er = new EventRegistry();
                    const analytics = new Analytics(er);
                    var text = `
                        Residents and tourists enjoy holiday weekend even as waves start to pound;
                        beaches remain closed due to dangerous rip currents.
                        Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
                        residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
                        Cloudy skies in the morning gave way to sunshine in the afternoon,
                        and despite winds that already were kicking up sand and carving the beach,
                        people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
                        where children rode amusement rides and teens enjoyed ice cream cones.
                    `;
                    analytics.sentiment(text).then((annInfo) => {
                        console.log(annInfo);
                    });
```

### Example 2

Use the neural-network based approach to detecting the sentiment mentioned in the input text.

Request body:

```json
{
  "text": "Residents and tourists enjoy holiday weekend even as waves start to pound; beaches remain closed due to dangerous rip currents. Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding, residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm. Cloudy skies in the morning gave way to sunshine in the afternoon, and despite winds that already were kicking up sand and carving the beach, people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach, where children rode amusement rides and teens enjoyed ice cream cones.",
  "method": "rnn",
  "sentences": 10,
  "apiKey": "YOUR_API_KEY"
}
```

```python
from eventregistry import *
er = EventRegistry(apiKey = YOUR_API_KEY)
analytics = Analytics(er)

text = """
    Residents and tourists enjoy holiday weekend even as waves start to pound;
    beaches remain closed due to dangerous rip currents.
    Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
    residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
    Cloudy skies in the morning gave way to sunshine in the afternoon,
    and despite winds that already were kicking up sand and carving the beach,
    people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
    where children rode amusement rides and teens enjoyed ice cream cones.
"""

annInfo = analytics.sentiment(text, "rnn")

print("Average sentiment: %.3f" % (annInfo.get("avgSent", 0)))
print("Sentiment per sentence: " + str(annInfo.get("sentimentPerSent", [])))
```

```javascript
var er = new erBase.EventRegistry();
                    var analytics = new erBase.Analytics(er);
                    var text = `
                        Residents and tourists enjoy holiday weekend even as waves start to pound;
                        beaches remain closed due to dangerous rip currents.
                        Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
                        residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
                        Cloudy skies in the morning gave way to sunshine in the afternoon,
                        and despite winds that already were kicking up sand and carving the beach,
                        people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
                        where children rode amusement rides and teens enjoyed ice cream cones.
                    `;
                    analytics.sentiment(text, "rnn").then((annInfo) => {
                        console.log(annInfo);
                    });
```

```typescript
const er = new EventRegistry();
                    const analytics = new Analytics(er);
                    var text = `
                        Residents and tourists enjoy holiday weekend even as waves start to pound;
                        beaches remain closed due to dangerous rip currents.
                        Despite a state of emergency declared by the governor and warnings about dangerous surf and the possibility of significant coastal flooding,
                        residents and visitors to the Jersey Shore spent Saturday making the most of the calm before the storm.
                        Cloudy skies in the morning gave way to sunshine in the afternoon,
                        and despite winds that already were kicking up sand and carving the beach,
                        people flocked to the boardwalk in both Seaside Heights and Point Pleasant Beach,
                        where children rode amusement rides and teens enjoyed ice cream cones.
                    `;
                    analytics.sentiment(text, "rnn").then((annInfo) => {
                        console.log(annInfo);
                    });
```

## Example output

```json
{
  "sentimentPerSent": [
    0.11179642774763501,
    0.111796363309785,
    0.08935511926215499,
    -0.24283100937554503,
    -0.24525503547442,
    0.111796362107285,
    -0.21589841596173504,
    0.08935617730552001,
    0.49281427124118,
    -0.25228517751527496
  ],
  "avgSent": 0.005064508264658501,
  "sentences": [
    "Tweet\n\nGranite  Oil  Corp  (TSE:GXO)  had  its  price  target  hoisted  by  analysts  at  National  Bank  Financial  from  C$9.50  to  C$10.50  in  a  report  released  on  Wednesday.",
    "National  Bank  Financial's  price  target  would  indicate  a  potential  upside  of  66.93%  from  the  company's  current  price.",
    "Granite  Oil  Corp  (TSE:GXO)  traded  up  1.29%  during  midday  trading  on  Wednesday,  hitting  $6.29.",
    "The  stock  had  a  trading  volume  of  59,336  shares.",
    "Granite  Oil  Corp  has  a  52  week  low  of  $6.05  and  a  52  week  high  of  $9.39.",
    "The  company's  market  cap  is  $211.31  million.",
    "The  stock's  50  day  moving  average  price  is  $7.06  and  its  200-day  moving  average  price  is  $7.16.",
    "This  story  is  the  sole  property  of  American  Banking  News  and  it  was  originally  published  by  American  Banking  News.",
    "If  you  are  reading  this  story  on  another  website,  that  means  this  article  was  illegally  copied  and  re-published  to  this  website  in  violation  of  U.S.  and  International  copyright  law.",
    "The  original  version  of  this  article  is  available  at  http://www.americanbankingnews.com/2016/09/04/granite-oil-corp-gxo-pt-raised-to-c10-50/\n\nOther  research  analysts  have  also  issued  research  reports  about  the  stock."
  ]
}
```

## Output schema

```json
{
  "type": "object",
  "properties": {
    "avgSent": {
      "type": "double"
    },
    "sentimentPerSent": {
      "type": "array",
      "items": {
        "type": "double"
      }
    }
  }
}
```
