From bc8e7ed0f71bce47669d0f2ff4f3a84d18d45205 Mon Sep 17 00:00:00 2001 From: Danny Wahl Date: Fri, 15 Feb 2019 16:12:09 -0700 Subject: [PATCH] filter tokens remove ["",""] from the tokens array, added from tinyMCE html junk. --- src/langs/sentimony.js | 27 ++++++++++++++++++++------- src/modules/sentimony.js | 12 +++++------- src/plugin.css | 25 +++++++++++++++++++++++-- src/plugin.js | 7 ++++++- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/langs/sentimony.js b/src/langs/sentimony.js index 68c463a..3b3a8db 100644 --- a/src/langs/sentimony.js +++ b/src/langs/sentimony.js @@ -12,8 +12,11 @@ Langs.prototype.strings = { sentimentPegative: "negative", labelSelectCategory: function() {return "Select a category"}, labelOverview: function() {return "Sentiment Overview"}, - labelScore: function() {return "Sentiment Details"}, - containerReport: function(data) { + labelDetail: function() {return "Sentiment Details"}, + containerDetail: function(data) { + return `
${JSON.stringify(data, null, 2)}
` + }, + containerOverview: function(data) { let sentiment = "neutral" let emotion = "neutral" if (data.comparative > 0) { @@ -31,11 +34,11 @@ Langs.prototype.strings = { sentiment because none of your words seem to have a feeling or emotion associated wiith them` } - return `

${strength.html}

+ return `

${strength.html}

Overall, readers might see your sentiment as - ${strength.scale} ${sentiment} - because you used words like ${data[sentiment][0]} - ${data[sentiment][1] ? `and ${data[sentiment][1]}` : ''} + ${strength.scale} ${sentiment} + because you used words like ${data[sentiment][0]} + ${data[sentiment][1] ? `and ${data[sentiment][1]}` : ''}

` } } @@ -48,7 +51,17 @@ Langs.prototype.t = function(l = 'en', s, d) { } Langs.prototype.comparativeToScale = function(data, emotion) { - return emotions[emotion]["slightly"] + if(data.comparative > 0) { + emotion = "joy" + } + if(data.comparative < 0) { + emotion = "sadness" + } + let scale = data.score / data.words.length + if (scale < 0) scale = scale * -1 + scale = Math.ceil(scale) + scale = Object.keys(emotions[emotion])[scale] + return emotions[emotion][scale] } module.exports = Langs; diff --git a/src/modules/sentimony.js b/src/modules/sentimony.js index 572b678..33316a3 100644 --- a/src/modules/sentimony.js +++ b/src/modules/sentimony.js @@ -16,11 +16,11 @@ Sentimony.prototype.setComparativeSentiment = function(data, emotions) { emotion.setAttribute("aria-label", emotions.sadness.slightly.name) emotion.innerHTML = emotions.sadness.slightly.html emotion.hidden = false - } else if ((data.comparative == 0) && (data.tokens.length > 2)) { + } else if ((data.comparative == 0) && (data.tokens.length > 0)) { emotion.setAttribute("aria-label", emotions.neutral.slightly.name) emotion.innerHTML = emotions.neutral.slightly.html emotion.hidden = false - } else { // data.comparative == 0 && data.tokens.length <=2 ["",""] + } else { emotion.setAttribute("aria-label", "") emotion.innerHTML = '' emotion.hidden = true @@ -28,15 +28,13 @@ Sentimony.prototype.setComparativeSentiment = function(data, emotions) { } Sentimony.prototype.showReport = function(section, data) { - - let overview = s.t(l, "containerReport", data) - let score = `Score: ${data.score}` - + let overview = s.t(l, "containerOverview", data) + let detail = s.t(l, "containerDetail", data) let reportBody = document.getElementById("sentimony-report-body") let report = overview if (reportBody) { - if (section == s.t(l, "labelScore")) report = score + if (section == s.t(l, "labelDetail")) report = detail reportBody.innerHTML = report } else { return report diff --git a/src/plugin.css b/src/plugin.css index 04ccca3..82a1549 100755 --- a/src/plugin.css +++ b/src/plugin.css @@ -8,10 +8,31 @@ text-decoration: none; } +#sentimony-report-body { + height: 170px; + overflow-y: scroll; +} #sentimony-report-body * { white-space: normal; } -.keyword { - font-style: italic; +#sentimony-report-details { + white-space: pre; + font-family: monospace; + border: 1px solid #ccc; + background: #fafafa; + color: #000; + padding: 5px; +} + +#report-emoji { + text-align: center; +} +#report-emoji .emoji { + font-size: xx-large; +} + +.report-keyword { /* override `.mce-contaner *` */ + color: #000!important; + border-bottom: 1px dashed!important; } diff --git a/src/plugin.js b/src/plugin.js index b51b27c..1dac17e 100755 --- a/src/plugin.js +++ b/src/plugin.js @@ -37,7 +37,7 @@ const plugin = editor => { }, values : [ {text: s.t(l, "labelOverview"), value: s.t(l, "labelOverview"), selected: true}, - {text: s.t(l, "labelScore"), value: s.t(l, "labelScore")} + {text: s.t(l, "labelDetail"), value: s.t(l, "labelDetail")} ] }, { @@ -47,7 +47,11 @@ const plugin = editor => { { type: 'container', id: 'sentimony-report', + role: 'region', html: sentimony.showReport(s.t(l, "labelOverview"), globalData), + onPostRender: function(){ + this.getEl().setAttribute("aria-live", "polite") + } } ] },) @@ -58,6 +62,7 @@ const plugin = editor => { editor.on('Change', function(e) { sentiment.analyze(editor.getContent({format:'text'}), {}, function(i,data) { globalData = data + globalData.tokens = globalData.tokens.filter(n => n) sentimony.setComparativeSentiment(globalData, emotions) console.log(globalData) })