UNPKG

wordmap

Version:
82 lines (81 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Represents two individual n-grams that have been matched from two texts. * e.g. from a primary text and secondary text. */ class Alignment { /** * Instantiates a new alignment * @param {Ngram} sourceNgram - an n-gram from the source text * @param {Ngram} targetNgram - an n-gram from the secondary text */ constructor(sourceNgram, targetNgram) { this.sourceNgram = sourceNgram; this.targetNgram = targetNgram; } /** * Returns the n-gram from the source text. * @deprecated Consider using {@link sourceNgram} instead since getters have a performance hit. * @return {Ngram} */ get source() { return this.sourceNgram; } /** * Returns the n-gram from the target text * @deprecated Consider using {@link targetNgram} instead since getters have a performance hit. * @return {Ngram} */ get target() { return this.targetNgram; } /** * Returns the alignment key. * TODO: would a regular function be faster? * @return {string} */ get key() { this.cacheKeys(); return this.cachedKey; } /** * Returns the alignment lemma-based key */ get lemmaKey() { this.cacheKeys(); return this.cachedLemmaKey; } /** * Outputs the alignment to json * @param verbose - print full metadata * @return {object} */ toJSON(verbose = false) { return { sourceNgram: this.sourceNgram.toJSON(verbose), targetNgram: this.targetNgram.toJSON(verbose) }; } /** * Caches the keys if they have not already been generated */ cacheKeys() { if (this.cachedKey === undefined) { this.cachedKey = `${this.sourceNgram.key}->${this.targetNgram.key}`; // TRICKY: the alignment supports lemma fallback if either language has lemma const sourceHasLemma = this.sourceNgram.lemmaKey !== undefined; const targetHasLemma = this.targetNgram.lemmaKey !== undefined; if (sourceHasLemma || targetHasLemma) { const sourceLemma = sourceHasLemma ? this.sourceNgram.lemmaKey : this.sourceNgram.key; const targetLemma = targetHasLemma ? this.targetNgram.lemmaKey : this.targetNgram.key; this.cachedLemmaKey = `${sourceLemma}->${targetLemma}`; } } } } exports.default = Alignment;