-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathas.js
186 lines (170 loc) · 5.64 KB
/
as.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// wink-nlp
//
// Copyright (C) GRAYPE Systems Private Limited
//
// This file is part of “wink-nlp”.
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and
// associated documentation files (the "Software"), to
// deal in the Software without restriction, including
// without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to
// whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
var sort4FT = require( './sort4FT.js' );
var containedMarkings = require( './contained-markings.js' );
var as = Object.create( null );
// ### array
/**
* It is a simple passthru function i.e. input is returned as-is.
*
* @param {string[]} tokens The input tokens.
* @return {string[]} the input `tokens` as-is.
* @private
*/
as.array = function ( tokens ) {
// Return the input tokens as-is.
return tokens;
}; // array()
// ### set
/**
* Constructs set from the `tokens`.
*
* @param {string[]} tokens The input tokens.
* @return {set} the set of `tokens`.
* @private
*/
as.set = function ( tokens ) {
// Create set & return.
return new Set( tokens );
}; // set()
// ### bow
/**
*
* Constructs the bag of words from the `tokens`.
*
* @param {string[]} tokens The input tokens.
* @return {objects} the bag of words object containing `token/frequency`
* `key/value` pairs.
* @private
*/
as.bow = function ( tokens ) {
// Bag of words.
var bow = Object.create( null );
var t;
for ( let i = 0; i < tokens.length; i += 1 ) {
t = tokens[ i ];
bow[ t ] = 1 + ( bow[ t ] || 0 );
}
return bow;
}; // bow()
// ### freqTable
/**
* Constructs the frequency table of `tokens`, which sorted in a descending
* order of token's frequency.
*
* @param {string[]} tokens The input tokens.
* @return {array[]} array of `[ token, frequency ]` pairs.
* @private
*/
as.freqTable = function ( tokens ) {
// NOTE: build FT based on argument type i.e. array or object (its.detail)
var bow = as.bow( tokens );
var keys = Object.keys( bow );
var length = keys.length;
var table = new Array( length );
for ( var i = 0; i < length; i += 1 ) {
table[ i ] = [ keys[ i ], bow[ keys[ i ] ] ];
}
return table.sort( sort4FT );
}; // freqTable()
// ### bigrams
/**
* Generates bigrams of the input tokens.
*
* @param {string[]} tokens The input tokens.
* @return {array[]} array of `[ token`<sub>i</sub>`, token`<sub>i+1</sub> ` ]`
* bigrams.
* @private
*/
as.bigrams = function ( tokens ) {
// Bigrams will be stored here.
var bgs = [];
// Helper variables.
var i, imax;
// Create bigrams.
for ( i = 0, imax = tokens.length - 1; i < imax; i += 1 ) {
bgs.push( [ tokens[ i ], tokens[ i + 1 ] ] );
}
return bgs;
}; // bigrams()
as.unique = function ( tokens ) {
return Array.from( new Set( tokens ) );
}; // unique()
// ### text
/**
*
* Generates the text by joining the contents of `twps` array (tokens with
* preceding spaces).
*
* @param {array} twps Array containing tokens with preceding spaces.
* @return {string} the text.
* @private
*/
as.text = function ( twps ) {
// Join on empty-space as preceding spaces are part of `twps`!
return twps.join( '' ).trim();
}; // text()
// ### markedUpText
/**
*
* Generates the marked up text of the span specified by the `start` and `end` using
* `twps` and `markings`.
*
* @param {array} twps Array containing tokens with preceding spaces.
* @param {array} markings Array containing span of markings & marking specs.
* @param {number} start The start index of the tokens.
* @param {number} end The end index of the tokens.
* @return {string} the markedup text.
* @private
*/
as.markedUpText = function ( twps, markings, start, end ) {
// Offset to be added while computing `first` and `last` indexes of `twps`.
var offset = start * 2;
// Compute the `range` of `markings` to consider on the basis `start` and `end`.
var range = containedMarkings( markings, start, end );
if ( range === null ) {
// Means no valid range, return the text as is.
return twps.join( '' ).trim();
}
// For every marking prefix the `first` one with `beginMarker` and suffix
// the `last` one with `endMarker`.
for ( let i = range.left; i <= range.right; i += 1 ) {
const first = ( ( markings[ i ][ 0 ] * 2 ) - offset ) + 1;
const last = ( ( markings[ i ][ 1 ] * 2 ) - offset ) + 1;
const beginMarker = ( markings[ i ][ 2 ] === undefined ) ? '<mark>' : markings[ i ][ 2 ];
const endMarker = ( markings[ i ][ 3 ] === undefined ) ? '</mark>' : markings[ i ][ 3 ];
twps[ first ] = beginMarker + twps[ first ];
twps[ last ] += endMarker;
}
// Join all the elements and return the `markedUpText`.
return twps.join( '' ).trim();
}; // markedUpText()
module.exports = as;