|
| 1 | +# 📄 JavaScript Text Classifier |
| 2 | + |
| 3 | +Use 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). |
| 4 | + |
| 5 | +Minimal 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. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +**Using npm** |
| 10 | + |
| 11 | +``` |
| 12 | +npm install ml-classify-text |
| 13 | +``` |
| 14 | + |
| 15 | +**Using yarn** |
| 16 | + |
| 17 | +``` |
| 18 | +yarn add ml-classify-text |
| 19 | +``` |
| 20 | + |
| 21 | +## Getting started |
| 22 | + |
| 23 | +**Import as an ES6 module** |
| 24 | + |
| 25 | +```javascript |
| 26 | +import Classifier from 'ml-classify-text' |
| 27 | +``` |
| 28 | + |
| 29 | +**Import as a CommonJS module** |
| 30 | + |
| 31 | +```javascript |
| 32 | +const { Classifier } = require('ml-classify-text') |
| 33 | +``` |
| 34 | + |
| 35 | +## Basic usage |
| 36 | + |
| 37 | +### Setting up a new Classifier instance |
| 38 | + |
| 39 | +```javascript |
| 40 | +const classifier = new Classifier() |
| 41 | +``` |
| 42 | + |
| 43 | +### Training a model |
| 44 | + |
| 45 | +```javascript |
| 46 | +let positive = [ |
| 47 | + 'This is great, so cool!', |
| 48 | + 'Wow, I love it!', |
| 49 | + 'It really is amazing', |
| 50 | +] |
| 51 | + |
| 52 | +let negative = [ |
| 53 | + 'This is really bad', |
| 54 | + 'I hate it with a passion', |
| 55 | + 'Just terrible!', |
| 56 | +] |
| 57 | + |
| 58 | +classifier.train(positive, 'positive') |
| 59 | +classifier.train(negative, 'negative') |
| 60 | +``` |
| 61 | + |
| 62 | +### Getting a prediction |
| 63 | + |
| 64 | +```javascript |
| 65 | +let predictions = classifier.predict('It sure is pretty great!') |
| 66 | + |
| 67 | +if (predictions.length) { |
| 68 | + predictions.forEach(prediction => { |
| 69 | + console.log(`${prediction.label} (${prediction.confidence})`) |
| 70 | + }) |
| 71 | +} else { |
| 72 | + console.log('No predictions returned') |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Returning: |
| 77 | + |
| 78 | +``` |
| 79 | +positive (0.5423261445466404) |
| 80 | +``` |
| 81 | + |
| 82 | +## Advanced usage |
| 83 | + |
| 84 | +### Configuration |
| 85 | + |
| 86 | +The 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. |
| 87 | + |
| 88 | +#### Options |
| 89 | + |
| 90 | +| Property | Description | Default | |
| 91 | +| --- | --- | --- | |
| 92 | +| **nGramMin** | Minimum n-gram size | `1` | |
| 93 | +| **nGramMax** | Maximum n-gram size | `1` | |
| 94 | +| **minimumConfidence** | Minimum confidence required for predictions | `0.2` | |
| 95 | +| **vocabulary** | Terms mapped to indexes in the model data entries, set to `false` to store terms directly in the data entries | `[]` | |
| 96 | +| **data** | Object literal containing all training data | `{}` | |
| 97 | + |
| 98 | +### Using n-grams |
| 99 | + |
| 100 | +The 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). |
| 101 | + |
| 102 | +This has a few limitations, since by ignoring the order of words, it's impossible to correctly match phrases and expressions. |
| 103 | + |
| 104 | +In 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. |
| 105 | + |
| 106 | +#### Example of using n-grams with a size of 2 (bigrams) |
| 107 | + |
| 108 | +```javascript |
| 109 | +const classifier = new Classifier({ |
| 110 | + nGramMin: 2, |
| 111 | + nGramMax: 2 |
| 112 | +}) |
| 113 | + |
| 114 | +let tokens = tokenize('I really dont like it') |
| 115 | + |
| 116 | +console.log(tokens) |
| 117 | +``` |
| 118 | + |
| 119 | +Returning: |
| 120 | + |
| 121 | +```javascript |
| 122 | +{ |
| 123 | + 'i really': 1, |
| 124 | + 'really dont': 1, |
| 125 | + 'dont like': 1, |
| 126 | + 'like it': 1 |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Serializing a model |
| 131 | + |
| 132 | +After 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 quicky make predictions. |
| 133 | + |
| 134 | +To 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. |
| 135 | + |
| 136 | +```javascript |
| 137 | +let model = classifier.model |
| 138 | + |
| 139 | +console.log(model.serialize()) |
| 140 | +``` |
| 141 | + |
| 142 | +Returning: |
| 143 | + |
| 144 | +``` |
| 145 | +{ |
| 146 | + nGramMin: 1, |
| 147 | + nGramMax: 1, |
| 148 | + minimumConfidence: 0.2, |
| 149 | + vocabulary: [ |
| 150 | + 'this', 'is', 'great', |
| 151 | + 'so', 'cool', 'wow', |
| 152 | + 'i', 'love', 'it', |
| 153 | + 'really', 'amazing', 'bad', |
| 154 | + 'hate', 'with', 'a', |
| 155 | + 'passion', 'just', 'terrible' |
| 156 | + ], |
| 157 | + data: { |
| 158 | + positive: { |
| 159 | + '0': 1, '1': 2, '2': 1, |
| 160 | + '3': 1, '4': 1, '5': 1, |
| 161 | + '6': 1, '7': 1, '8': 2, |
| 162 | + '9': 1, '10': 1 |
| 163 | + }, |
| 164 | + negative: { |
| 165 | + '0': 1, '1': 1, '6': 1, |
| 166 | + '8': 1, '9': 1, '11': 1, |
| 167 | + '12': 1, '13': 1, '14': 1, |
| 168 | + '15': 1, '16': 1, '17': 1 |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## Documentation |
| 175 | + |
| 176 | +* [Classifier](docs/classifier.md) |
| 177 | +* [Model](docs/model.md) |
| 178 | +* [Vocabulary](docs/vocabulary.md) |
| 179 | +* [Prediction](docs/prediction.md) |
| 180 | + |
| 181 | +## Contributing |
| 182 | + |
| 183 | +Read the [contribution guidelines](CONTRIBUTING.md). |
| 184 | + |
| 185 | +## Changelog |
| 186 | + |
| 187 | +Refer to the [changelog](CHANGELOG.md) for a full history of the project. |
| 188 | + |
| 189 | +## License |
| 190 | + |
| 191 | +JavaScript Text Classifier is licensed under the [MIT license](LICENSE). |
0 commit comments