'classifierType is not a constructor' - javascript

I'm using the limdu library found here (https://www.npmjs.com/package/limdu#binary-classification). I'm trying to combine the 'Multi-Label Classification' section with the 'Input Normalization' section to make the outputs all be lowercase, but I keep getting the following error:
/Users/nickbrinsmade/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43
this.classifier = new opts.classifierType();
^
TypeError: opts.classifierType is not a constructor
at new EnhancedClassifier (/Users/myusr/Desktop/limdutest/node_modules/limdu/classifiers/EnhancedClassifier.js:43:20)
at Object.<anonymous> (/Users/myusr/Desktop/limdutest/index.js:9:20)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
My code Is seen below:
var limdu = require('limdu');
var MyWinnow = limdu.classifiers.Winnow.bind(0, {retrain_count: 10});
var textClassifier = new limdu.classifiers.multilabel.BinaryRelevance({
binaryClassifierType: MyWinnow
});
intentClassifier = new limdu.classifiers.EnhancedClassifier({
classifierType: textClassifier, // same as in previous example
normalizer: limdu.features.LowerCaseNormalizer
}); //this declaration throws the error.
intentClassifier.trainBatch([
{input: {I:1,want:1,an:1,apple:1}, output: "APPLE"},
{input: {I:1,want:1,a:1,banana:1}, output: "BANANA"},
{input: {I:1,want:1,chips:1}, output: "CHIPS"}
]);
console.log(intentClassifier.classify({I:1,want:1,an:1,apple:1,and:1,a:1,banana:1}));

Related

unable to call a local module from an application file in node.js

