2019-03-15 11:01:27 +01:00
2023-08-31 22:25:00 +02:00
module . exports = function ( RED ) {
2019-03-15 11:01:27 +01:00
"use strict" ;
var multilangsentiment = require ( 'multilang-sentiment' ) ;
function MultiLangSentimentNode ( n ) {
2023-08-31 22:25:00 +02:00
RED . nodes . createNode ( this , n ) ;
2019-03-15 11:01:27 +01:00
this . lang = n . lang ;
2023-08-31 22:25:00 +02:00
this . property = n . property || "payload" ;
2019-03-15 11:01:27 +01:00
var node = this ;
2023-08-31 22:25:00 +02:00
this . on ( "input" , function ( msg ) {
var value = RED . util . getMessageProperty ( msg , node . property ) ;
2019-03-15 11:01:27 +01:00
if ( value !== undefined ) {
2023-08-31 22:25:00 +02:00
multilangSentiment ( value , node . lang || msg . lang || 'en' , { 'words' : msg . words || null , 'tokens' : msg . tokens || null } , function ( err , result ) {
2019-03-15 11:01:27 +01:00
msg . sentiment = result ;
2023-08-31 22:25:00 +02:00
msg . sentiment . comparative = msg . sentiment . score / msg . sentiment . tokens . length ; // temporarily addresses an issue in v2.0.0: https://github.com/marcellobarile/multilang-sentiment/issues/10
2019-03-15 11:01:27 +01:00
node . send ( msg ) ;
} ) ;
}
else { node . send ( msg ) ; } // If no matching property - just pass it on.
} ) ;
}
2023-08-31 22:25:00 +02:00
RED . nodes . registerType ( "mlsentiment" , MultiLangSentimentNode ) ;
2019-03-15 11:01:27 +01:00
}