Activity 8: Interactive Storytelling w/ Sentiment Analysis
Due: On git by Thursday, Oct. 20, 2016 at 11:00am
Team: This is a solo assignment; you should type all the code to your solution. However, feel free to get help from others/Google/etc.
Grading: This assignment is the mid-week activity for Experience 8, worth 10 points. There is no partial credit.

Introduction

In Activity 7, you wrote an interactive story. In doing so, you built a graph as well as a large amount of text. In this activity, you will use Natural Language Processing (NLP) to find the sentiment of paths through your story.


Part 1: Getting Ready

In order to complete Activity 8, your Activity 7 must be complete and computed. You must have a if.json file in exp_activity7/res in order to complete Activity 8. (The if.json is generated when you run your compute.py file when your story is complete.)

Additionally, you need to merge the files for this activity into your workbook:

git fetch release
git merge release/exp_activity8 master -m "merge"

Part 2: Programming

Using NLTK, add the sentiment for each piece of text in of your story (both the narrative for nodes and the text for edges). Store this sentiment of each piece of text as the attribute sentiment in each node and edge.

To do this, compute.py already has code to load your story, as a graph, from the JSON that was saved as part of the previous activity. As a strategy, you will need to only two things:

  • Loop through every node, performing sentiment analysis on the text contained in the narrative attribute and saving the sentiment in the sentiment attribute.
  • Loop through every edge, performing sentiment analysis on the text contained in the text attribute and saving the sentiment in the sentiment attribute.

Part 2.1: Doing Sentiment Analysis

To perform the sentiment analysis, you can refer to demo_peopleInHungerGames. Specifically, compute.py:40 contains the following logic that you can use to compute sentiment:

from nltk.sentiment.vader import SentimentIntensityAnalyzer

def getSentenceSentiment(sents):
  print("Doing sentiment analysis...")
  sid = SentimentIntensityAnalyzer()
  sentiment = []
  for s in sents:
    sentiment.append( sid.polarity_scores(s)["compound"] )
  return sentiment
(Note: This code cannot be used directly, but should be applied to your program to compute the sentiment of your text.)

Part 2.2: Looping through Nodes and Edges

Using networkx, you can loop through each node via a for-loop:

for node in G.nodes(data=True):
  # `node` is a node in G
  # `node[0]` contains the key of the node
  # `node[1]` contains a dictionary of the atrributes of the node
  #    (ex: node[1]["narrative"] contains your narrative)

Likewise, you can loop through each edge via a similar for-loop:

for edge in G.edges(data=True):
  # `edge` is an edge in G
  # `edge[0]` contains the key of the source node for the edge
  # `edge[1]` contains the key of the destination node for the edge
  # `edge[2]` contains a dictionary of the attributes of the edge
  #    (ex: edge[2]["text"] contains your edge text)

The dictionary of attributes is both readable and writable, allowing you to add a new key to store the sentiment.


Part 3: View a graph of your story

After you story is complete, use the Workbook to view a graph of your story. You will need to compute (by clicking compute in the workbook) to generate the JSON that will be used to render your story. Once complete, viewing your visualization will allow you to explore your story.

In the visualization, you can scroll over each node and edge to find out details about your story. Every node and edge must have a sentiment. For example, here is a node from my story:

As part of exploring the graph you created, ensure that:

  1. Your graph is connected the way you intended. If your graph structure is wrong, update it in Activity 7 and recompute both Activity 7 and Activity 8.
  2. Every node and edge has a sentiment.

Submission

This activity is submitted digitally via git. View detailed submission instructions here.