Skip to main content

Basic usage script for LabMT1.0 dataset

Project description

TL;DR a simple labMT usage script

This script uses the language assessment by Mechanical Turk (labMT) word list to score the happiness of a corpus. The labMT word list was created by combining the 5000 words most frequently appearing in four sources: Twitter, the New York Times, Google Books, and music lyrics, and then scoring the words for sentiment on Amazon’s Mechanical Turk. The list is described in detail in the publication Dodds’ et al. 2011, PLOS ONE, “Temporal Patterns of Happiness and Information in a Global-Scale Social Network: Hedonometrics and Twitter.”

Given two corpora, the script “storylab.py” creates a word-shift graph illustrating the words most responsible for the difference in happiness between the two corpora. The corpora should be large (e.g. at least 10,000 words) in order for the difference to be meaningful, as this is a bag-of-words approach. As an example, a random collection of English tweets from both Saturday January 18 2014 and Tuesday January 21 2014 are included in the “test” directory. They can be compared by moving to the test directory, using the command

python test.py

and opening the file shiftPlot.html. For an explanation of the resulting plot, please visit

http://www.hedonometer.org/shifts.html

Usage

The Python script test.py uses this module to test a subsample of Twitter data:

from storyLab import *
labMT,labMTvector,labMTwordList = emotionFileReader(returnVector=True)

## take a look at these guys
print labMT['laughter']
print labMTvector[0:5]
print labMTwordList[0:5]

## test shift a subsample of two twitter days
import codecs ## handle utf8
f = codecs.open("25.01.14.txt","r","utf8")
saturday = f.read()
f.close()
f = codecs.open("28.01.14.txt","r","utf8")
tuesday = f.read()
f.close()

## compute valence score
saturdayValence = emotion(saturday,labMT)
tuesdayValence = emotion(tuesday,labMT)
print 'the valence of {0} is {1}'.format('saturday',saturdayValence)
print 'the valence of {0} is {1}'.format('tuesday',tuesdayValence)

## compute valence score and return frequency vector for generating wordshift
saturdayValence,saturdayFvec = emotion(saturday,labMT,shift=True,happsList=labMTvector)
tuesdayValence,tuesdayFvec = emotion(tuesday,labMT,shift=True,happsList=labMTvector)

## make a shift: shift(values,ref,comp)
shiftMag,shiftType = shift(labMTvector,tuesdayFvec,saturdayFvec)
## take the absolute value of the shift magnitude
shiftMagAbs = map(abs,shiftMag)

## sort them both
indices = sorted(range(len(shiftMag)), key=shiftMagAbs.__getitem__, reverse=True)
sortedMag = [shiftMag[i] for i in indices]
sortedType = [shiftType[i] for i in indices]
sortedWords = [labMTwordList[i] for i in indices]

## take a peek at the top words
print indices[0:10]
print sortedMag[0:20]
print sortedType[0:20]
print sortedWords[0:20]

## print each of these to a file
f = open("sampleSortedMag.csv","w")
for val in sortedMag:
  f.write(str(val))
  f.write("\n")
f.close()

f = open("sampleSortedType.csv","w")
for val in sortedType:
  f.write(str(val))
  f.write("\n")
f.close()

f = open("sampleSortedWords.csv","w")
for val in sortedWords:
  f.write(val)
  f.write("\n")
f.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

labMTsimple-1.tar.gz (11.1 MB view details)

Uploaded Source

File details

Details for the file labMTsimple-1.tar.gz.

File metadata

  • Download URL: labMTsimple-1.tar.gz
  • Upload date:
  • Size: 11.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for labMTsimple-1.tar.gz
Algorithm Hash digest
SHA256 47ff7d1cbee467a8eb78f28c73ba54fc10602bbf4b83b5e81850410d3dd7a762
MD5 2a455c73a2cae5021949f693f732a391
BLAKE2b-256 32554cc3165ffdba37463c0fa45eae7ee3bb71cad11e7789176317349ad20038

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page