UNPKG

@palasimi/ipa-cluster

Version:

Cluster words with similar IPA transcriptions together

76 lines 2.14 kB
"use strict"; // SPDX-License-Identifier: GPL-3.0-or-later // Copyright (c) 2023 Levi Gruspe // DSL compiler. Object.defineProperty(exports, "__esModule", { value: true }); exports.compile = exports.Querier = void 0; const context_1 = require("./context"); const final_1 = require("./final"); const parser_1 = require("./parser"); /** * Used for querying rules. */ class Querier { constructor() { this.map = new Map(); } /** * Adds a rule. */ add(rule) { // `left <= right` after splitting, so we don't have to check again. const { left, right } = rule; const key = `${left}~${right}`; if (!this.map.has(key)) { this.map.set(key, new context_1.ContextMatcher()); } const matcher = this.map.get(key); matcher.add(rule); } /** * Checks if the query satisfies a defined rule. * * @param s - An array of IPA segments * @param t - An array of IPA segments * @param i - Index to an element in `s` * @param j - Index to an element in `t` * @param l1 - Language of `s` * @param l2 - Language of `t` */ query(s, t, i, j, l1, l2) { // Construct map key. // "_" stands for "delete the opposite segment." let a = s[i] || "_"; let b = t[j] || "_"; if (a === b) { return true; } if (b < a) { [s, t] = [t, s]; [i, j] = [j, i]; [a, b] = [b, a]; [l1, l2] = [l2, l1]; } const key = `${a}~${b}`; const matcher = this.map.get(key); if (matcher == null) { return false; } return matcher.test(s, t, i, j, l1, l2); } } exports.Querier = Querier; /** * Compiles code into a rule query function. * May throw `ParseError`s during parsing. */ function compile(code) { const querier = new Querier(); const ir = (0, final_1.finalize)((0, parser_1.parse)(code)); for (const rule of ir.rules) { querier.add(rule); } return querier; } exports.compile = compile; //# sourceMappingURL=compiler.js.map