geneea-nlp-client
Version:
The TypeScript Client for Geneea Interpretor G3 API.
153 lines (152 loc) • 5.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Analysis = void 0;
const common_1 = require("../../common/common");
const paragraph_1 = require("./paragraph");
/** An object encapsulating the results of NLP analysis. */
class Analysis {
/**
* Creates an Analysis object.
* @param docId Document id.
* @param language Language of the document and analysis.
* @param paragraphs The paragraphs within the document. For F2, these are segments.
* @param docSentiment Sentiment of the document.
* @param entities The entities in the document.
* @param tags The tags of the document.
* @param relations The relations in the document.
* @param docVectors Optional vectors for the whole document.
* @param usedChars Characters billed for the analysis.
* @param metadata The extra non-NLP type of information related to analysis.
* @param debugInfo Debugging information, if any.
*/
constructor(docId = null, language, paragraphs, docSentiment = null, entities, tags, relations, docVectors = null, usedChars = null, metadata = null, debugInfo = null) {
this.docId = docId;
this.language = language;
this.paragraphs = paragraphs;
this.docSentiment = docSentiment;
this.entities = entities;
this.tags = tags;
this.relations = relations;
this.docVectors = docVectors;
this.usedChars = usedChars;
this.metadata = metadata;
this.debugInfo = debugInfo;
}
get id() {
var _a;
return (_a = this.docId) !== null && _a !== void 0 ? _a : "";
}
get sentiment() {
return this.docSentiment;
}
get vectors() {
return this.docVectors;
}
/** Sentences across all paragraphs. */
*sentences() {
for (const p of this.paragraphs) {
for (const s of p.sentences)
yield s;
}
}
/** Tokens across all paragraphs. */
*tokens() {
for (const p of this.paragraphs) {
for (const t of p.tokens())
yield t;
}
}
/** Tecto tokens across all paragraphs. */
*tectoTokens() {
for (const p of this.paragraphs) {
for (const tt of p.tectoTokens())
yield tt;
}
}
/** Returns the top [n] tags sorted by `relevance` in descending order. */
topTags(n = -1) {
const topTags = this.tags.sort((a, b) => b.relevance - a.relevance);
return n === -1 ? topTags : topTags.slice(0, n);
}
/**
* Returns a paragraph with the specified type.
* Throws IllegalArgumentException if there are more than one such paragraphs, and return null if there are none.
* This is intended for legacy paragraphs corresponding to title/lead/text segments.
*
* For standard types, use these predefined constants:
* [Paragraph.TYPE_TITLE], [Paragraph.TYPE_ABSTRACT], [Paragraph.TYPE_BODY], [Paragraph.TYPE_SECTION_HEADING],
* or dedicated methods: [title], [lead], [body].
*/
getParaByType(paraType) {
const paras = this.paragraphs.filter((p) => p.type === paraType);
if (paras.length > 1) {
throw new Error(`Multiple paragraphs with the type ${paraType}`);
}
return paras.length === 1 ? paras[0] : null;
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple title paragraphs.
*/
titlePara() {
return this.getParaByType(paragraph_1.Paragraph.TYPE_TITLE);
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple subject paragraphs.
*/
subjectPara() {
return this.titlePara();
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple abstract paragraphs.
*/
abstractPara() {
return this.getParaByType(paragraph_1.Paragraph.TYPE_ABSTRACT);
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple lead paragraphs.
*/
leadPara() {
return this.abstractPara();
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple perex paragraphs.
*/
perexPara() {
return this.abstractPara();
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple body paragraphs.
*/
bodyPara() {
return this.getParaByType(paragraph_1.Paragraph.TYPE_BODY);
}
/**
* Returns the title paragraph if present, null if not, and throws
* an error if there are multiple text paragraphs.
*/
textPara() {
return this.bodyPara();
}
toString() {
return (0, common_1.objToStr)(this, [
"docId",
"language",
"paragraphs",
"docSentiment",
"entities",
"tags",
"relations",
"docVectors",
"usedChars",
"metadata",
"debugInfo",
]);
}
}
exports.Analysis = Analysis;