|
30 | 30 |
|
31 | 31 | // |
32 | 32 |
|
| 33 | +var sort4FT = require( './sort4FT.js' ); |
33 | 34 | var constants = require( './constants.js' ); |
34 | 35 | var caseMap = [ 'other', 'lowerCase', 'upperCase', 'titleCase' ]; |
35 | 36 |
|
@@ -138,5 +139,51 @@ its.sentiment = function ( spanItem ) { |
138 | 139 | return spanItem[ 3 ]; |
139 | 140 | }; // span() |
140 | 141 |
|
| 142 | +/* ------ utilities ------ */ |
| 143 | + |
| 144 | +its.terms = function ( tf, idf, terms ) { |
| 145 | + return terms; |
| 146 | +}; // terms() |
| 147 | + |
| 148 | +its.docTermMatrix = function ( tf, idf, terms ) { |
| 149 | + const dtm = new Array( tf.length ); |
| 150 | + for ( let id = 0; id < tf.length; id += 1 ) { |
| 151 | + dtm[ id ] = []; |
| 152 | + for ( let i = 0; i < terms.length; i += 1 ) { |
| 153 | + dtm[ id ].push( tf[ id ][ terms[ i ] ] || 0 ); |
| 154 | + } |
| 155 | + } |
| 156 | + return dtm; |
| 157 | +}; // getDocTermMatrix() |
| 158 | + |
| 159 | +its.docBOWArray = function ( tf ) { |
| 160 | + return tf; |
| 161 | +}; // docBOWArray() |
| 162 | + |
| 163 | +its.bow = function ( tf ) { |
| 164 | + return tf; |
| 165 | +}; // bow() |
| 166 | + |
| 167 | +its.idf = function ( tf, idf ) { |
| 168 | + var arr = []; |
| 169 | + for ( const t in idf ) { // eslint-disable-line guard-for-in |
| 170 | + arr.push( [ t, idf[ t ] ] ); |
| 171 | + } |
| 172 | + // Sort on frequency followed by the term. |
| 173 | + return arr.sort( sort4FT ); |
| 174 | +}; // idf() |
| 175 | + |
| 176 | +its.tf = function ( tf ) { |
| 177 | + const arr = []; |
| 178 | + for ( const t in tf ) { // eslint-disable-line guard-for-in |
| 179 | + arr.push( [ t, tf[ t ] ] ); |
| 180 | + } |
| 181 | + // Sort on frequency followed by the term. |
| 182 | + return arr.sort( sort4FT ); |
| 183 | +}; // tf() |
| 184 | + |
| 185 | +its.modelJSON = function ( tf, idf ) { |
| 186 | + return JSON.stringify( { tf: tf, idf: idf } ); |
| 187 | +}; // model() |
141 | 188 |
|
142 | 189 | module.exports = its; |
0 commit comments