Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional analytics2 #150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions browser/js/analytics/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
<div class="state-container left-padding">
<h2 class="sp">Analytics</h2>
<div class="select-sp">
<select ng-model="selectedScreenplay" ng-init="selectedScreenplay='Select a Script'" ng-change="changeSP(selectedScreenplay)">
<option>Select a Script</option>
<option ng-repeat="script in scripts | orderBy: 'name'" value="{{script._id}}">{{script.name}}</option>
</select>
<select ng-change='changeSP()' ng-model="selectedScreenplay" ng-options="script as script.name for script in scripts | orderBy: 'name'"><option value=''></option></select>
</div>
<div class="chart-types">
<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.pieChart({id: currentSP})" type="button">Character Weight</button>
<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.charWeight({id: currentSP})" type="button">Character Weight</button>

<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.donutChart({id: currentSP})" type="button">Character Word Distribution</button>
<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.charWord({id: currentSP})" type="button">Character Word Distribution</button>

<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.lineChart({id: currentSP})" type="button">Emotional Development</button>
<button class="btn sp-btn add-screenplay btn-space" ui-sref="analytics.emotion({id: currentSP})" type="button">Emotional Development</button>
</div>
<div class="analytics-div" ng-show="hidden" ui-view></div>
</div>
Expand Down
43 changes: 30 additions & 13 deletions browser/js/analytics/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ app.config(function ($stateProvider) {
}
}
})
.state('analytics.pieChart', {
url: '/pieChart/:id',
.state('analytics.charWeight', {
url: '/charWeight/:id',
templateUrl: 'js/analytics/pieChart.html',
controller: function($scope, AnalyticsFactory, pieChartData) {
$scope.hidden = true;
console.log(pieChartData);
$scope.options = AnalyticsFactory.pieChartOptions;
console.log("PC options", $scope.options);
$scope.data = pieChartData;
},
resolve: {
Expand All @@ -32,12 +30,11 @@ app.config(function ($stateProvider) {
}
}
})
.state('analytics.donutChart', {
url: 'donutChart/:id',
.state('analytics.charWord', {
url: '/charWord/:id',
templateUrl: 'js/analytics/donutChart.html',
controller: function($scope, AnalyticsFactory, pieChartData) {
$scope.hidden = true;
console.log("piechartdata in donut:", pieChartData);
$scope.selectDChar = function(char){
$scope.data = $scope.dselected;
};
Expand All @@ -53,11 +50,30 @@ app.config(function ($stateProvider) {
}
}
})
.state('analytics.lineChart', {
url: '/lineChart/:id',
templateUrl: 'js/analyticsSingle/lineChart.html',
.state('analytics.emotion', {
url: '/emotion/:id',
templateUrl: 'js/analytics/lineChart.html',
controller: function($scope, lineChartData, AnalyticsFactory) {
$scope.hidden = true;
$scope.hidden = true;
console.log('in emotion controller');
console.log($scope);
$scope.selectOtherMovie = () => {
console.log($scope);
AnalyticsFactory.getSentiment($scope.selectedmovie._id)
.then(sentiment => {
$scope.otherMovieChars = sentiment;
$scope.data.push({
color: "hsl(" + Math.random() * 360 + ",100%,50%)",
key: $scope.selectedmovie.name,
values: sentiment.sceneText});
});
};
$scope.selectOtherMovieChar = () => {
$scope.data.push({
color: "hsl(" + Math.random() * 360 + ",100%,50%)",
key: $scope.selectedOtherMovieChar,
values: $scope.otherMovieChars[$scope.selectedOtherMovieChar]});
};
$scope.selectChar = function(){
$scope.data.push({
color: "hsl(" + Math.random() * 360 + ",100%,50%)",
Expand Down Expand Up @@ -85,7 +101,8 @@ app.config(function ($stateProvider) {
});

app.controller('analytics', function($scope, ScreenplaysFactory, scrapedSPs, AnalyticsFactory){
$scope.changeSP = (spId) => {$scope.currentSP = spId};
$scope.changeSP = (spId) => {
console.log($scope.selectedScreenplay);
$scope.currentSP = $scope.selectedScreenplay._id};
$scope.scripts = scrapedSPs;
$scope.hidden = false;
});
2 changes: 1 addition & 1 deletion browser/js/analytics/analyticsFact.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ app.factory('AnalyticsFactory', ($http) => {
"<td><strong>" + series.key + "</strong></td>" +
"</tr>" +
"<tr>" +
"<td><strong>Scene: "+e.pointIndex+"</strong></td>" +
"<td><strong>Scene: "+e.value+"</strong></td>" +
"<td><strong>Total Words: "+e.point.sentiment.tokens.length+"</strong></td>" +
"</tr>" +
"</thead>";
Expand Down
13 changes: 8 additions & 5 deletions browser/js/analytics/lineChart.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

<section class="state-container">
<div>
<select ng-change='selectChar()' ng-model="selected" ng-options="k as k for (k, v) in chars"><option value=''></option></select>
<nvd3 class="nvd3holder" options="options" data="data"></nvd3>
<div class="chart-container">
<div class='charselector'>
<select ng-change='selectChar()' ng-model="selected" ng-options="k as k for (k, v) in chars"></select>
<select ng-change='selectOtherMovie()' ng-model="selectedmovie" ng-options="script as script.name for script in scripts | orderBy: 'name'"><option value=''></option></select>
<select ng-show='otherMovieChars' ng-change='selectOtherMovieChar()' ng-model="selectedOtherMovieChar" ng-options="k as k for (k, v) in otherMovieChars"><option value=""></option></select>
</div>
<nvd3 options="options" data="data"></nvd3>
</div>
<section>
<section>
1 change: 1 addition & 0 deletions browser/js/analyticsSingle/analyticsSingle.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ app.config(function ($stateProvider) {
controller: function($scope, screenplay, lineChartData, AnalyticsFactory, scrapedSPs) {
$scope.scripts = scrapedSPs;
$scope.selectOtherMovie = () => {
console.log($scope);
AnalyticsFactory.getSentiment($scope.selectedmovie._id)
.then(sentiment => {
$scope.otherMovieChars = sentiment;
Expand Down
2 changes: 1 addition & 1 deletion browser/js/analyticsSingle/donutChart.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="chart-container">
<select ng-change='selectDChar()' ng-model="dselected" ng-options="char.words as char.key for char in chars">
<select ng-change='selectDChar()' ng-model="dselected" ng-options="char.tfidf as char.key for char in chars">
<option value=''></option>
</select>
<nvd3 options="options" data="data"></nvd3>
Expand Down
9 changes: 5 additions & 4 deletions server/app/routes/analytics/analyticsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ module.exports.sceneStringsToEmotion = arrayOfSceneStrings => {
for (var char in scene){
var sentimentRes = sentiment(scene[char].tokenizeAndStem().join(' '));
if (emotion[char]) {
emotion[char].push({x: idx, y: sentimentRes.comparative, sentiment: sentimentRes});
emotion[char].push({x: idx+1, y: sentimentRes.comparative, sentiment: sentimentRes});
} else if (scene[char]) {
emotion[char] = [];
emotion[char].push({x: idx, y: sentimentRes.comparative, sentiment: sentimentRes});
emotion[char].push({x: idx+1, y: sentimentRes.comparative, sentiment: sentimentRes});
}
}
});
Expand All @@ -43,7 +43,7 @@ module.exports.sceneStringsToEmotion = arrayOfSceneStrings => {
module.exports.arrayOfStringsToEmotion = arrayOfStrings => {
return arrayOfStrings.map((ele, idx) => {
var sentimentRes = sentiment(ele.tokenizeAndStem().join(' '));
return {x: idx, y: sentimentRes.comparative, sentiment: sentimentRes};
return {x: idx+1, y: sentimentRes.comparative, sentiment: sentimentRes};
});
};

Expand All @@ -60,7 +60,8 @@ module.exports.objectOfStringsToEmotion = objOfStrings => {

module.exports.processText = (textArr, stopwordsSupp) => {
const TfIdf = new natural.TfIdf();
textArr = textArr.map(text => nlp.tokenizer(text).filter(word => word.length > 2));
console.log(stopwordsSupp.length);
textArr = textArr.map(text => nlp.tokenizer(text).filter(word => word.length > 3));
var tokenstopMap = textArr.map(text => {
return nlp.stopwords(text, {additionalWords: {all: stopwordsSupp}})
.then(processedTxt => {
Expand Down
28 changes: 19 additions & 9 deletions server/app/routes/analytics/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,37 @@ router.get('/:screenplayId/emotion', (req, res, next) => {


router.get('/:screenplayId/wordcount', (req, res , next)=>{
const TfIdf = new natural.TfIdf();
var compObjPerm;
var charNames = [];
req.screenplay.GetDialogue()
.then(dialogue => {
var compObj = {};
dialogue.forEach(comp => {
if (charNames.indexOf(comp.charName.toLowerCase()) === -1){
charNames.push(comp.charName.toLowerCase());
}
if (compObj[comp.charName]) {
compObj[comp.charName] += ' '+comp.text;
} else {
compObj[comp.charName] = comp.text;
}
});
var solution = [];
var textArr = [];
for (var char in compObj) {
TfIdf.addDocument(compObj[char], char);
solution.push({y: compObj[char].split(' ').length, key: char});
textArr.push(compObj[char]);
}
compObjPerm = compObj;
return helper.processText(textArr, charNames); })
.then(tfidf => {
var solution = [];
var idx = 0;
for (var char in compObjPerm) {
var tenTfIdf = tfidf.listTerms(idx).slice(0,10).map(tfidfChar => {
return {key: tfidfChar.term, y: tfidfChar.tfidf};
});
solution.push({y: compObjPerm[char].split(' ').length, key: char, tfidf: tenTfIdf});
idx++;
}
solution = solution.map((ele,idx) => {
var termHolder = TfIdf.listTerms(idx).slice(0,10);
ele.words = termHolder.map(term => ({y: term.tfidf, key: term.term}));
return ele;
});
res.json(solution);
})
.catch(error => console.error(error));
Expand Down
1 change: 0 additions & 1 deletion server/db/models/scrapedScreenplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ spschema.methods.TextbyScenes = function(){
}
}, '');
sceneArray.push(sceneString);

}
return sceneArray;
};
Expand Down
15 changes: 9 additions & 6 deletions server/db/models/screenplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ schema.methods.GetDialogue = function(){
};




schema.methods.TextbyScenes = function(){
schema.methods.TextbyScenes = function(sections){
return this.PopulateComponents()
.then(scenes => {
var sceneSize = Math.ceil(scenes.length / 100);
var hundoScenes = [];
while (scenes.length > 0) {
hundoScenes.push(scenes.splice(0, sceneSize).reduce((orig, scene) => orig.concat(scene.components), []));
}
var sceneObj = {};
sceneObj.sceneText = {};
sceneObj.sceneText['1'] = '';
scenes.forEach((scene,idx) => {
sceneObj.sceneText[1] = '';
hundoScenes.forEach((scene,idx) => {
sceneObj.sceneText[idx] = '';
scene.components.forEach(comp => {
scene.forEach(comp => {
if (!comp.text || !comp.text.trim().length) {return; }
sceneObj.sceneText[idx] += (' '+comp.text);
var key = comp.charName ? comp.charName : comp.type;
Expand Down