Add types for bayes-classifier (#37337)

* Add types for bayes-classifier

* Address review

* Address review
This commit is contained in:
Jason Harrison 2019-08-07 17:58:45 -04:00 committed by Nathan Shively-Sanders
parent 86f1551d60
commit 537ab33583
4 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,33 @@
import BayesClassifier = require('bayes-classifier');
const classifier = new BayesClassifier();
const positiveDocuments = [
`I love tacos.`,
`Dude, that burrito was epic!`,
`Holy cow, these nachos are so good and tasty.`,
`I am drooling over the awesome bean and cheese quesadillas.`,
];
const negativeDocuments = [
`Gross, worst taco ever.`,
`The buritos gave me horrible diarrhea.`,
`I'm going to puke if I eat another bad nacho.`,
`I'd rather die than eat those nasty enchiladas.`,
];
classifier.addDocuments(positiveDocuments, `positive`);
classifier.addDocuments(negativeDocuments, `negative`);
classifier.train();
classifier.classify(`I heard the mexican restaurant is great!`); // "positive"
classifier.classify(`I don't want to eat there again.`); // "negative"
classifier.classify(`The torta is epicly bad.`); // "negative"
classifier.classify(`The torta is tasty.`); // "positive"
classifier.getClassifications(`Burritos are the meaning of life.`);
/*
[ { label: 'positive', value: 0.22222222222222224 },
{ label: 'negative', value: 0.11111111111111112 } ]
*/

28
types/bayes-classifier/index.d.ts vendored Normal file
View File

@ -0,0 +1,28 @@
// Type definitions for bayes-classifier 0.0
// Project: https://github.com/miguelmota/bayes-classifier
// Definitions by: Jason Harrison <https://github.com/jasonharrison>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.5
interface Classifications {
label: string;
value: number;
}
declare class BayesClassifier {
constructor();
addDocument(doc: string, label: string): void;
addDocuments(docs: string[], label: string): void;
classify(doc: string): string;
getClassifications(doc: string): Classifications;
restore(classifier: any): void;
train(): void;
}
export = BayesClassifier;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"bayes-classifier-tests.ts"
]
}

View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}