Skip to content

Commit

Permalink
filter tokens
Browse files Browse the repository at this point in the history
remove ["",""] from the tokens array, added from tinyMCE html junk.
  • Loading branch information
thedannywahl committed Feb 15, 2019
1 parent 2cd93e8 commit bc8e7ed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
27 changes: 20 additions & 7 deletions src/langs/sentimony.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pre id="sentimony-report-details">${JSON.stringify(data, null, 2)}</pre>`
},
containerOverview: function(data) {
let sentiment = "neutral"
let emotion = "neutral"
if (data.comparative > 0) {
Expand All @@ -31,11 +34,11 @@ Langs.prototype.strings = {
sentiment because none of your words seem to have a feeling or
emotion associated wiith them`
}
return `<p><span>${strength.html}</span></p>
return `<p id="report-emoji"><span class="emoji" role="img" aria-label="">${strength.html}</span></p>
<p>Overall, readers might see your sentiment as
<span class="keyword">${strength.scale}</span> ${sentiment}
because you used words like <span class="keyword">${data[sentiment][0]}</span>
${data[sentiment][1] ? `and <span class="keyword">${data[sentiment][1]}</span>` : ''}
<span class="report-keyword">${strength.scale}</span> ${sentiment}
because you used words like <span class="report-keyword">${data[sentiment][0]}</span>
${data[sentiment][1] ? `and <span class="report-keyword">${data[sentiment][1]}</span>` : ''}
</p>`
}
}
Expand All @@ -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;
12 changes: 5 additions & 7 deletions src/modules/sentimony.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,25 @@ 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
}
}

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
Expand Down
25 changes: 23 additions & 2 deletions src/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 6 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
]
},
{
Expand All @@ -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")
}
}
]
},)
Expand All @@ -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)
})
Expand Down

0 comments on commit bc8e7ed

Please sign in to comment.