Merge pull request #34648 from ybentz/natural-classifiers-classification

[natural] updated classifiers classification definition
This commit is contained in:
Benjamin Lichtman
2019-04-15 10:15:10 -07:00
committed by GitHub
2 changed files with 11 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
// Type definitions for Natural 0.2.2
// Type definitions for Natural 0.6
// Project: https://github.com/NaturalNode/natural
// Definitions by: Dylan R. E. Moonfire <https://github.com/dmoonfire>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -67,6 +67,7 @@ declare var LancasterStemmer: {
stem(token: string): string;
}
interface BayesClassifierClassification { label: string, value: number }
interface BayesClassifierCallback { (err: any, classifier: any): void }
declare class BayesClassifier {
events: events.EventEmitter;
@@ -74,12 +75,13 @@ declare class BayesClassifier {
addDocument(text: string[], stem: string): void;
train(): void;
classify(observation: string): string;
getClassifications(observation: string): string[];
getClassifications(observation: string): BayesClassifierClassification[];
save(filename: string, callback: BayesClassifierCallback): void;
static load(filename: string, stemmer: Stemmer, callback: BayesClassifierCallback): void;
static restore(classifier: any, stemmer?: Stemmer): BayesClassifier;
}
interface LogisticRegressionClassifierClassification { label: string, value: number }
interface LogisticRegressionClassifierCallback { (err: any, classifier: any): void }
declare class LogisticRegressionClassifier {
events: events.EventEmitter;
@@ -87,7 +89,7 @@ declare class LogisticRegressionClassifier {
addDocument(text: string[], stem: string): void;
train(): void;
classify(observation: string): string;
getClassifications(observation: string): string[];
getClassifications(observation: string): LogisticRegressionClassifierClassification[];
save(filename: string, callback: LogisticRegressionClassifierCallback): void;
static load(filename: string, stemmer: Stemmer, callback: LogisticRegressionClassifierCallback): void;
static restore(classifier: any, stemmer?: Stemmer): LogisticRegressionClassifier;

View File

@@ -1,9 +1,3 @@
// Type definitions for Natural 0.2.1
// Project: https://github.com/NaturalNode/natural
// Definitions by: Dylan R. E. Moonfire <https://github.com/dmoonfire/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import natural = require('natural');
// Tokenizers
@@ -59,7 +53,12 @@ classifier.addDocument('sell gold', 'sell');
classifier.train();
console.log(classifier.classify('i am short silver'));
console.log(classifier.classify('i am long copper'));
console.log(classifier.getClassifications('i am long copper'));
var classifications = classifier.getClassifications('i am long copper');
classifications.forEach(function(classification) {
var label = classification.label
var value = classification.value
console.log('label: ' + label + ', value: ' + value)
})
classifier.addDocument(['sell', 'gold'], 'sell');
classifier.events.on('trainedWithDocument', function (obj: any) {
console.log(obj);