Textual analysis

HTML

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.js"></script>
<textarea id="data" cols="80" rows="20" placeholder="Paste text or JSON here"></textarea><br />
<label for="text"><input type="radio" name="format" checked value="text" id="text"> Simple text</label>
<button type="button" onclick="parseData()">Analyze text</button>
<br><br>
<div id="output"></div><br><br>
<div id="wpm"></div>
<table>
    <thead>
    </thead>
    <tbody>
    </tbody>
</table>

CSS

body {
    font-family: arial;
}
table, tr, td, th {
    border-collapse: collapse;
    border: solid 1px #ddd;
}
th, td {
    padding: 4px 8px;    
}
.subText {
    color:#999;
    font-style: italic;
}
#wpm {
    width:600px;
    height: 400px;
}

JavaScript

function parseData() {
	var rawData = document.getElementById('data').value.trim(),  
        result,
        output = $('#output'),
        table = $('table').remove(),
        header,
        row,
        cell,
        ul,
        slice,
        wpm = [],        
        wpmAvg = [];
    
    output.empty();
    table.find('thead, tbody').empty();
       
    if ($('[name="format"]:checked').val() === 'text') {
        // Simple text        
        result = analyzeText(rawData);
        output.append('Word count: ' + result.count + '<br><br>Frequent words:<br>');
        ul = $('<ul>');
        _.forEach(result.frequentWords, function(value, key) {
            ul.append('<li>' + value.word + ': ' + value.count + '</li>');
        });
        output.append(ul);       
    }
    else {
        // JSON
        try {
        data = JSON.parse(rawData);
        }
        catch(e) {
            console.log('Error parsing JSON', e);
        }
        header = table.find('thead');
        body = table.find('tbody');
        row = $('<tr>');
        body.append(row);
        // Loop over slices
        _.forEach(data, function(value, key) {
            slice = '';
            // Loop over statements
        	_.forEach(value, function(value, key) {
                slice += value.words + ' ';
            });
            
            result = analyzeText(slice);
            
			addCell(slice, key);
            
        });
        $.plot('#wpm', [wpm], {
            xaxes: [{
            	axisLabel: 'Time index (1-minute increments)',
        	}],
            yaxes: [{
                position: 'left',
                axisLabel: 'Words per minute',
            }]
        });
        output.append(table);
    }
    
    function addCell(data, index) {
        var cell1, cell2, ul1, ul2, result;
        cell1 = $('<td>');
        cell2 = $('<td>');
        ul1 = $('<ul>');
        ul2 = $('<ul>');
        cell1.append(ul1);
        cell2.append(ul2);
       ...