UNPKG

classifier.js

Version:

:robot: Natural language processing with Javascript

124 lines (123 loc) 7.03 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const classifier_1 = require("../src/classifier"); const fs_1 = __importDefault(require("fs")); describe('Classifier', () => { let classifier; const fromFileOutput = { animal: 0, food: 0.6666666666666666, unknown: 0.3333333333333333, }; beforeAll(() => { classifier = new classifier_1.Classifier(); classifier.learn('I love cats', ['animal']); classifier.learn('Brazilian eat rice and beans', ['food']); }); afterAll(() => __awaiter(void 0, void 0, void 0, function* () { if (fs_1.default.existsSync('test.json')) yield fs_1.default.promises.rm('test.json'); if (fs_1.default.existsSync('test.yaml')) yield fs_1.default.promises.rm('test.yaml'); if (fs_1.default.existsSync('nested-dir')) yield fs_1.default.promises.rm('nested-dir', { recursive: true }); })); describe('file system saving methods', () => { it('should be able to save data as json in the current directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.toJSON('test.json'); expect(fs_1.default.existsSync('test.json')).toBe(true); })); it('should be able to save data as json in a nested directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.toJSON('nested-dir/test.json'); expect(fs_1.default.existsSync('nested-dir/test.json')).toBe(true); })); it('should be able to save data as yaml in the current directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.toYAML('test.yaml'); expect(fs_1.default.existsSync('test.yaml')).toBe(true); })); it('should be able to save data as yaml in a nested directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.toYAML('nested-dir/test.yaml'); expect(fs_1.default.existsSync('nested-dir/test.yaml')).toBe(true); })); }); describe('file system reading methods', () => { it('load data from a json file in the current directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.fromJSON('test.json'); expect(classifier.classify("They don't eat rice")).toEqual(fromFileOutput); })); it('load data from a json file in a nested directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.fromJSON('nested-dir/test.json'); expect(classifier.classify("They don't eat rice")).toEqual(fromFileOutput); })); it('load data from an yaml file in the current directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.fromYAML('test.yaml'); expect(classifier.classify("They don't eat rice")).toEqual(fromFileOutput); })); it('load data from an yaml file in a nested directory', () => __awaiter(void 0, void 0, void 0, function* () { yield classifier.fromYAML('nested-dir/test.yaml'); expect(classifier.classify("They don't eat rice")).toEqual(fromFileOutput); })); }); describe('classifying method', () => { it('should be able to classify sentences with only one category', () => { classifier.resetKnowledge(); classifier.learn('I like cats', ['animal']); classifier.learn('Cats are cool', ['animal']); classifier.learn('Dogs are noisy', ['animal']); classifier.learn('I love animals', ['animal']); classifier.learn('I like my horse', ['animal']); classifier.learn('Chocolate is good', ['food']); classifier.learn('I eat apple', ['food']); classifier.learn('Juice is very good', ['food']); classifier.learn('Brazilians eat rice and beans', ['food']); classifier.learn('Bananas are good for health', ['food']); expect(classifier.classify('Apple juice is awesome')).toEqual(expect.objectContaining({ unknown: 0.2, animal: 0, food: 0.8 })); }); it('should be able to classify sentences with more than one category', () => { classifier.resetKnowledge(); classifier.learn('I like cats', ['animal']); classifier.learn('Cats are cool', ['animal']); classifier.learn('Dogs are noisy', ['animal']); classifier.learn('I love animals', ['animal']); classifier.learn('I like my horse', ['animal', 'herbivorous']); classifier.learn('Chocolate is good', ['food']); classifier.learn('I eat apple', ['food', 'fruit']); classifier.learn('Juice is very good', ['food']); classifier.learn('Brazilians eat rice and beans', ['food']); classifier.learn('Bananas are good for health', ['food']); expect(classifier.classify('Apple juice is awesome')).toEqual(expect.objectContaining({ unknown: 0.12727272727272726, animal: 0, herbivorous: 0, food: 0.509090909090909, fruit: 0.36363636363636365, })); }); it('should be able to accept string as category to classify sentences', () => { classifier.resetKnowledge(); classifier.learn('I like cats', 'animal'); classifier.learn('Cats are cool', 'animal'); classifier.learn('Dogs are noisy', 'animal'); classifier.learn('I love animals', 'animal'); classifier.learn('I like my horse', 'animal'); classifier.learn('Chocolate is good', 'food'); classifier.learn('I eat apple', 'food'); classifier.learn('Juice is very good', 'food'); classifier.learn('Brazilians eat rice and beans', 'food'); classifier.learn('Bananas are good for health', 'food'); expect(classifier.classify('Apple juice is awesome')).toEqual(expect.objectContaining({ unknown: 0.2, animal: 0, food: 0.8 })); }); }); });