The ANEW (Affective Norms for English Words) is a dataset that puts a valence, or mood to words. Using this (all be it from 1999) data, we can derive the mood of a body of text, such as a Tweet - however first things first, lets get the data into a usable form and learn how we can make use of it.

First of all we need to use a few scripts from a package called begin_anew that will allow use to extract the ANEW tables from the PDF published in 1999. If you follow the instructions on the begin_anew site, you should have three CSV files (all.csv, female.csv and male.csv) - we'll be using all.csv, as we can not be 100% sure that we will receive a gender with every Tweet. If you cannot follow the instructions on the begin_anew site, here is a copy of all.csv.

Now that we have our list of valence ratings for each word, we can get down to work. How we can use these values is detailed in this article, however for those that would like to get down and dirty with the minimum of reading, here's a break down of what we will be doing.

Read our CSV file into an array using a PHP script for reference. Each entry in this array will be another array, consisting of a valence value, and a count value

Take a body of text, and using REGEX break it into component words

Loop through these words, checking to see if there is a key in the valence array for the lowercase of the word. If there is, increase that words count by 1 in the valence array

Calculate the total valence by looping through your valence array and adding each value of (count*valence) to a variable - let's call this totalValence

Calculate the total valence hit count by looping through your valence array and adding each valence count to a variable - let's call this totalHits

Calculate the valence of the body of text by performing the following operation - (1/totalHits)*totalValence - let's call this endValence

Once you have completed the above steps, your variable endValence will hold the valence value for the body of text. This will range between 0 and 10, depending on the mood of the text - the lower the valence, the greater the negativity of the text.

There you have it, we can now determine the mood of a body of text, using the ANEW tables! Below is some example PHP source that will show the above techniques in action.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?php

//read in and store the ANEW data in the 2d array

$valenceArray=array();

$lines=file("anew.csv");

foreach($linesas$line_num=>$line){

if($line_num>1){

$lineSplit=explode(",",$line);

if(count($lineSplit)>1){

$valenceArray[$lineSplit[0]]=array("v"=>$lineSplit[2],"count"=>0);

}

}

}

//split our string into word components with REGEX, and then loop over, check against

//ANEW data, and increase ANEW count if the word exists

$stringToCheck="this lovely string will amazingly resulting in a very high and happy valence";