Skip to content

Commit

Permalink
Merge pull request #81 from amos-ws16/feature-12_chat-text-comparison
Browse files Browse the repository at this point in the history
Feature 12 chat text comparison
  • Loading branch information
simonschwan committed Feb 1, 2017
2 parents 5d8e348 + 9f84b4d commit aa4b25a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/plugins/chat-text-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const extractObject = require('../extract-object.js')
const similarityPlugin = require('./similar-text-plugin.js')
const meanAggregator = require('../aggregators/mean.js')
const utils = require('../utils.js')

/**
* Takes as an input an object of the form:
*
* @param chat - an object that is a chat
* @param text - an object that is a text
* @return score - between 1.0 and 0.0. This is the mean value of all the
* similarities between every single chat message and text.
*/
function chatComparisonPlugin (chat, text) {
let [messageText, e1] = extractObject(chat, 'chat[].text')
if ((e1 !== null) && (e1.constructor === Error)) {
throw new Error('Error extracting text from chat.')
}

utils.arrayNotEmpty(messageText)
utils.isValidString(text)
var singleMessageScores = []
for (let message of messageText) {
if ((message !== undefined) && (typeof (message) === 'string')) {
singleMessageScores.push(similarityPlugin(message, text))
}
}

return meanAggregator.create('*').eval(singleMessageScores)
}

module.exports = chatComparisonPlugin
18 changes: 17 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,20 @@ function ensureValidAggregators (aggregators) {
})
}

module.exports = { cloneObject, toDebugString, isValidString, isArray, isInRange, isInteger, isTimestamp, loadPlugin, loadModule, insertByPath, ensureValidAggregators }
/**
* Throw an error if array is empty.
* @param array - the array to be checked
*/
function arrayNotEmpty (array) {
let count = 0
for (let a of array) {
if (a === undefined) {
count++
}
}
if (count === array.length) {
throw new Error('Array empty.')
}
}

module.exports = { cloneObject, toDebugString, isValidString, isArray, isInRange, isInteger, isTimestamp, loadPlugin, loadModule, insertByPath, ensureValidAggregators, arrayNotEmpty }
113 changes: 113 additions & 0 deletions test/plugins/chat-text-plugin-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const buster = require('buster')
const plugin = require('../../lib/plugins/chat-text-plugin.js')

const testChat = {
chat: [
{
'type': 'message',
'text': 'Hello world'
},
{
'type': 'message',
'text': 'Hello underworld'
}
]
}

const testChatSimple = {
chat: [
{
'type': 'message',
'text': 'Hello world'
}
]
}

const brokenChat = {
chat: [
{
'type': 'message',
'notext': 1234
},
{
'type': 'message',
'notext': 'Hello underworld'
}
]
}

const halfChat = {
chat: [
{
'type': 'message',
'notext': 1234
},
{
'type': 'message',
'text': 'Hello'
}
]
}

const wrongeTypeChat = {
chat: [
{
'type': 'message',
'notext': 1234
},
{
'type': 'message',
'text': 'Hello'
}
]
}

const extractObjectBroken = {
chat: 'chat value'
}

buster.testCase('Chat Scorer', {
'should throw error if chat': function () {
buster.assert.exception(() => plugin('', 'testphrase'))
},
'should throw error if no text to compare to': function () {
buster.assert.exception(() => plugin(testChat, ''))
},
'should throw error if no text in chat messages': function () {
buster.assert.exception(() => plugin(brokenChat, 'testphrase'))
},
'should throw error if chat messages cannot be extracted': function () {
buster.assert.exception(() => plugin(extractObjectBroken, 'Hello'))
},
'should ignore chat messages without text if others do have text': function () {
let result = plugin(halfChat, 'Hello')
buster.assert.equals(result, 1.0)
},
'should ignore chat messages without text': function () {
let result = plugin(wrongeTypeChat, 'Hello')
buster.assert.equals(result, 1.0)
},
'should throw error if second arg is not a valid string': function () {
buster.assert.exception(() => plugin(testChat, 1))
},
'should score 1 if chatmessage matches text': function () {
let compareText = testChatSimple.chat[0].text
let result = plugin(testChatSimple, compareText)
buster.assert.equals(result, 1.0)
},
'should score high if high similarity between messages and text': function () {
let compareText = 'Hello world'
let result = plugin(testChat, compareText)
buster.assert.near(result, 1.0, 0.3)
},
'should score medium if medium similarity between messages and text': function () {
let compareText = 'Hello'
let result = plugin(testChat, compareText)
buster.assert.near(result, 0.5, 0.3)
},
'should score low if low similarity between messages and text': function () {
let compareText = 'water'
let result = plugin(testChat, compareText)
buster.assert.near(result, 0.0, 0.2)
}
})

0 comments on commit aa4b25a

Please sign in to comment.