alexisfacques / node-fpgrowth

FPGrowth Algorithm implementation in TypeScript / JavaScript.
https://www.npmjs.com/package/node-fpgrowth
MIT License
16 stars 0 forks source link

Make this module runnable in the browser #2

Closed noVaSon closed 6 years ago

noVaSon commented 6 years ago

Hi @alexisfacques!

This is very good work! I would like to use it in the frontend and there is an error of process.hrtime is not a function, because process is undefined.

I patched it by hand with the help of browser-process-hrtime, but I need to make this simple patch permanent. What about including a runtime environment check in your code? I could renounce the execution time anyway, but I failed to edit your Typescript code in my fork until here.

Regards! noVaSon

alexisfacques commented 6 years ago

Hi @noVaSon ! Thanks for your support, you made me look back at this project and found a tiny bug occurring in the tree's generation, which is nice already!

Back to your issue, I believe we can work a solution out. I needed a execution time assessment feature at the time, as my final goal was to compare this Typescript implementation with other pattern mining algorithms; but I believe it's not of great use to anyone else other than that; especially considering it's the only thing preventing this module from being run in a browser environment out of the box.

I like the idea of using browser-process-hrtime, but see no problem removing the time measurement feature too, keeping the project's dependencies low. Whatever you feel more appropriate.

If you have any piece of code, even in JS, feel free to contribute !

noVaSon commented 6 years ago

Hi @alexisfacques!

It would be totally adequate to remove the timing output. I just couldn't quickly figure out how to get rid of it using TypeScript...

I just did it this way in my project:

In the ES5 compiled ./node-modules/node-fpgrowth/dist/fpgrowth.js

var process = {}; process.hrtime = require('browser-process-hrtime');

That worked for me.

Alternatively if you want to keep the feature: https://github.com/kumavis/browser-process-hrtime/blob/master/index.js

His solution is short and straight forward, if you port it to TypeScript, maybe add a check for node or process.hrtime() first, it would still work.

Thanks for your quick reply! I am wondering why this repo is so lonely here :)

Best!

alexisfacques commented 6 years ago

I just removed the time measurement feature on the following branch: 002-make-this-module-runnable-in-browser.

FpGrowth.exec() now solely returns the array of itemsets once completed.

fpgrowth.exec(transactions) 
    .then( (itemsets: Itemset<number>[]) => { 
      // Returns an array representing the frequent itemsets. 
    }); 

Feel free to try and import it in your project :smile: Will merge and publish this new version to npm once sure it runs properly on a browser environment. Maybe include a proper example on how to import the module on browser too.

noVaSon commented 6 years ago

Unfortunately the branch includes only package.json, readme.md and examples when I try to install with npm install <repo-url>#<branch-name> like it is advised here. So i tryed npm install https://github.com/alexisfacques/Node-FPGrowth.git#002-make-this-module-runnable-in-browser --save.

Need to try it another way later... But thanks for your quick reply, helps a lot!

alexisfacques commented 6 years ago

@noVaSon Woops, I guess including the .npmignore to the repo was not the way to go.

Anyway, everything should be all set right now:

Hope everything works for you. Best !

noVaSon commented 6 years ago

Yes, it works fine! Thanks for your support! Best, Eric