geneea-nlp-client
Version:
The TypeScript Client for Geneea Interpretor G3 API.
118 lines (117 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelationSupport = exports.RelationArgument = exports.Relation = void 0;
const common_1 = require("../../common/common");
const token_1 = require("./token");
/** An object encapsulating a relation extracted from text. For example `buy(John, car)`. */
class Relation {
/**
*
* @param id ID of the relation used to refer to it from other objects.
* @param textRepr Human redable representation of the relation, e.g. `eat-not(SUBJ:John, DOBJ:pizza)`.
* @param name Name of the relation, e.g. `eat` for _eat a pizza_ or `good` for _a good pizza_.
* @param type One of Relation.TYPE_ATTR, Relation.TYPE_RELATION, Relation.TYPE_EXTERNAL.
* @param args Arguments of the relation (subject, possibly an object).
* @param support Tecto-tokens of all the mentions of the relations (restricted to its head). Empty if not requested.
* @param feats Any features of the relation e.g. modality: can.
* @param sentiment Sentiment of this relation. null if not requested.
* @param vectors Optional vectors for this relation.
*/
constructor(id, textRepr, name, type, args, support, feats, sentiment, vectors) {
this.id = id;
this.textRepr = textRepr;
this.name = name;
this.type = type;
this.args = args;
this.support = support;
this.feats = feats;
this.sentiment = sentiment;
this.vectors = vectors;
}
/** Relation factory method, public constructor. */
static of(id, textRepr, name, type, args, support = null, feats = null, sentiment = null, vectors = null) {
return new Relation(id, textRepr, name, type, args, support !== null && support !== void 0 ? support : [], feats !== null && feats !== void 0 ? feats : new Map(), sentiment, vectors);
}
isNegated() {
return this.feats.get(Relation.FEAT_NEGATED) === "true" ? true : false;
}
modality() {
var _a;
return (_a = this.feats.get(Relation.FEAT_MODALITY)) !== null && _a !== void 0 ? _a : null;
}
/** Returns the relation subject argument, if precisely one is present; otherwise returns null. */
subj() {
const args = this.args.filter((x) => x.type.toUpperCase() === "SUBJECT");
return args.length === 1 ? args[0] : null;
}
/** Returns the relation object argument, if precisely one is present; otherwise returns null. */
obj() {
const args = this.args.filter((x) => x.type.toUpperCase() === "OBJECT");
return args.length === 1 ? args[0] : null;
}
toString() {
return (0, common_1.objToStr)(this, [
"id",
"textRepr",
"name",
"type",
"args",
"feats",
"support",
"sentiment",
"vectors",
]);
}
}
exports.Relation = Relation;
/** Attribute relation (e.g. `good(pizza)` for _good pizza_, _pizza is good_). */
Relation.TYPE_ATTR = "attr";
/** Verbal relation (e.g. `eat(pizza)` for _eat a pizza_. */
Relation.TYPE_RELATION = "relation";
/**
* Relation where at least one argument is outside of the the document (e.g. between `pizza` in the document and
* `food` item in the knowledgebase).
*/
Relation.TYPE_EXTERNAL = "external";
/** Key presence signifies it is a negated word, value = True.*/
Relation.FEAT_NEGATED = "negated";
/** Feature storing info about modality. */
Relation.FEAT_MODALITY = "modality";
/** Object representing an argument (subject/object) of a relation. */
class RelationArgument {
/**
*
* @param name Name of the argument (e.g. John).
* @param type Type of the argument (subject, object).
* @param entity The entity corresponding to this argument, if any. null if the argument is not an entity.
*/
constructor(name, type, entity) {
this.name = name;
this.type = type;
this.entity = entity;
}
toString() {
return (0, common_1.objToStr)(this, ["name", "type"]);
}
}
exports.RelationArgument = RelationArgument;
/** Tokens corresponding to a single head (predicate) of a relation. */
class RelationSupport {
/**
*
* @param tokens Tokens corresponding to the head of the relation.
* @param tectoToken Tecto token corresponding to the tokens. null if tecto tokens are not part of the model.
*/
constructor(tokens, tectoToken) {
this.tokens = tokens;
this.tectoToken = tectoToken;
}
/** RelationSupport factory method, public constructor. */
static of(tokens, tectoToken = null) {
return new RelationSupport(token_1.TokenSupport.of(tokens), tectoToken);
}
toString() {
return (0, common_1.objToStr)(this, Object.keys(this));
}
}
exports.RelationSupport = RelationSupport;