Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Machine learning based text classification in JavaScript using n-grams and cosine similarity

License

NotificationsYou must be signed in to change notification settings

andreekeberg/ml-classify-text-js

Repository files navigation

Use machine learning to classify text usingn-grams andcosine similarity.

Minimal library that can be used both in thebrowser and inNode.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.

Installation

Using npm

npm install ml-classify-text

Using yarn

yarn add ml-classify-text

Getting started

Import as an ES6 module

importClassifierfrom'ml-classify-text'

Import as a CommonJS module

const{ Classifier}=require('ml-classify-text')

Basic usage

Setting up a new Classifier instance

constclassifier=newClassifier()

Training a model

letpositive=['This is great, so cool!','Wow, I love it!','It really is amazing',]letnegative=['This is really bad','I hate it with a passion','Just terrible!',]classifier.train(positive,'positive')classifier.train(negative,'negative')

Getting a prediction

letpredictions=classifier.predict('It sure is pretty great!')if(predictions.length){predictions.forEach(prediction=>{console.log(`${prediction.label} (${prediction.confidence})`)})}else{console.log('No predictions returned')}

Returning:

positive (0.5423261445466404)

Advanced usage

Configuration

The following configuration options can be passed both directly to a newModel, or indirectly by passing it to theClassifier constructor.

Options

PropertyDescriptionDefault
nGramMinMinimum n-gram size1
nGramMaxMaximum n-gram size1
minimumConfidenceMinimum confidence required for predictions0.2
vocabularyTerms mapped to indexes in the model data entries, set tofalse to store terms directly in the data entries[]
dataObject literal containing all training data{}

Using n-grams

The default behavior is to split up texts by single words (known as abag of words, or unigrams).

This has a few limitations, since by ignoring the order of words, it's impossible to correctly match phrases and expressions.

In comesn-grams, 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.

Example of using n-grams with a size of 2 (bigrams)

constclassifier=newClassifier({nGramMin:2,nGramMax:2})lettokens=tokenize('I really dont like it')console.log(tokens)

Returning:

{'i really':1,'really dont':1,'dont like':1,'like it':1}

Serializing a model

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.

To do this, simply use theserialize method on yourModel, and either save the data structure to a file, send it to a server, or store it in any other way you want.

letmodel=classifier.modelconsole.log(model.serialize())

Returning:

{    nGramMin: 1,    nGramMax: 1,    minimumConfidence: 0.2,    vocabulary: [    'this',    'is',      'great',    'so',      'cool',    'wow',    'i',       'love',    'it',    'really',  'amazing', 'bad',    'hate',    'with',    'a',    'passion', 'just',    'terrible'    ],    data: {        positive: {            '0': 1, '1': 2, '2': 1,            '3': 1, '4': 1, '5': 1,            '6': 1, '7': 1, '8': 2,            '9': 1, '10': 1        },        negative: {            '0': 1, '1': 1, '6': 1,            '8': 1, '9': 1, '11': 1,            '12': 1, '13': 1, '14': 1,            '15': 1, '16': 1, '17': 1        }    }}

Documentation

Contributing

Read thecontribution guidelines.

Changelog

Refer to thechangelog for a full history of the project.

License

JavaScript Text Classifier is licensed under theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp