Skip to content

Commit

Permalink
Preliminary addition of the 'natural' library in sandbox. General fun…
Browse files Browse the repository at this point in the history
…ctional test, unit test and other things pending
  • Loading branch information
shamasis committed Nov 20, 2024
1 parent 8d5424d commit 90f2797
Show file tree
Hide file tree
Showing 4 changed files with 1,391 additions and 7 deletions.
36 changes: 35 additions & 1 deletion lib/environment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
/**
* @fileOverview
* This is a sandbox environment configuration definition file. Basically, this holds the configuration
* of what codebases and libraries needs to be loaded by the bundler (located in lib/bundle and is a form of
* specialised code packer derived by using browserify) as they get initialised in bootcode.js
*/
module.exports = {
bundler: {
noParse: ['jquery']
},

/**
* These are definitions of what libraries will be available for being "require"-ed by users
* within the sandbox. To understand the structure of defining this, have a look at the definition
* of the {@see Bundle} Class
*
* @type {Object<Bundle>}
*/
require: {
// builtins required by the libraries exposed for sandbox users
events: { preferBuiltin: true, glob: true },
Expand Down Expand Up @@ -33,7 +47,27 @@ module.exports = {
uuid: { resolve: '../vendor/uuid', expose: 'uuid', glob: true },
chai: { glob: true },
moment: { resolve: 'moment/min/moment.min', expose: 'moment', glob: true },
lodash: { glob: true }
lodash: { glob: true },

// use reduced version of natural module
natural: { resolve: '../vendor/natural', expose: 'natural', glob: true },
// also expose sub modules of `natural`. only expose the modules that safely works
// @note if you are modifying items to the `natural-*` list, then please sync the same in vendor/natural.js
'natural-distance': { resolve: 'natural/lib/natural/distance', expose: 'natural/distance', glob: true },
'natural-inflectors': { resolve: 'natural/lib/natural/inflectors', expose: 'natural/inflectors', glob: true },
'natural-ngrams': { resolve: 'natural/lib/natural/ngrams', expose: 'natural/ngrams', glob: true },
'natural-normalizers': { resolve: 'natural/lib/natural/normalizers', expose: 'natural/normalizers',
glob: true },
'natural-phonetics': { resolve: 'natural/lib/natural/phonetics', expose: 'natural/phonetics', glob: true },
'natural-analyzers': { resolve: 'natural/lib/natural/analyzers', expose: 'natural/analyzers', glob: true },
'natural-sentiment': { resolve: 'natural/lib/natural/sentiment', expose: 'natural/sentiment', glob: true },
'natural-stemmers': { resolve: 'natural/lib/natural/stemmers', expose: 'natural/stemmers', glob: true },
'natural-tfidf': { resolve: 'natural/lib/natural/tfidf', expose: 'natural/tfidf', glob: true },
'natural-tokenizers': { resolve: 'natural/lib/natural/tokenizers', expose: 'natural/tokenizers', glob: true },
'natural-transliterators': { resolve: 'natural/lib/natural/transliterators', expose: 'natural/transliterators',
glob: true },
'natural-trie': { resolve: 'natural/lib/natural/trie', expose: 'natural/trie', glob: true },
'natural-wordnet': { resolve: 'natural/lib/natural/wordnet', expose: 'natural/wordnet', glob: true }
},
ignore: ['aws4', 'hawk', 'node-oauth1'],
files: {
Expand Down
46 changes: 46 additions & 0 deletions lib/vendor/natural.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* `natural` is an npm module that we provide to our users. However, the module does not do
* well as a direct `require` within Postman Sandbox. So, we firstly provide partial imports as
* defined within `environment.js`. However, for the popular usage structure, we are reproducing the
* rough structure of the natural base module and excluding the erroneous bits from it.
*/

/**
* This is a helper function to build the module exportables. This is adapted from the natural module itself
* @param {Array} modules
* @returns {Object}
*
* @todo somebody replace this with array reduce style code.
*/
function buildExportMap (modules) {
const result = {};

modules.forEach(module => {
Object.keys(module).forEach(key => {
result[key] = module[key];
});
})

return result;
};

// adaptation from https://github.com/NaturalNode/natural/blob/master/lib/natural/index.js
module.exports = buildExportMap([
// require('natural/lib/natural/brill_pos_tagger'), // @todo no idea why this was excluded. there were reasons!
// require('natural/lib/natural/classifiers'), // @todo has issues loading web-worker threads
require('natural/lib/natural/distance'),
require('natural/lib/natural/inflectors'),
require('natural/lib/natural/ngrams'),
require('natural/lib/natural/normalizers'),
require('natural/lib/natural/phonetics'),
require('natural/lib/natural/analyzers'),
require('natural/lib/natural/sentiment'),
// require('natural/lib/natural/spellcheck'), // @todo no idea why this was excluded. there were reasons!
require('natural/lib/natural/stemmers'),
require('natural/lib/natural/tfidf'),
require('natural/lib/natural/tokenizers'),
require('natural/lib/natural/transliterators'),
require('natural/lib/natural/trie'),
// require('natural/lib/natural/util'), // @todo has entire db drivers in it! can't have that in sandbox
require('natural/lib/natural/wordnet')
]);
Loading

0 comments on commit 90f2797

Please sign in to comment.