{"id":13602343,"url":"https://github.com/andreekeberg/ml-classify-text-js","last_synced_at":"2025-08-30T02:10:04.996Z","repository":{"id":45761391,"uuid":"290597701","full_name":"andreekeberg/ml-classify-text-js","owner":"andreekeberg","description":"Machine learning based text classification in JavaScript using n-grams and cosine similarity","archived":false,"fork":false,"pushed_at":"2024-04-21T06:15:27.000Z","size":90,"stargazers_count":129,"open_issues_count":8,"forks_count":11,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-09T07:16:18.516Z","etag":null,"topics":["artificial-intelligence","classification","classifier","cosine-similarity","labels","library","machine-learning","n-gram","n-grams","natural-language-processing","predictions","sentiment-analysis","similarity","text-classification","text-classifier","training"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ml-classify-text","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreekeberg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-26T20:27:29.000Z","updated_at":"2025-03-25T20:41:38.000Z","dependencies_parsed_at":"2024-05-16T21:07:08.651Z","dependency_job_id":"e4e7a310-ba86-490c-91d7-0c3acb338730","html_url":"https://github.com/andreekeberg/ml-classify-text-js","commit_stats":{"total_commits":32,"total_committers":1,"mean_commits":32.0,"dds":0.0,"last_synced_commit":"7efa51b37d3d15357a8adf22fd9fff958cdfa4a8"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreekeberg%2Fml-classify-text-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreekeberg%2Fml-classify-text-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreekeberg%2Fml-classify-text-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreekeberg%2Fml-classify-text-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreekeberg","download_url":"https://codeload.github.com/andreekeberg/ml-classify-text-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248361600,"owners_count":21090939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","classification","classifier","cosine-similarity","labels","library","machine-learning","n-gram","n-grams","natural-language-processing","predictions","sentiment-analysis","similarity","text-classification","text-classifier","training"],"created_at":"2024-08-01T18:01:20.585Z","updated_at":"2025-04-11T08:32:06.281Z","avatar_url":"https://github.com/andreekeberg.png","language":"JavaScript","funding_links":[],"categories":["artificial-intelligence"],"sub_categories":[],"readme":"# 📄 ClassifyText (JS)\n\n[![Version](https://img.shields.io/npm/v/ml-classify-text)](https://www.npmjs.com/package/ml-classify-text) [![Total Downloads](https://img.shields.io/npm/dt/ml-classify-text)](https://www.npmjs.com/package/ml-classify-text) [![License](https://img.shields.io/npm/l/ml-classify-text)](https://www.npmjs.com/package/ml-classify-text)\n\nUse machine learning to classify text using [n-grams](https://en.wikipedia.org/wiki/N-gram) and [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity).\n\nMinimal library that can be used both in the **browser** and in **Node.js**, that allows you to train a model with a large amount of text samples (and corresponding labels), and then use this model to quickly predict one or more appropriate labels for new text samples.\n\n## Installation\n\n**Using npm**\n\n```\nnpm install ml-classify-text\n```\n\n**Using yarn**\n\n```\nyarn add ml-classify-text\n```\n\n## Getting started\n\n**Import as an ES6 module**\n\n```javascript\nimport Classifier from 'ml-classify-text'\n```\n\n**Import as a CommonJS module**\n\n```javascript\nconst { Classifier } = require('ml-classify-text')\n```\n\n## Basic usage\n\n### Setting up a new Classifier instance\n\n```javascript\nconst classifier = new Classifier()\n```\n\n### Training a model\n\n```javascript\nconst positive = [\n\t'This is great, so cool!',\n\t'Wow, I love it!',\n\t'It really is amazing'\n]\n\nconst negative = [\n\t'This is really bad',\n\t'I hate it with a passion',\n\t'Just terrible!'\n]\n\nclassifier.train(positive, 'positive')\nclassifier.train(negative, 'negative')\n```\n\n### Getting a prediction\n\n```javascript\nconst predictions = classifier.predict('It sure is pretty great!')\n\nif (predictions.length) {\n\tpredictions.forEach((prediction) =\u003e {\n\t\tconsole.log(`${prediction.label} (${prediction.confidence})`)\n\t})\n} else {\n\tconsole.log('No predictions returned')\n}\n```\n\nReturning:\n\n```\npositive (0.5423261445466404)\n```\n\n## Advanced usage\n\n### Configuration\n\nThe following configuration options can be passed both directly to a new [Model](docs/model.md), or indirectly by passing it to the [Classifier](docs/classifier.md) constructor.\n\n#### Options\n\n| Property       | Type                        | Default | Description                                                                                           |\n| -------------- | --------------------------- | ------- | ----------------------------------------------------------------------------------------------------- |\n| **nGramMin**   | `int`                       | `1`     | Minimum n-gram size                                                                                   |\n| **nGramMax**   | `int`                       | `1`     | Maximum n-gram size                                                                                   |\n| **vocabulary** | `Array` \\| `Set` \\| `false` | `[]`    | Terms mapped to indexes in the model data, set to `false` to store terms directly in the data entries |\n| **data**       | `Object`                    | `{}`    | Key-value store of labels and training data vectors                                                   |\n\n### Using n-grams\n\nThe default behavior is to split up texts by single words (known as a [bag of words](https://en.wikipedia.org/wiki/Bag-of-words_model), or unigrams).\n\nThis has a few limitations, since by ignoring the order of words, it's impossible to correctly match phrases and expressions.\n\nIn comes [n-grams](https://en.wikipedia.org/wiki/N-gram), which, when set to use more than one word per term, act like a sliding window that moves across the text — a continuous sequence of words of the specified amount, which can greatly improve the accuracy of predictions.\n\n#### Example of using n-grams with a size of 2 (bigrams)\n\n```javascript\nconst classifier = new Classifier({\n\tnGramMin: 2,\n\tnGramMax: 2\n})\n\nconst tokens = classifier.tokenize('I really dont like it')\n\nconsole.log(tokens)\n```\n\nReturning:\n\n```javascript\n{\n    'i really': 1,\n    'really dont': 1,\n    'dont like': 1,\n    'like it': 1\n}\n```\n\n### Serializing a model\n\nAfter training a model with large sets of data, you'll want to store all this data, to allow you to simply set up a new model using this training data at another time, and quickly make predictions.\n\nTo do this, simply use the `serialize` method on your [Model](docs/model.md), and either save the data structure to a file, send it to a server, or store it in any other way you want.\n\n```javascript\nconst model = classifier.model\n\nconsole.log(model.serialize())\n```\n\nReturning:\n\n```\n{\n    nGramMin: 1,\n    nGramMax: 1,\n    vocabulary: [\n    \t'this',    'is',      'great',\n    \t'so',      'cool',    'wow',\n    \t'i',       'love',    'it',\n    \t'really',  'amazing', 'bad',\n    \t'hate',    'with',    'a',\n    \t'passion', 'just',    'terrible'\n    ],\n    data: {\n        positive: {\n            '0': 1, '1': 2, '2': 1,\n            '3': 1, '4': 1, '5': 1,\n            '6': 1, '7': 1, '8': 2,\n            '9': 1, '10': 1\n        },\n        negative: {\n            '0': 1, '1': 1, '6': 1,\n            '8': 1, '9': 1, '11': 1,\n            '12': 1, '13': 1, '14': 1,\n            '15': 1, '16': 1, '17': 1\n        }\n    }\n}\n```\n\n## Documentation\n\n-   [Classifier](docs/Classifier.md)\n-   [Model](docs/Model.md)\n-   [Vocabulary](docs/Vocabulary.md)\n-   [Prediction](docs/Prediction.md)\n\n## Contributing\n\nRead the [contribution guidelines](CONTRIBUTING.md).\n\n## Changelog\n\nRefer to the [changelog](CHANGELOG.md) for a full history of the project.\n\n## License\n\nClassifyText is licensed under the [MIT license](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreekeberg%2Fml-classify-text-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreekeberg%2Fml-classify-text-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreekeberg%2Fml-classify-text-js/lists"}