-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from amos-ws16/feature-12_chat-text-comparison
Feature 12 chat text comparison
- Loading branch information
Showing
3 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) |