UNPKG

okesa

Version:

Okesa: LLM-powered Natural Language Processing 💬

201 lines (200 loc) • 10.2 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const index_1 = require("../index"); const ai_1 = require("../ai"); (0, vitest_1.describe)("Okesa NLP Engine", () => { (0, vitest_1.beforeEach)(() => __awaiter(void 0, void 0, void 0, function* () { vitest_1.vi.clearAllMocks(); const generateTextSpy = vitest_1.vi.spyOn(ai_1.AI, "generateText"); const generateObjectSpy = vitest_1.vi.spyOn(ai_1.AI, "generateObject"); const mockGenerateText = () => __awaiter(void 0, void 0, void 0, function* () { return { text: "Hello, world!" }; }); const mockGenerateObject = (config) => __awaiter(void 0, void 0, void 0, function* () { switch (config.schemaDescription) { case "POS tagging Schema": return { object: { posTagging: [ { word: "I", pos: "PRP" }, { word: "love", pos: "VBP" }, { word: "coding", pos: "VBG" }, { word: ".", pos: "." }, ], }, }; case "Tokenization Results Schema": return { object: { words: "I love coding", sentences: "I love coding.", phrases: "I love coding", whitespace: "I love coding", symbols: "I love coding.", }, }; case "NER Schema": return { object: [ { entity: "Barack Obama", label: "PERSON", start: 0, end: 12 }, { entity: "44th", label: "ORDINAL", start: 21, end: 25 }, { entity: "President", label: "TITLE", start: 26, end: 35 }, { entity: "United States", label: "LOCATION", start: 43, end: 56, }, ], }; case "Sentiment Analysis Schema": return { object: { sentiment: "positive", confidence: 0.95, }, }; case "Language Detection Schema": return { object: { language: "English", confidence: 0.99, }, }; case "Text Classification Schema": return { object: { class: "Positive", confidence: 0.98, }, }; case "Text Summarization Schema": return { object: { summary: "Coding is enjoyable and fulfilling.", }, }; case "Keyword Extraction Schema": return { object: ["coding", "love"], }; case "Spell Checking Schema": return { object: { corrected_text: "I love coding.", }, }; case "Stemming and Lemmatization Schema": return { object: ["love", "coding"], }; case "Dependency Parsing Schema": return { object: [ { word: "I", dependency: "nsubj", head: 2 }, { word: "love", dependency: "ROOT", head: 0 }, { word: "coding", dependency: "dobj", head: 2 }, ], }; default: return { object: {} }; } }); generateTextSpy.mockImplementation(mockGenerateText); generateObjectSpy.mockImplementation(mockGenerateObject); const success = yield (0, index_1.init)(true); (0, vitest_1.expect)(success).toBe(true); })); (0, vitest_1.afterEach)(() => { vitest_1.vi.resetAllMocks(); }); (0, vitest_1.it)("should perform POS tagging", () => __awaiter(void 0, void 0, void 0, function* () { const generateObjectSpy = vitest_1.vi.spyOn(ai_1.AI, "generateObject"); const mockGenerateObject = () => __awaiter(void 0, void 0, void 0, function* () { return { object: { posTagging: [ { word: "I", pos: "PRP" }, { word: "love", pos: "VBP" }, { word: "coding", pos: "VBG" }, { word: ".", pos: "." }, ], }, }; }); generateObjectSpy.mockImplementation(mockGenerateObject); const result = yield (0, index_1.posTagging)({ text: "I love coding." }); (0, vitest_1.expect)(result).toBeInstanceOf(Array); (0, vitest_1.expect)(result[0]).toHaveProperty("word"); (0, vitest_1.expect)(result[0]).toHaveProperty("pos"); })); (0, vitest_1.it)("should perform tokenization", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.tokenize)({ text: "I love coding." }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("words"); (0, vitest_1.expect)(result).toHaveProperty("sentences"); })); (0, vitest_1.it)("should perform NER", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.ner)({ text: "Barack Obama was the 44th President of the United States.", }); (0, vitest_1.expect)(result).toBeInstanceOf(Array); (0, vitest_1.expect)(result[0]).toHaveProperty("entity"); (0, vitest_1.expect)(result[0]).toHaveProperty("label"); })); (0, vitest_1.it)("should perform sentiment analysis", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.sentimentAnalysis)({ text: "I love coding." }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("sentiment"); (0, vitest_1.expect)(result).toHaveProperty("confidence"); })); (0, vitest_1.it)("should detect language", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.languageDetection)({ text: "I love coding." }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("language"); (0, vitest_1.expect)(result).toHaveProperty("confidence"); })); (0, vitest_1.it)("should classify text", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.textClassification)({ text: "I love coding." }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("class"); (0, vitest_1.expect)(result).toHaveProperty("confidence"); })); (0, vitest_1.it)("should summarize text", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.textSummarization)({ text: "I love coding. It is very enjoyable and fulfilling.", }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("summary"); })); (0, vitest_1.it)("should extract keywords", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.keywordExtraction)({ text: "I love coding." }); (0, vitest_1.expect)(result).toBeInstanceOf(Array); })); (0, vitest_1.it)("should perform spell checking", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.spellChecking)({ text: "I lov coding." }); (0, vitest_1.expect)(result).not.toBeNull(); (0, vitest_1.expect)(result).toHaveProperty("corrected_text"); })); (0, vitest_1.it)("should perform stemming and lemmatization", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.stemmingLemmatization)({ text: "I love coding." }); (0, vitest_1.expect)(result).toBeInstanceOf(Array); })); (0, vitest_1.it)("should perform dependency parsing", () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, index_1.dependencyParsing)({ text: "I love coding." }); (0, vitest_1.expect)(result).toBeInstanceOf(Array); (0, vitest_1.expect)(result === null || result === void 0 ? void 0 : result[0]).toHaveProperty("word"); (0, vitest_1.expect)(result === null || result === void 0 ? void 0 : result[0]).toHaveProperty("dependency"); (0, vitest_1.expect)(result === null || result === void 0 ? void 0 : result[0]).toHaveProperty("head"); })); });