I am beginner in node.js.
I am trying to call a very basic addition module saved in C:\wks\guru99 with file name: Node_03_addition.js. The calling application is also saved in the same location with the name: app.js
Addition Module:
var exports = module.exports = {};
exports.addNumber=function(a,b)
{
return a+b;
};
Application File:
var Addition = require('/.Node_03_addition.js');
console.log(Addition.addNumber(1,2));
when I run the application in cmd using node app.js
I am receiving the error that Module is not found. Can someone please help me with where I am going wrong here.
c:\wks\guru99>node app.js
internal/modules/cjs/loader.js:969
throw err;
^
Error: Cannot find module '/.Node_03_addition.js'
Require stack:
- c:\wks\guru99\app.js
[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:842:27)[39m
[90m at Module.require (internal/modules/cjs/loader.js:1026:19)[39m
[90m at require (internal/modules/cjs/helpers.js:72:18)[39m
at Object.<anonymous> (c:\wks\guru99\app.js:1:16)
[90m at Module._compile (internal/modules/cjs/loader.js:1138:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:986:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:879:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: [ [32m'c:\\wks\\guru99\\app.js'[39m ]
}
SUGGESTION
// Node_03_addition.js
// Declare your function
const addNumber = function(a,b) {
return a + b;
};
// Export your function
module.exports = {
addNumber
};
// Application-File.js
// Import your module
const Addition = require('./Node_03_addition');
// Use it
console.log(Addition.addNumber(1,2)); // 3
Addition Module:
var exports = module.exports = {};
exports.addNumber=function(a,b)
{
return a+b;
};
Application File:
var Addition = require('./Node_03_addition.js');
console.log(Addition.addNumber(1,2));

How do I use jquery within the serverless framework?

I would like to make use of the jquery deferred method in my serverless lambda project. However after requiring both jquery & jquery-ui as dependencies, the following error occurs when I attempt to make use of the jquery library? I'm new to serverless and lambda so please see the handler.js function below.
TypeError: $.each is not a function
at /vagrant/project/node_modules/jquery-ui/ui/widget.js:690:3
at widgetUuid (/vagrant/project/node_modules/jquery-ui/ui/widget.js:24:3)
at Object.<anonymous> (/vagrant/project/node_modules/jquery-ui/ui/widget.js:26:2)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/vagrant/project/handler.js:4:19)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:156:33)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
at AwsInvokeLocal.tryCatcher (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/usr/local/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
handler.js
'use strict';
global.jQuery = require('jquery');
global.jQueryUI = require('jquery-ui');
var Model = require('./resources/model');
module.exports.project = function(event, context, callback) {
Model.init();
};
model.js
var JsonFile = require('jsonfile');
var $ = global.jQueryUI;
module.exports = {
init : function() {
var self = this;
self.fetch_file().done(function(file){
console.log(file);
});
},
fetch_file : function(){
var deferred = $.Deferred();
JsonFile.readFile('path to file', function (err, file) {
deferred.resolve(file);
});
return $.when(deferred).promise();
}
};
package.json
{
"private": true,
"dependencies": {
"avro-js": "^1.8.2",
"aws-sdk": "^2.88.0",
"jquery": "^3.2.1",
"jquery-ui": "^1.12.1",
"jsonfile": "^3.0.1"
},
"name": "project",
"version": "0.1.0"
}
After requiring Jquery instead of JqueryUI I get this exception:
TypeError: $.Deferred is not a function
at Object.fetch_schema (/vagrant/project/resources/model.js:17:26)
at Object.init (/vagrant/project/resources/model.js:11:14)
at module.exports.pixel_event_lambda_producer (/vagrant/project/handler.js:9:10)
at AwsInvokeLocal.invokeLocalNodeJs (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:229:12)
at AwsInvokeLocal.invokeLocal (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:114:19)
From previous event:
at Object.invoke:local:invoke [as hook] (/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/index.js:24:10)
at BbPromise.reduce (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:55)
From previous event:
at PluginManager.invoke (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:218:22)
at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/PluginManager.js:237:17)
at variables.populateService.then (/usr/local/lib/node_modules/serverless/lib/Serverless.js:99:33)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
at Serverless.run (/usr/local/lib/node_modules/serverless/lib/Serverless.js:86:74)
at serverless.init.then (/usr/local/lib/node_modules/serverless/bin/serverless:39:50)
at <anonymous>
Issue resolved when I specifically required the jquery-deferred module which can be found here: https://www.npmjs.com/package/jquery-deferred
You have confused jQuery with jQuery UI. In your model.js, replace:
var $ = global.jQueryUI;
With:
var $ = global.jQuery;
Deferred is a jQuery method, and not a jQuery UI feature.

NFC Error in nodejs

I was about to make a program that reads NFC cards via NFC reader.
when I execute my program i get the following errros...
/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:83
throw e
^
Error: libnfc.so.5: cannot open shared object file: No such file or directory
at Object.Module._extensions..node (module.js:598:18)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (/home/dotmark/Desktop/nfc2/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/home/dotmark/Desktop/nfc2/node_modules/nfc/index.js:1:95)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
My script for loading nfc libraries is as follows...
var nfc = require('nfc').nfc
, util = require('util')
;
console.log('nfc.version(): ' + util.inspect(nfc.version(), { depth: null }));
// { name: 'libfnc', version: '1.7.0' }
console.log('nfc.scan(): ' + util.inspect(nfc.scan(), { depth: null }));
Any help would be really great..

Plugins for acorn loose_parser?

This code snippet seems not work, why?
var acorn = require("./node_modules/acorn/dist/acorn_loose");
/* Extend default Acorn's methods.*/
acorn.pluginsLoose.testPlug = function(looseParser) {
looseParser.extend('finishNode', function(nextMethod) {
return function(node, type) {
console.log(node, type);
return nextMethod.call(this, node, type);
};
});
};
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
console.log(res)
Give us next error:
...\node_modules\acorn\dist\acorn.js:508
if (!plugin) throw new Error("Plugin '" + name + "' not found")
^
Error: Plugin 'testPlug' not found
at Parser.loadPlugins (...\node_modules\acorn\dist\acorn.js:508:26)
at new Parser (...\node_modules\acorn\dist\acorn.js:444:10)
at Object.tokenizer (...\node_modules\acorn\dist\acorn.js:3115:12)
at new LooseParser (...\node_modules\acorn\dist\acorn_loose.js:15:23)
at Object.parse_dammit (...\node_modules\acorn\dist\acorn_loose.js:1258:13)
at Object.<anonymous> (...\plugins_poc.js:16:17)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
Is it bug or I do something wrong?
Enviroment: idea intellij 16.
Update:
Chrome give the similar error.
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
You need to use pluginsLoose instead of plugins here as well!

Node mustache rendering server side

I'm trying to create a simple application to compile mustache templates into static pages server side, here's what I've got so far:
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
var mustache = require("mustache");
var template = require("./home.template");
var output = mustache.to_html(template, view);
console.log(output);
And my template looks like:
{{title}} spend {{calc}}
Any suggestions as to what is causing this to fail?
Here is the complete error message:
home.template:1
} spend {{calc}}
^
module.js:437
var compiledWrapper = runInThisContext(wrapper, filename, true);
^
SyntaxError: Unexpected token {
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/Users/MorehouseJ09/Documents/production_development/mustache/current/compiler.js:12:16)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
Any help would be great!
Use fs.readFile() to read your template in as a string. Require won't work unless it's requiring javascript code, not mustache code.
http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback
Edit
See if this works...
var mustache = require("mustache");
var fs = require("fs");
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
fs.readFile('./home.template', 'utf-8', function (err, data) {
if (err) throw err;
var output = mustache.to_html(data, view);
console.log(output);
});

Categories