-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
213 lines (185 loc) · 5.78 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//#region Info
/**
* @file <h3>DropSuit</h3>
* <p>
* DropSuit is a JavaScript(ES6) and Node.js(v14.x) module library
* created by Lado Oniani that offers a diverse collection of functions
* for natural language processing (NLP) and data manipulation.
* It provides a curated collection of original and classic techniques and methods
* for tasks such as text analysis, language understanding and generation,
* as well as for data manipulation tasks. Additionally,
* it includes unique tools and features for chatbot dialog flow logic
* and other specific use cases.
* The library is open-source and available under the Apache License 2.0.
* Whether you're a researcher, developer, or data scientist,
* DropSuit offers a range of tools to enhance your work,
* with a focus on diversity and experimentation.
* </p>
* @author Lado Oniani
* {@link https://github.com/ladooniani GitHub}
* @see mailto:[email protected]
* @version 1.0.0
* @see https://github.com/ladooniani/DropSuit#readme
* @copyright 2016-2023 Lado Oniani - DropSuit. All Rights Reserved.
* @license Apache License 2.0
*/
//#endregion
//#region Constructor
function Constructor(inputObj, dsout) {
this.inputObj = inputObj;
this.dsout = dsout;
}
//#endregion
//#region tok
/**
* @constructor
* @classdesc Tokenization class.
* @param {array} input_data - Optional input sentence or word to be processed.
* If not provided, the constructor's input will be used.
* @param {number} filter - Return option parameter arguments (0) keep duplicate (1) remove duplicate.
* @returns {object} - The tokenized output, containing the properties:
* tokArr - An array of the individual tokens,
* tokStr - A concatenated string of the tokens, and
* tokNmb - The number of tokens.
*/
Constructor.prototype.tok = function (input_data, filter) {
let input = objOrFncInp(input_data, this.inputObj);
let out = tok_f(input, filter, this.dsout);
return out;
};
//#endregion
//#region tok_f
const dropsuit_clnstr = require("../dropsuit-clnstr/index.js");
var ds_clnstr = new dropsuit_clnstr(null, false);
const dropsuit_clnarr = require("../dropsuit-clnarr/index.js");
var ds_clnarr = new dropsuit_clnarr(false);
/**
* Tokenization function.
* @function
* @param {array} inputRequest - The input requests to be tokenized.
* @param {number} filter - Return option parameter arguments (0) keep duplicate (1) remove duplicate.
* @param {boolean} [dispout=false] - Flag to display processing output results in terminal.
* @returns {object} - The tokenized output, containing the properties:
* tokArr - An array of the individual tokens,
* tokStr - A concatenated string of the tokens, and
* tokNmb - The number of tokens.
* @example
* tok_f([ "This is a sample sentence", "This is another sample sentence" ], true)
*/
function tok_f(inputRequest, filter, dispout) {
filter = checkType(filter, 2);
let aos = arrStrChecker(inputRequest);
if (aos == "string") {
inputRequest = inputRequest.split(" ");
}
inputRequest = ds_clnarr.clnarr(inputRequest, filter);
let allWordsArr = [];
for (l = 0; l < inputRequest.length; l++) {
let input_sentence_sep = inputRequest[l];
let sentence = ds_clnstr.clnstr(input_sentence_sep).txt();
let sentenceWords = sentence.split(" ");
for (p = 0; p < sentenceWords.length; p++) {
let word = sentenceWords[p];
if (filter == 1) {
if (!allWordsArr.includes(word)) {
allWordsArr.push(word);
}
} else if (filter == 0) {
allWordsArr.push(word);
}
}
}
allWordsArr = ds_clnarr.clnarr(allWordsArr, filter);
let allWordStr = allWordsArr.join(" ");
let retout = return_tokOut(allWordsArr, allWordStr);
displayC0(dispout, inputRequest, retout); /// DISPLAY >>
return retout;
}
function arrStrChecker(inputdtwords) {
let isArray = arrChecker(inputdtwords);
var isString = stringChecker(inputdtwords);
if (isString == true) {
return "string";
} else if (isArray == true) {
return "array";
}
}
function arrChecker(array) {
const result = Array.isArray(array);
if (result) {
return true;
} else {
return false;
}
}
function stringChecker(string) {
if (typeof string === "string") {
return true;
} else {
return false;
}
}
function return_tokOut(allWordsArr, allWordStr) {
const tokObj = {
tkNmb: allWordsArr.length,
tkArr: allWordsArr,
tkStr: allWordStr,
tokNmb: function () {
return this.tkNmb;
},
tokArr: function () {
return this.tkArr;
},
tokStr: function () {
return this.tkStr;
},
};
return tokObj;
}
function objOrFncInp(function_input, constructor_input) {
if (function_input !== "" && function_input !== null) {
function_input = function_input;
} else {
function_input = constructor_input;
}
return function_input;
}
function checkType(type, range) {
if (range == 1) {
if (type == 0 || type == 1) {
return type;
} else {
return (type = 0);
}
}
if (range == 2) {
if (type == 0 || type == 1 || type == 2) {
return type;
} else {
return (type = 0);
}
}
}
//#endregion
//#region console log
const getdt = require("./infodt.js");
let fnctit = getdt.displayInfoData();
const line = fnctit.line;
var description = fnctit.descript;
function displayC0(dispout, inputRequest, retout) {
if (dispout == true) {
console.log(
description,
"\nInput:\n\n",
inputRequest,
"\n\nObject:\n\n",
retout,
"\n",
line
);
}
}
//#endregion
//#region Export Module Constructor
module.exports = Constructor;
//#endregion