nlptoolkit-util 1.0.2 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Random.d.ts +8 -0
- package/dist/Random.js +45 -0
- package/dist/Random.js.map +1 -0
- package/dist/RandomArray.d.ts +11 -0
- package/dist/RandomArray.js +49 -0
- package/dist/RandomArray.js.map +1 -0
- package/index.js +2 -1
- package/package.json +1 -1
- package/source/Random.ts +35 -0
- package/source/RandomArray.ts +35 -0
- package/tests/RandomArray.ts +25 -0
- package/dist/RandomNormalizedArray.d.ts +0 -17
- package/dist/RandomNormalizedArray.js +0 -44
- package/dist/RandomNormalizedArray.js.map +0 -1
- package/source/RandomNormalizedArray.ts +0 -33
- package/tests/RandomNormalizedArray.ts +0 -16
package/dist/Random.d.ts
ADDED
package/dist/Random.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Random = void 0;
|
|
13
|
+
var Random = /** @class */ (function () {
|
|
14
|
+
function Random(seed) {
|
|
15
|
+
this.seed = 0;
|
|
16
|
+
if (seed != undefined) {
|
|
17
|
+
this.seed = seed;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
Random.prototype.nextRandom = function () {
|
|
21
|
+
this.seed = (1664525 * this.seed + 1013904223) % Number.MAX_SAFE_INTEGER;
|
|
22
|
+
};
|
|
23
|
+
Random.prototype.nextDouble = function (min, max) {
|
|
24
|
+
this.nextRandom();
|
|
25
|
+
if (min != undefined && max != undefined) {
|
|
26
|
+
return min + (this.seed / Number.MAX_SAFE_INTEGER) * (max - min);
|
|
27
|
+
}
|
|
28
|
+
return this.seed / Number.MAX_SAFE_INTEGER;
|
|
29
|
+
};
|
|
30
|
+
Random.prototype.nextInt = function (maxRange) {
|
|
31
|
+
this.nextRandom();
|
|
32
|
+
return this.seed % maxRange;
|
|
33
|
+
};
|
|
34
|
+
Random.prototype.shuffle = function (array) {
|
|
35
|
+
var _a;
|
|
36
|
+
for (var i = array.length - 1; i > 0; i--) {
|
|
37
|
+
var randomIndex = this.nextInt(i + 1);
|
|
38
|
+
_a = [array[randomIndex], array[i]], array[i] = _a[0], array[randomIndex] = _a[1];
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return Random;
|
|
42
|
+
}());
|
|
43
|
+
exports.Random = Random;
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=Random.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Random.js","sourceRoot":"","sources":["../source/Random.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA;QAII,gBAAY,IAAa;YAFjB,SAAI,GAAW,CAAC,CAAA;YAGpB,IAAI,IAAI,IAAI,SAAS,EAAC;gBAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;aACnB;QACL,CAAC;QAEO,2BAAU,GAAlB;YACI,IAAI,CAAC,IAAI,GAAG,CAAC,OAAO,GAAG,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,GAAG,MAAM,CAAC,gBAAgB,CAAC;QAC7E,CAAC;QAED,2BAAU,GAAV,UAAW,GAAY,EAAE,GAAY;YACjC,IAAI,CAAC,UAAU,EAAE,CAAA;YACjB,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,IAAI,SAAS,EAAC;gBACrC,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAA;aACnE;YACD,OAAO,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,gBAAgB,CAAC;QAC/C,CAAC;QAED,wBAAO,GAAP,UAAQ,QAAgB;YACpB,IAAI,CAAC,UAAU,EAAE,CAAA;YACjB,OAAO,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC;QAChC,CAAC;QAED,wBAAO,GAAP,UAAQ,KAAiB;;YACrB,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAC;gBACtC,IAAI,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;gBACtC,KACI,CAAC,KAAK,CAAC,WAAW,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,EADjC,KAAK,CAAC,CAAC,CAAC,QAAA,EAAE,KAAK,CAAC,WAAW,CAAC,QAAA,CACM;aACtC;QACL,CAAC;QACL,aAAC;IAAD,CAAC,AAlCD,IAkCC;IAlCY,wBAAM"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class RandomArray {
|
|
2
|
+
/**
|
|
3
|
+
* The constructor of {@link RandomArray} class gets an integer itemCount as an input. Creates an array of
|
|
4
|
+
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
5
|
+
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
6
|
+
*
|
|
7
|
+
* @param itemCount integer input defining array size.
|
|
8
|
+
*/
|
|
9
|
+
static normalizedArray(itemCount: number): Array<number>;
|
|
10
|
+
static indexArray(itemCount: number, seed: number): Array<number>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./Random"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RandomArray = void 0;
|
|
13
|
+
var Random_1 = require("./Random");
|
|
14
|
+
var RandomArray = /** @class */ (function () {
|
|
15
|
+
function RandomArray() {
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The constructor of {@link RandomArray} class gets an integer itemCount as an input. Creates an array of
|
|
19
|
+
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
20
|
+
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
21
|
+
*
|
|
22
|
+
* @param itemCount integer input defining array size.
|
|
23
|
+
*/
|
|
24
|
+
RandomArray.normalizedArray = function (itemCount) {
|
|
25
|
+
var sum = 0.0;
|
|
26
|
+
var array = new Array(itemCount);
|
|
27
|
+
for (var i = 0; i < itemCount; i++) {
|
|
28
|
+
array[i] = Math.random();
|
|
29
|
+
sum += array[i];
|
|
30
|
+
}
|
|
31
|
+
for (var i = 0; i < itemCount; i++) {
|
|
32
|
+
array[i] /= sum;
|
|
33
|
+
}
|
|
34
|
+
return array;
|
|
35
|
+
};
|
|
36
|
+
RandomArray.indexArray = function (itemCount, seed) {
|
|
37
|
+
var array = new Array(itemCount);
|
|
38
|
+
for (var i = 0; i < itemCount; i++) {
|
|
39
|
+
array[i] = i;
|
|
40
|
+
}
|
|
41
|
+
var random = new Random_1.Random(seed);
|
|
42
|
+
random.shuffle(array);
|
|
43
|
+
return array;
|
|
44
|
+
};
|
|
45
|
+
return RandomArray;
|
|
46
|
+
}());
|
|
47
|
+
exports.RandomArray = RandomArray;
|
|
48
|
+
});
|
|
49
|
+
//# sourceMappingURL=RandomArray.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RandomArray.js","sourceRoot":"","sources":["../source/RandomArray.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,mCAAgC;IAEhC;QAAA;QAgCA,CAAC;QA9BG;;;;;;WAMG;QACI,2BAAe,GAAtB,UAAuB,SAAiB;YACpC,IAAI,GAAG,GAAG,GAAG,CAAC;YACd,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;YACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE;gBAChC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;gBACzB,GAAG,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC;aACnB;YACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE;gBAChC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;aACnB;YACD,OAAO,KAAK,CAAA;QAChB,CAAC;QAEM,sBAAU,GAAjB,UAAkB,SAAiB,EAAE,IAAY;YAC7C,IAAI,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;YACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAC;gBAC/B,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;aAChB;YACD,IAAI,MAAM,GAAG,IAAI,eAAM,CAAC,IAAI,CAAC,CAAA;YAC7B,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;YACrB,OAAO,KAAK,CAAA;QAChB,CAAC;QAEL,kBAAC;IAAD,CAAC,AAhCD,IAgCC;IAhCY,kCAAW"}
|
package/index.js
CHANGED
package/package.json
CHANGED
package/source/Random.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class Random {
|
|
2
|
+
|
|
3
|
+
private seed: number = 0
|
|
4
|
+
|
|
5
|
+
constructor(seed?: number) {
|
|
6
|
+
if (seed != undefined){
|
|
7
|
+
this.seed = seed
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
private nextRandom(){
|
|
12
|
+
this.seed = (1664525 * this.seed + 1013904223) % Number.MAX_SAFE_INTEGER;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
nextDouble(min?: number, max?: number): number{
|
|
16
|
+
this.nextRandom()
|
|
17
|
+
if (min != undefined && max != undefined){
|
|
18
|
+
return min + (this.seed / Number.MAX_SAFE_INTEGER) * (max - min)
|
|
19
|
+
}
|
|
20
|
+
return this.seed / Number.MAX_SAFE_INTEGER;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
nextInt(maxRange: number): number{
|
|
24
|
+
this.nextRandom()
|
|
25
|
+
return this.seed % maxRange;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
shuffle(array: Array<any>){
|
|
29
|
+
for (let i = array.length - 1; i > 0; i--){
|
|
30
|
+
let randomIndex = this.nextInt(i + 1);
|
|
31
|
+
[array[i], array[randomIndex]] =
|
|
32
|
+
[array[randomIndex], array[i]];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Random} from "./Random";
|
|
2
|
+
|
|
3
|
+
export class RandomArray {
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The constructor of {@link RandomArray} class gets an integer itemCount as an input. Creates an array of
|
|
7
|
+
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
8
|
+
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
9
|
+
*
|
|
10
|
+
* @param itemCount integer input defining array size.
|
|
11
|
+
*/
|
|
12
|
+
static normalizedArray(itemCount: number) : Array<number>{
|
|
13
|
+
let sum = 0.0;
|
|
14
|
+
let array = new Array(itemCount);
|
|
15
|
+
for (let i = 0; i < itemCount; i++) {
|
|
16
|
+
array[i] = Math.random();
|
|
17
|
+
sum += array[i];
|
|
18
|
+
}
|
|
19
|
+
for (let i = 0; i < itemCount; i++) {
|
|
20
|
+
array[i] /= sum;
|
|
21
|
+
}
|
|
22
|
+
return array
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static indexArray(itemCount: number, seed: number): Array<number>{
|
|
26
|
+
let array = new Array(itemCount);
|
|
27
|
+
for (let i = 0; i < itemCount; i++){
|
|
28
|
+
array[i] = i;
|
|
29
|
+
}
|
|
30
|
+
let random = new Random(seed)
|
|
31
|
+
random.shuffle(array)
|
|
32
|
+
return array
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as assert from "assert";
|
|
2
|
+
import {RandomArray} from "../dist/RandomArray";
|
|
3
|
+
|
|
4
|
+
describe('RandomNormalizedArray', function() {
|
|
5
|
+
describe('normalizedArrayTest', function () {
|
|
6
|
+
it('normalizedArrayTest', function () {
|
|
7
|
+
let array = RandomArray.normalizedArray(10);
|
|
8
|
+
let sum = 0.0;
|
|
9
|
+
for (var i = 0; i < 10; i++){
|
|
10
|
+
sum += array[i];
|
|
11
|
+
}
|
|
12
|
+
assert.equal(sum, 1.0);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
describe('indexArrayTest', function () {
|
|
16
|
+
it('indexArrayTest', function () {
|
|
17
|
+
let array = RandomArray.indexArray(10, 0);
|
|
18
|
+
let sum = 0;
|
|
19
|
+
for (var i = 0; i < 10; i++){
|
|
20
|
+
sum += array[i];
|
|
21
|
+
}
|
|
22
|
+
assert.equal(sum, 45);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export declare class RandomNormalizedArray {
|
|
2
|
-
private readonly array;
|
|
3
|
-
/**
|
|
4
|
-
* The constructor of {@link RandomNormalizedArray} class gets an integer itemCount as an input. Creates an array of
|
|
5
|
-
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
6
|
-
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
7
|
-
*
|
|
8
|
-
* @param itemCount integer input defining array size.
|
|
9
|
-
*/
|
|
10
|
-
constructor(itemCount: number);
|
|
11
|
-
/**
|
|
12
|
-
* Getter for the double {@link Array}.
|
|
13
|
-
*
|
|
14
|
-
* @return the double {@link Array}.
|
|
15
|
-
*/
|
|
16
|
-
get(): Array<number>;
|
|
17
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
(function (factory) {
|
|
2
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
-
var v = factory(require, exports);
|
|
4
|
-
if (v !== undefined) module.exports = v;
|
|
5
|
-
}
|
|
6
|
-
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports"], factory);
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.RandomNormalizedArray = void 0;
|
|
13
|
-
var RandomNormalizedArray = /** @class */ (function () {
|
|
14
|
-
/**
|
|
15
|
-
* The constructor of {@link RandomNormalizedArray} class gets an integer itemCount as an input. Creates an array of
|
|
16
|
-
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
17
|
-
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
18
|
-
*
|
|
19
|
-
* @param itemCount integer input defining array size.
|
|
20
|
-
*/
|
|
21
|
-
function RandomNormalizedArray(itemCount) {
|
|
22
|
-
var sum = 0.0;
|
|
23
|
-
this.array = new Array(itemCount);
|
|
24
|
-
for (var i = 0; i < itemCount; i++) {
|
|
25
|
-
this.array[i] = Math.random();
|
|
26
|
-
sum += this.array[i];
|
|
27
|
-
}
|
|
28
|
-
for (var i = 0; i < itemCount; i++) {
|
|
29
|
-
this.array[i] /= sum;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Getter for the double {@link Array}.
|
|
34
|
-
*
|
|
35
|
-
* @return the double {@link Array}.
|
|
36
|
-
*/
|
|
37
|
-
RandomNormalizedArray.prototype.get = function () {
|
|
38
|
-
return this.array;
|
|
39
|
-
};
|
|
40
|
-
return RandomNormalizedArray;
|
|
41
|
-
}());
|
|
42
|
-
exports.RandomNormalizedArray = RandomNormalizedArray;
|
|
43
|
-
});
|
|
44
|
-
//# sourceMappingURL=RandomNormalizedArray.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RandomNormalizedArray.js","sourceRoot":"","sources":["../source/RandomNormalizedArray.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA;QAII;;;;;;WAMG;QACH,+BAAY,SAAiB;YACzB,IAAI,GAAG,GAAG,GAAG,CAAC;YACd,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;YAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE;gBAChC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;gBAC9B,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;aACxB;YACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE;gBAChC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC;aACxB;QACL,CAAC;QAED;;;;WAIG;QACH,mCAAG,GAAH;YACI,OAAO,IAAI,CAAC,KAAK,CAAC;QACtB,CAAC;QAEL,4BAAC;IAAD,CAAC,AAhCD,IAgCC;IAhCY,sDAAqB"}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
export class RandomNormalizedArray {
|
|
2
|
-
|
|
3
|
-
private readonly array: Array<number>;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The constructor of {@link RandomNormalizedArray} class gets an integer itemCount as an input. Creates an array of
|
|
7
|
-
* size itemCount and loops through each element of the array and initializes them with a random number by using Math.random().
|
|
8
|
-
* Then, accumulates each element of the array and at the end divides each element with the resulting sum.
|
|
9
|
-
*
|
|
10
|
-
* @param itemCount integer input defining array size.
|
|
11
|
-
*/
|
|
12
|
-
constructor(itemCount: number) {
|
|
13
|
-
let sum = 0.0;
|
|
14
|
-
this.array = new Array(itemCount);
|
|
15
|
-
for (let i = 0; i < itemCount; i++) {
|
|
16
|
-
this.array[i] = Math.random();
|
|
17
|
-
sum += this.array[i];
|
|
18
|
-
}
|
|
19
|
-
for (let i = 0; i < itemCount; i++) {
|
|
20
|
-
this.array[i] /= sum;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Getter for the double {@link Array}.
|
|
26
|
-
*
|
|
27
|
-
* @return the double {@link Array}.
|
|
28
|
-
*/
|
|
29
|
-
get(): Array<number>{
|
|
30
|
-
return this.array;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import * as assert from "assert";
|
|
2
|
-
import {RandomNormalizedArray} from "../dist/RandomNormalizedArray";
|
|
3
|
-
|
|
4
|
-
describe('RandomNormalizedArray', function() {
|
|
5
|
-
describe('get', function () {
|
|
6
|
-
it('Checks get method of RandomNormalizedArray', function () {
|
|
7
|
-
let randomNormalizedArray = new RandomNormalizedArray(10);
|
|
8
|
-
let array = randomNormalizedArray.get();
|
|
9
|
-
let sum = 0.0;
|
|
10
|
-
for (var i = 0; i < 10; i++){
|
|
11
|
-
sum += array[i];
|
|
12
|
-
}
|
|
13
|
-
assert.equal(sum, 1.0);
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
});
|