concepts-parser
Version:
Concepts Extracting from text
100 lines (99 loc) • 2.84 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const utils = require("./utils");
const splitter_1 = require("./splitter");
const MAX_LENGTH = 100;
class Concept extends types_1.Model {
constructor(args) {
super(args);
this.reset(this.value, this.index, this.lang);
}
reset(value, index, lang) {
if (typeof value !== "string") {
throw new Error("Invalid field `value`");
}
this.set("value", value);
if (typeof lang !== "string" || lang.trim().length !== 2) {
throw new Error("Invalid field `lang`");
}
this.set("lang", lang.trim().toLowerCase());
if (typeof index === "number" && index > -1) {
this.set("index", index);
}
else {
this.set("index", this.index || 0);
}
const words = value.split(/[ ]+/g);
this.set("atonicValue", utils.atonic(value));
if (words.length === 1 && value === value.toUpperCase()) {
this.set("isAbbr", true);
}
this.set("countWords", words.length);
if (words.length > 1) {
if (utils.isDigit(words[words.length - 1])) {
this.set("endsWithNumber", true);
}
}
if (value[value.length - 1] === ".") {
this.set("endsWithDot", true);
}
}
isValid() {
let value = this.value;
if (!value ||
value.length < 2 ||
value.length > MAX_LENGTH ||
utils.isDigit(value)) {
return false;
}
if (value.length !== value.trim().length) {
return false;
}
if (value.length === 2 &&
/[!"#%&'\(\)\*,\.\/:\?@\[\]\\_{}-]/.test(value[1])) {
return false;
}
return true;
}
split() {
return splitter_1.split(this);
}
get value() {
return this.get("value");
}
get lang() {
return this.get("lang");
}
get abbr() {
return this.get("abbr");
}
set abbr(value) {
this.set("abbr", value);
}
get isAbbr() {
return this.get("isAbbr");
}
set isAbbr(value) {
this.set("isAbbr", value);
}
get index() {
return this.get("index");
}
get endIndex() {
return this.index + this.value.length;
}
get countWords() {
return this.get("countWords");
}
get atonicValue() {
return this.get("atonicValue");
}
get endsWithDot() {
return this.get("endsWithDot");
}
get endsWithNumber() {
return this.get("endsWithNumber");
}
}
exports.Concept = Concept;
;