Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | 18x 18x 75x 75x 75x 75x 95x 41x 54x 54x 2x 54x 54x 54x 95x 72x 23x 2x 4x 11x 11x 2x 2x 1x 13x 13x 1x 12x 58x 58x 5x 5x 4x 115x 115x 115x 29x 20x 115x 24x 24x 24x 24x 21x 21x 20x 1x 24x 24x 24x 19x 16x 24x 24x 1x 1x 1x 1x 24x 9x 9x 6x 3x 3x 3x 6x 6x 6x 6x 6x 3x 3x 3x 18x | /*
* Copyright (c) AXA Shared Services Spain S.A.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const { SimilarSearch } = require('../util');
const NamedEntity = require('./named-entity');
/**
* Class for a named entities manager, that can be recognized inside
* a string.
* Basically, a named entity is an enumerator that has a set of options,
* and each option can have several words to refer to this option.
* Example:
* We have the entity "Superhero". Inside "Superhero" we have different
* options:
* - Superman: Superman
* - Spiderman: Spiderman, Spider-man
* - Wolverine: Wolverine, Logan, Patch, Weapon X
*/
class NerManager {
/**
* Constructor of the class.
* @param {Object} settings Settings for initializing this instance.
*/
constructor(settings) {
this.settings = settings || {};
this.threshold = this.settings.threshold || 0.5;
this.namedEntities = {};
this.similar = new SimilarSearch({ normalize: true });
}
/**
* Adds a new entity to be managed by the NER. If the entity already exists,
* then returns the already existing one.
* @param {String} entityName Name of the entity.
* @returns {Object} Already existing entity or the new one created.
*/
addNamedEntity(entityName, regex) {
if (this.namedEntities[entityName]) {
return this.namedEntities[entityName];
}
const options = { name: entityName };
if (regex) {
options.regex = regex;
}
const entity = new NamedEntity(options);
this.namedEntities[entityName] = entity;
return entity;
}
/**
* Get an entity given its name. If the entity does not exists and the
* force flag is on, then creates the entity.
* @param {String} entityName Name of the entity.
* @param {boolean} force Flag to create the entity when it does not exists.
* @returns {Object} The entity, or undefined if not found and not forced.
*/
getNamedEntity(entityName, force = false) {
if (force) {
return this.addNamedEntity(entityName);
}
return this.namedEntities[entityName];
}
/**
* Removes an entity from the NER.
* @param {String} entityName Name of the entity.
*/
removeNamedEntity(entityName) {
delete this.namedEntities[entityName];
}
/**
* Given an entity and an option name, returns the position of the option.
* @param {Object} entity Entiy for searching the option.
* @param {String} optionName Name of the option.
* @return {Object} Position of the option, -1 if not found.
*/
getOptionsPositionFromEntity(entity, optionName) {
return entity.getOptionPosition(optionName);
}
/**
* Adds an option to an entity of the NER.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option to be added.
* @returns Existing option if it already exists, or new one if created.
*/
addNamedEntityOption(entityName, optionName) {
const entity = this.getNamedEntity(entityName, true);
return entity.addOption(optionName);
}
/**
* Given an entity name and an option name, removes the option from
* the entity.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
*/
removeNamedEntityOption(entityName, optionName) {
const entity = this.getNamedEntity(entityName);
if (entity) {
entity.removeOption(optionName);
}
}
/**
* Given an entity name and an option name, return this option from
* the entity. If force flag is active, and the entity or the option
* does not exists, then are forced to be created.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
* @param {boolean} force Flag for forcing creation of entity or option.
* @returns {Object} Option already existing, or the created one.
*/
getNamedEntityOption(entityName, optionName, force = false) {
const entity = this.getNamedEntity(entityName, force);
if (!entity) {
return undefined;
}
return entity.getOption(optionName, force);
}
/**
* Add texts to the given languages of an option of an entity.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
* @param {String[]} srcLanguages Language or languages for adding the texts.
* @param {String[]} srcTexts Text or texts to be added.
*/
addNamedEntityText(entityName, optionName, srcLanguages, srcTexts) {
const entity = this.getNamedEntity(entityName, true);
entity.addText(optionName, srcLanguages, srcTexts);
}
/**
* Remove texts for the given languages of the option of an entity.
* @param {String} entityName Name of the entity.
* @param {String} optionName Name of the option.
* @param {String[]} srcLanguages Languages affected.
* @param {String[]} srcTexts Texts to be removed.
*/
removeNamedEntityText(entityName, optionName, srcLanguages, srcTexts) {
const entity = this.getNamedEntity(entityName);
if (entity) {
entity.removeText(optionName, srcLanguages, srcTexts);
}
}
/**
* Given an utterance, search for %entity% format inside it, in order to
* return the list of entities referenced by the utterance.
* @param {String} utterance Utterance for searching.
* @returns {String[]} List of entities.
*/
getEntitiesFromUtterance(utterance) {
const entityKeys = Object.keys(this.namedEntities);
const result = [];
entityKeys.forEach((entity) => {
if (utterance.indexOf(`%${entity}%`) > -1) {
result.push(entity);
}
});
return result;
}
/**
* Find entities inside an utterance.
* @param {String} utterance Utterance for searching entities.
* @param {String} locale Locale of the language.
* @param {String[]} whitelist Whitelist of entity names.
*/
findEntities(utterance, locale, whitelist) {
const entityNames = whitelist || Object.keys(this.namedEntities);
const entitiesEnum = {};
const entitiesRegex = {};
entityNames.forEach((entityName) => {
const entity = this.namedEntities[entityName];
if (entity.type === 'enum') {
entitiesEnum[entityName] = entity;
} else {
entitiesRegex[entityName] = entity;
}
});
const resultEntities = this.similar.getBestEntity(utterance, entitiesEnum, locale);
const result = [];
resultEntities.forEach((resultEntity) => {
if (resultEntity.accuracy >= this.threshold) {
result.push(resultEntity);
}
});
const keys = Object.keys(entitiesRegex);
for (let i = 0; i < keys.length; i += 1) {
const entity = entitiesRegex[keys[i]];
const matchs = entity.getMatchs(utterance);
matchs.forEach((match) => {
result.push(Object.assign(match, { entity: keys[i] }));
});
}
return result;
}
generateEntityUtterance(utterance, locale) {
const entities = this.findEntities(utterance, locale);
if (entities.length === 0) {
return utterance;
}
let index = 0;
let result = '';
for (let i = 0; i < entities.length; i += 1) {
const entity = entities[i];
const left = utterance.slice(index, entity.start);
index = entity.end;
result += left;
result += `%${entity.entity}%`;
}
const right = utterance.slice(entities[entities.length - 1].end);
result += right;
return result;
}
}
module.exports = NerManager;
|