"""Script that generates data about LIWC entities that can be used for
visualizations.
"""

from bs4 import BeautifulSoup
from datetime import datetime
import argparse
import codecs
import os
from collections import Counter

from emotools.bs4_helpers import speaker_turn, sentence, entity

def get_characters(speakerturns):
    """Return a list of characters based a list of speaker turns."""
    characters = Counter()
    for turn in speakerturns:
        # more postprocessing required for character names (character names
        # now sometimes include stage directions)
        actor = turn['actor'].strip()
        characters[actor] += 1
    return characters

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('file_in', help='the name of the FoLiA XML file add '
                        'LIWC entities to')
    args = parser.parse_args()

    file_name = args.file_in
    
    with open(file_name, 'r') as f:
        soup = BeautifulSoup(f, 'xml')

    speakerturns = soup.find_all(speaker_turn)
    characters = get_characters(speakerturns)
    #for char, num in characters.iteritems():
    #    print char, num

    character = 'Eloiza.'

    print 'Sentence,{},{},{}'.format('affect', 'posemo', 'negemo')

    sent = 0
    speaker = 'UNKNOWN'
    count_affect = 0
    count_posemo = 0
    count_negemo = 0
    for elem in soup.descendants:
        if speaker_turn(elem):
            speaker = elem.get('actor').strip()
        elif sentence(elem):
            if count_affect > 0 or count_posemo > 0 or count_negemo > 0:
                if speaker == character:
                    print '{},{},{},{}'.format(sent,
                                               count_affect,
                                               count_posemo,
                                               count_negemo)
                count_affect = 0
                count_posemo = 0
                count_negemo = 0
            sent += 1
        elif entity(elem) and elem.get('class').startswith('liwc-'):
            if elem.get('class') == 'liwc-Affect':
                count_affect += 1
            if elem.get('class') == 'liwc-Posemo':
                count_posemo += 1
            if elem.get('class') == 'liwc-Negemo':
                count_negemo += 1

@@ -216,16 +216,35 @@
  Counter
+%0Aimport numpy as np
 %0A%0Afrom e
@@ -680,16 +680,212 @@
 acters%0A%0A
+%0Adef moving_average(a, n=3):%0A    %22%22%22Calculate the moving average of array a and window size n.%22%22%22%0A    ret = np.cumsum(a, dtype=float)%0A    ret%5Bn:%5D = ret%5Bn:%5D - ret%5B:-n%5D%0A    return ret%5Bn - 1:%5D / n%0A%0A%0A
 if __nam
@@ -1398,24 +1398,25 @@
 oiza.'%0A%0A    
+#
 print 'Sente
@@ -1465,16 +1465,70 @@
 negemo')
+%0A    print 'Sentence,%7B%7D,%7B%7D'.format('affect', 'smooth')
 %0A%0A    se
@@ -1617,24 +1617,55 @@
 _negemo = 0%0A
+    res_affect = np.array(%5B%5D)%0A%0A
     for elem
@@ -1813,109 +1813,102 @@
  if 
-count_affect %3E 0 or count_posemo %3E 0 or count_negemo %3E 0:%0A                if speaker == character:%0A  
+speaker == character:%0A                res_affect = np.append(res_affect, float(count_affect))%0A
     
@@ -1919,18 +1919,17 @@
         
-  
+#
 print '%7B
@@ -1961,36 +1961,33 @@
                 
-   
- 
+#
                 
@@ -2019,35 +2019,33 @@
                 
-   
+#
                 
@@ -2047,33 +2047,32 @@
                 
- 
 count_posemo,%0A  
@@ -2077,36 +2077,33 @@
                 
-    
+#
                 
@@ -2127,24 +2127,108 @@
 negemo)%0A    
+        else: %0A                res_affect = np.append(res_affect, 0.0)%0A            %0A
             
@@ -2240,28 +2240,24 @@
 _affect = 0%0A
-    
             
@@ -2265,36 +2265,32 @@
 ount_posemo = 0%0A
-    
             coun
@@ -2294,32 +2294,45 @@
 ount_negemo = 0%0A
+            %0A
             sent
@@ -2653,16 +2653,510 @@
 unt_negemo += 1%0A
+%0A    #print 'aantal zinnen:', sent, len(soup.find_all(sentence))%0A    #print 'lengte res_affect array:', len(res_affect)%0A    #print res_affect%0A%0A    s = 1%0A    window = 5 %0A    res_affect_smooth = moving_average(res_affect, window)%0A    for i in range(len(res_affect)):%0A        a = res_affect%5Bi%5D%0A        if i %3E window/2 and i %3C len(res_affect_smooth):%0A            a_sm = res_affect_smooth%5Bi-window/2%5D%0A        else:%0A            a_sm = 0.0%0A%0A        print '%7B%7D,%7B%7D,%7B%7D'.format(s, a, a_sm)%0A        s += 1%0A%0A

Add functionality for smoothing of raw liwc frequencies