@lokascript/framework 4.8.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +70 -1
- package/dist/api/create-dsl.d.ts +9 -10
- package/dist/api/create-dsl.d.ts.map +1 -1
- package/dist/api/index.js +835 -1670
- package/dist/api/index.js.map +1 -1
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +549 -1132
- package/dist/core/index.js.map +1 -1
- package/dist/core/matching/index.d.ts +2 -0
- package/dist/core/matching/index.d.ts.map +1 -1
- package/dist/core/matching/possessive-keywords.d.ts.map +1 -0
- package/dist/core/matching/stock-confidence.d.ts +3 -4
- package/dist/core/matching/stock-confidence.d.ts.map +1 -1
- package/dist/core/matching/stock-reading.d.ts +4 -4
- package/dist/core/{pattern-matching/utils → matching}/type-validation.d.ts +1 -1
- package/dist/core/matching/type-validation.d.ts.map +1 -0
- package/dist/generation/index.d.ts +1 -1
- package/dist/generation/index.d.ts.map +1 -1
- package/dist/generation/index.js +20 -169
- package/dist/generation/index.js.map +1 -1
- package/dist/generation/profile.d.ts +30 -0
- package/dist/generation/profile.d.ts.map +1 -0
- package/dist/generation/renderer.d.ts +1 -1
- package/dist/generation/renderer.d.ts.map +1 -1
- package/dist/generation/schema-patterns.d.ts +3 -3
- package/dist/index.cjs +929 -1674
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +929 -1668
- package/dist/index.js.map +1 -1
- package/dist/multilingual/builders.d.ts +3 -3
- package/dist/multilingual/builders.d.ts.map +1 -1
- package/dist/multilingual/index.js.map +1 -1
- package/dist/parsing/index.d.ts +0 -1
- package/dist/parsing/index.d.ts.map +1 -1
- package/dist/parsing/index.js +0 -1145
- package/dist/parsing/index.js.map +1 -1
- package/dist/testing/gate/lint/types.d.ts +1 -1
- package/dist/testing/gate/lint/types.d.ts.map +1 -1
- package/package.json +3 -8
- package/src/__test__/test-utils.ts +1 -1
- package/src/api/create-dsl-engine.test.ts +60 -35
- package/src/api/create-dsl.ts +24 -35
- package/src/core/index.ts +0 -1
- package/src/core/matching/index.ts +2 -0
- package/src/core/matching/stock-confidence.ts +3 -4
- package/src/core/matching/stock-reading.ts +6 -6
- package/src/core/{pattern-matching/utils → matching}/type-validation.test.ts +2 -2
- package/src/core/{pattern-matching/utils → matching}/type-validation.ts +1 -1
- package/src/generation/domain-renderer.test.ts +1 -1
- package/src/generation/index.ts +1 -1
- package/src/generation/profile.ts +41 -0
- package/src/generation/renderer.test.ts +1 -1
- package/src/generation/renderer.ts +1 -1
- package/src/generation/schema-patterns.ts +3 -3
- package/src/multilingual/bridge.test.ts +31 -23
- package/src/multilingual/builders.ts +3 -3
- package/src/parsing/index.ts +0 -1
- package/src/testing/gate/lint/types.ts +1 -1
- package/dist/core/pattern-matching/index.d.ts +0 -6
- package/dist/core/pattern-matching/index.d.ts.map +0 -1
- package/dist/core/pattern-matching/index.js +0 -1211
- package/dist/core/pattern-matching/index.js.map +0 -1
- package/dist/core/pattern-matching/pattern-matcher.d.ts +0 -122
- package/dist/core/pattern-matching/pattern-matcher.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/index.d.ts +0 -6
- package/dist/core/pattern-matching/utils/index.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/possessive-keywords.d.ts.map +0 -1
- package/dist/core/pattern-matching/utils/type-validation.d.ts.map +0 -1
- package/dist/generation/pattern-generator.d.ts +0 -55
- package/dist/generation/pattern-generator.d.ts.map +0 -1
- package/src/core/pattern-matching/index.ts +0 -6
- package/src/core/pattern-matching/pattern-matcher.test.ts +0 -1031
- package/src/core/pattern-matching/pattern-matcher.ts +0 -765
- package/src/core/pattern-matching/utils/index.ts +0 -6
- package/src/generation/pattern-generator.test.ts +0 -532
- package/src/generation/pattern-generator.ts +0 -327
- /package/dist/core/{pattern-matching/utils → matching}/possessive-keywords.d.ts +0 -0
- /package/src/core/{pattern-matching/utils → matching}/possessive-keywords.ts +0 -0
package/dist/core/index.js
CHANGED
|
@@ -1678,89 +1678,387 @@ var BaseMorphologicalNormalizer = class {
|
|
|
1678
1678
|
}
|
|
1679
1679
|
};
|
|
1680
1680
|
|
|
1681
|
-
// src/core/
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1681
|
+
// src/core/matching/match-skeleton.ts
|
|
1682
|
+
function groupHasRole(group) {
|
|
1683
|
+
return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
|
|
1684
|
+
}
|
|
1685
|
+
var MatchSkeleton = class {
|
|
1686
|
+
/**
|
|
1687
|
+
* @param makeReader - builds the grammar's reader, handed this matcher's view
|
|
1688
|
+
* of the match in progress (one reader per matcher).
|
|
1689
|
+
*/
|
|
1690
|
+
constructor(makeReader, confidenceModel) {
|
|
1691
|
+
/**
|
|
1692
|
+
* The pattern tokens still to match after the one being matched, through
|
|
1693
|
+
* every enclosing group: a value stops at any marker the pattern still owes.
|
|
1694
|
+
*/
|
|
1695
|
+
this.patternRest = [];
|
|
1696
|
+
/**
|
|
1697
|
+
* Whether the role slot being matched directly follows a literal the pattern
|
|
1698
|
+
* matched: its marker (`[por {quantity}]`), or the command's verb.
|
|
1699
|
+
*/
|
|
1700
|
+
this.slotAfterLiteral = false;
|
|
1701
|
+
/**
|
|
1702
|
+
* Selective memoization cache (Phase 6.1).
|
|
1703
|
+
* Caches pattern match results keyed by `tokenPosition:patternId`.
|
|
1704
|
+
* Cleared before each top-level matchBest() call.
|
|
1705
|
+
*/
|
|
1706
|
+
this.matchCache = /* @__PURE__ */ new Map();
|
|
1707
|
+
/**
|
|
1708
|
+
* Stream index where each role's capture began, for the current
|
|
1709
|
+
* `matchPattern` call. Rebuilt per call, written on the same success path as
|
|
1710
|
+
* `captured` — see `PatternMatchResult.roleStarts`.
|
|
1711
|
+
*/
|
|
1712
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
1713
|
+
/**
|
|
1714
|
+
* Track stem matches for confidence calculation.
|
|
1715
|
+
* This is set during matching and read during confidence calculation.
|
|
1716
|
+
*/
|
|
1717
|
+
this.stemMatchCount = 0;
|
|
1718
|
+
this.totalKeywordMatches = 0;
|
|
1719
|
+
this.confidenceModel = confidenceModel;
|
|
1720
|
+
this.reader = makeReader({
|
|
1721
|
+
patternRest: () => this.patternRest,
|
|
1722
|
+
slotAfterLiteral: () => this.slotAfterLiteral,
|
|
1723
|
+
wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
|
|
1724
|
+
});
|
|
1686
1725
|
}
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1726
|
+
/**
|
|
1727
|
+
* Try to match a single pattern against the token stream.
|
|
1728
|
+
* Returns the match result or null if no match.
|
|
1729
|
+
*/
|
|
1730
|
+
matchPattern(tokens, pattern) {
|
|
1731
|
+
const mark = tokens.mark();
|
|
1732
|
+
const captured = /* @__PURE__ */ new Map();
|
|
1733
|
+
this.reader.beginPattern(pattern);
|
|
1734
|
+
this.stemMatchCount = 0;
|
|
1735
|
+
this.totalKeywordMatches = 0;
|
|
1736
|
+
this.roleStarts = /* @__PURE__ */ new Map();
|
|
1737
|
+
this.patternRest = [];
|
|
1738
|
+
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
1739
|
+
if (!success) {
|
|
1740
|
+
tokens.reset(mark);
|
|
1741
|
+
return null;
|
|
1691
1742
|
}
|
|
1692
|
-
|
|
1743
|
+
const confidence = this.confidenceModel.calculate({
|
|
1744
|
+
pattern,
|
|
1745
|
+
captured,
|
|
1746
|
+
stemMatchCount: this.stemMatchCount,
|
|
1747
|
+
totalKeywordMatches: this.totalKeywordMatches
|
|
1748
|
+
});
|
|
1749
|
+
this.applyExtractionRules(pattern, captured);
|
|
1750
|
+
return {
|
|
1751
|
+
pattern,
|
|
1752
|
+
captured,
|
|
1753
|
+
consumedTokens: tokens.position() - mark.position,
|
|
1754
|
+
confidence,
|
|
1755
|
+
roleStarts: this.roleStarts,
|
|
1756
|
+
...this.reader.resultFields?.()
|
|
1757
|
+
};
|
|
1693
1758
|
}
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1759
|
+
/**
|
|
1760
|
+
* Try to match multiple patterns, return the best match.
|
|
1761
|
+
*/
|
|
1762
|
+
matchBest(tokens, patterns) {
|
|
1763
|
+
this.matchCache.clear();
|
|
1764
|
+
const matches = [];
|
|
1765
|
+
const startPos = tokens.position();
|
|
1766
|
+
for (const pattern of patterns) {
|
|
1767
|
+
const mark = tokens.mark();
|
|
1768
|
+
const cacheKey = `${startPos}:${pattern.id}`;
|
|
1769
|
+
let result;
|
|
1770
|
+
if (this.matchCache.has(cacheKey)) {
|
|
1771
|
+
result = this.matchCache.get(cacheKey);
|
|
1772
|
+
} else {
|
|
1773
|
+
result = this.matchPattern(tokens, pattern);
|
|
1774
|
+
this.matchCache.set(cacheKey, result);
|
|
1775
|
+
}
|
|
1776
|
+
if (result) {
|
|
1777
|
+
matches.push(result);
|
|
1778
|
+
}
|
|
1779
|
+
tokens.reset(mark);
|
|
1711
1780
|
}
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
this.log("error", message, ...args);
|
|
1781
|
+
if (matches.length === 0) {
|
|
1782
|
+
return null;
|
|
1783
|
+
}
|
|
1784
|
+
matches.sort((a, b) => {
|
|
1785
|
+
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
1786
|
+
if (priorityDiff !== 0) return priorityDiff;
|
|
1787
|
+
return b.confidence - a.confidence;
|
|
1788
|
+
});
|
|
1789
|
+
const best = matches[0];
|
|
1790
|
+
this.matchPattern(tokens, best.pattern);
|
|
1791
|
+
return best;
|
|
1724
1792
|
}
|
|
1725
1793
|
/**
|
|
1726
|
-
*
|
|
1794
|
+
* Match a sequence of pattern tokens against the token stream.
|
|
1727
1795
|
*/
|
|
1728
|
-
|
|
1729
|
-
|
|
1796
|
+
matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
|
|
1797
|
+
this.reader.skipLeading?.(tokens, patternTokens);
|
|
1798
|
+
for (let i = 0; i < patternTokens.length; i++) {
|
|
1799
|
+
const patternToken = patternTokens[i];
|
|
1800
|
+
this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
|
|
1801
|
+
if (this.reader.maySkipSlot(
|
|
1802
|
+
patternToken,
|
|
1803
|
+
tokens.peek(),
|
|
1804
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
1805
|
+
)) {
|
|
1806
|
+
const skipMark = tokens.mark();
|
|
1807
|
+
const speculative = new Map(captured);
|
|
1808
|
+
if (this.matchTokenSequence(
|
|
1809
|
+
tokens,
|
|
1810
|
+
patternTokens.slice(i + 1),
|
|
1811
|
+
speculative,
|
|
1812
|
+
nextAfterSequence
|
|
1813
|
+
) && this.reader.atClauseEnd(tokens)) {
|
|
1814
|
+
captured.clear();
|
|
1815
|
+
for (const [role, value] of speculative) captured.set(role, value);
|
|
1816
|
+
return true;
|
|
1817
|
+
}
|
|
1818
|
+
tokens.reset(skipMark);
|
|
1819
|
+
}
|
|
1820
|
+
const outerRest = this.patternRest;
|
|
1821
|
+
this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
|
|
1822
|
+
this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
|
|
1823
|
+
const matched = this.matchPatternToken(
|
|
1824
|
+
tokens,
|
|
1825
|
+
patternToken,
|
|
1826
|
+
captured,
|
|
1827
|
+
patternTokens[i + 1] ?? nextAfterSequence
|
|
1828
|
+
);
|
|
1829
|
+
this.patternRest = outerRest;
|
|
1830
|
+
if (!matched) {
|
|
1831
|
+
if (this.isOptional(patternToken)) {
|
|
1832
|
+
continue;
|
|
1833
|
+
}
|
|
1834
|
+
return false;
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
return true;
|
|
1730
1838
|
}
|
|
1731
1839
|
/**
|
|
1732
|
-
*
|
|
1840
|
+
* Match a single pattern token against the current position in the stream.
|
|
1733
1841
|
*/
|
|
1734
|
-
|
|
1735
|
-
|
|
1842
|
+
matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
|
|
1843
|
+
switch (patternToken.type) {
|
|
1844
|
+
case "literal":
|
|
1845
|
+
return this.matchLiteralToken(tokens, patternToken);
|
|
1846
|
+
case "role": {
|
|
1847
|
+
const roleStart = tokens.position();
|
|
1848
|
+
const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
|
|
1849
|
+
if (matchedRole && captured.has(patternToken.role)) {
|
|
1850
|
+
this.roleStarts.set(patternToken.role, roleStart);
|
|
1851
|
+
}
|
|
1852
|
+
return matchedRole;
|
|
1853
|
+
}
|
|
1854
|
+
case "group":
|
|
1855
|
+
return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
|
|
1856
|
+
default:
|
|
1857
|
+
return false;
|
|
1858
|
+
}
|
|
1736
1859
|
}
|
|
1737
1860
|
/**
|
|
1738
|
-
*
|
|
1861
|
+
* Match a literal pattern token (keyword or particle).
|
|
1739
1862
|
*/
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1863
|
+
matchLiteralToken(tokens, patternToken) {
|
|
1864
|
+
const token = tokens.peek();
|
|
1865
|
+
if (!token) return false;
|
|
1866
|
+
const matchType = this.getMatchType(token, patternToken.value);
|
|
1867
|
+
if (matchType !== "none") {
|
|
1868
|
+
this.totalKeywordMatches++;
|
|
1869
|
+
if (matchType === "stem") {
|
|
1870
|
+
this.stemMatchCount++;
|
|
1871
|
+
}
|
|
1872
|
+
tokens.advance();
|
|
1873
|
+
return true;
|
|
1874
|
+
}
|
|
1875
|
+
if (patternToken.alternatives) {
|
|
1876
|
+
for (const alt of patternToken.alternatives) {
|
|
1877
|
+
const altMatchType = this.getMatchType(token, alt);
|
|
1878
|
+
if (altMatchType !== "none") {
|
|
1879
|
+
this.totalKeywordMatches++;
|
|
1880
|
+
if (altMatchType === "stem") {
|
|
1881
|
+
this.stemMatchCount++;
|
|
1882
|
+
}
|
|
1883
|
+
tokens.advance();
|
|
1884
|
+
return true;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
|
|
1889
|
+
for (const sep of ["-", "_"]) {
|
|
1890
|
+
if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
|
|
1891
|
+
this.totalKeywordMatches++;
|
|
1892
|
+
return true;
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
return false;
|
|
1755
1897
|
}
|
|
1756
|
-
|
|
1898
|
+
/**
|
|
1899
|
+
* Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
|
|
1900
|
+
* shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
|
|
1901
|
+
* segments must be identifier/keyword/particle tokens equal to the expected
|
|
1902
|
+
* segments (case-insensitive; particle because prepositions like ru `по` /
|
|
1903
|
+
* uk `за` lead some underscore compounds), separators lone `sep` tokens.
|
|
1904
|
+
* Consumes the run on success, resets the stream on failure.
|
|
1905
|
+
*/
|
|
1906
|
+
tryMatchShatteredCompound(tokens, expected, sep) {
|
|
1907
|
+
const segments = expected.split(sep);
|
|
1908
|
+
if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
|
|
1909
|
+
const mark = tokens.mark();
|
|
1910
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1911
|
+
if (i > 0) {
|
|
1912
|
+
const sepToken = tokens.peek();
|
|
1913
|
+
if (!sepToken || sepToken.value !== sep) {
|
|
1914
|
+
tokens.reset(mark);
|
|
1915
|
+
return false;
|
|
1916
|
+
}
|
|
1917
|
+
tokens.advance();
|
|
1918
|
+
}
|
|
1919
|
+
const word = tokens.peek();
|
|
1920
|
+
if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
|
|
1921
|
+
tokens.reset(mark);
|
|
1922
|
+
return false;
|
|
1923
|
+
}
|
|
1924
|
+
tokens.advance();
|
|
1925
|
+
}
|
|
1757
1926
|
return true;
|
|
1758
1927
|
}
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1928
|
+
/**
|
|
1929
|
+
* Match a group pattern token (optional sequence).
|
|
1930
|
+
*/
|
|
1931
|
+
matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
|
|
1932
|
+
const mark = tokens.mark();
|
|
1933
|
+
const capturedBefore = new Map(captured);
|
|
1934
|
+
let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
|
|
1935
|
+
if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
|
|
1936
|
+
success = false;
|
|
1937
|
+
}
|
|
1938
|
+
if (success) {
|
|
1939
|
+
const markerLiterals = [];
|
|
1940
|
+
for (const inner of patternToken.tokens) {
|
|
1941
|
+
if (inner.type === "group" && inner.renderRequired) {
|
|
1942
|
+
for (const t of inner.tokens) {
|
|
1943
|
+
if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
if (markerLiterals.length > 0) {
|
|
1948
|
+
const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
|
|
1949
|
+
if (addedRole) {
|
|
1950
|
+
const last = tokens.tokens[tokens.position() - 1];
|
|
1951
|
+
const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
|
|
1952
|
+
if (!markerSeen) success = false;
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
if (!success) {
|
|
1957
|
+
tokens.reset(mark);
|
|
1958
|
+
for (const role of captured.keys()) {
|
|
1959
|
+
if (!capturedBefore.has(role)) {
|
|
1960
|
+
captured.delete(role);
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
for (const [role, value] of capturedBefore) {
|
|
1964
|
+
if (captured.get(role) !== value) {
|
|
1965
|
+
captured.set(role, value);
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
return patternToken.optional || false;
|
|
1969
|
+
}
|
|
1970
|
+
return true;
|
|
1971
|
+
}
|
|
1972
|
+
/**
|
|
1973
|
+
* Whether a pattern token (literal, or a group starting with a literal) would
|
|
1974
|
+
* match the given stream token. Used to keep the event-head source-clause
|
|
1975
|
+
* consumption from stealing a marker the pattern explicitly expects.
|
|
1976
|
+
*/
|
|
1977
|
+
patternTokenWouldMatch(pt, token) {
|
|
1978
|
+
if (!pt) return false;
|
|
1979
|
+
if (pt.type === "literal") {
|
|
1980
|
+
if (this.getMatchType(token, pt.value) !== "none") return true;
|
|
1981
|
+
return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
|
|
1982
|
+
}
|
|
1983
|
+
if (pt.type === "group") {
|
|
1984
|
+
return this.patternTokenWouldMatch(pt.tokens?.[0], token);
|
|
1985
|
+
}
|
|
1986
|
+
return false;
|
|
1987
|
+
}
|
|
1988
|
+
/**
|
|
1989
|
+
* Get the type of match for a token against a value.
|
|
1990
|
+
* Used for confidence calculation.
|
|
1991
|
+
*/
|
|
1992
|
+
getMatchType(token, value) {
|
|
1993
|
+
if (token.value === value) return "exact";
|
|
1994
|
+
if (token.normalized === value) return "normalized";
|
|
1995
|
+
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
1996
|
+
return "stem";
|
|
1997
|
+
}
|
|
1998
|
+
if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
|
|
1999
|
+
return "case-insensitive";
|
|
2000
|
+
}
|
|
2001
|
+
if (token.kind === "keyword") {
|
|
2002
|
+
const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
|
|
2003
|
+
if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
|
|
2004
|
+
return "case-insensitive";
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
return "none";
|
|
2008
|
+
}
|
|
2009
|
+
/**
|
|
2010
|
+
* Apply extraction rules to fill in static values and defaults for missing
|
|
2011
|
+
* roles, and re-shape captured roles that declare a `transform` (the
|
|
2012
|
+
* ExtractionRule field was documented but unwired — go-qu-url-dest is the
|
|
2013
|
+
* first consumer: it re-types the marker-bound capture to the expression
|
|
2014
|
+
* shape the en reference produces).
|
|
2015
|
+
*/
|
|
2016
|
+
applyExtractionRules(pattern, captured) {
|
|
2017
|
+
for (const [role, rule] of Object.entries(pattern.extraction)) {
|
|
2018
|
+
const existing = captured.get(role);
|
|
2019
|
+
if (existing) {
|
|
2020
|
+
if (rule.transform) {
|
|
2021
|
+
const raw = existing.type === "literal" ? String(existing.value) : String(existing.raw ?? "");
|
|
2022
|
+
captured.set(role, rule.transform(raw));
|
|
2023
|
+
}
|
|
2024
|
+
} else if (rule.value !== void 0) {
|
|
2025
|
+
captured.set(
|
|
2026
|
+
role,
|
|
2027
|
+
rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
|
|
2028
|
+
);
|
|
2029
|
+
} else if (rule.default) {
|
|
2030
|
+
const implicitDefault = {
|
|
2031
|
+
...rule.default,
|
|
2032
|
+
implicit: true
|
|
2033
|
+
};
|
|
2034
|
+
captured.set(role, implicitDefault);
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
/**
|
|
2039
|
+
* Check if a pattern token is optional.
|
|
2040
|
+
*/
|
|
2041
|
+
isOptional(patternToken) {
|
|
2042
|
+
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
2043
|
+
}
|
|
2044
|
+
};
|
|
2045
|
+
|
|
2046
|
+
// src/core/matching/type-validation.ts
|
|
2047
|
+
function isTypeCompatible(actualType, expectedTypes) {
|
|
2048
|
+
if (!expectedTypes || expectedTypes.length === 0) {
|
|
2049
|
+
return true;
|
|
2050
|
+
}
|
|
2051
|
+
if (expectedTypes.includes(actualType)) {
|
|
2052
|
+
return true;
|
|
2053
|
+
}
|
|
2054
|
+
if (expectedTypes.includes("expression")) {
|
|
2055
|
+
return true;
|
|
2056
|
+
}
|
|
2057
|
+
if (actualType === "property-path") {
|
|
2058
|
+
return expectedTypes.some((t) => ["selector", "reference", "expression"].includes(t));
|
|
2059
|
+
}
|
|
2060
|
+
return false;
|
|
2061
|
+
}
|
|
1764
2062
|
function validateValueType(value, expectedTypes) {
|
|
1765
2063
|
if (!expectedTypes || expectedTypes.length === 0) {
|
|
1766
2064
|
return true;
|
|
@@ -1798,7 +2096,7 @@ function isBuiltInReference(value) {
|
|
|
1798
2096
|
return builtIns.has(value.toLowerCase());
|
|
1799
2097
|
}
|
|
1800
2098
|
|
|
1801
|
-
// src/core/
|
|
2099
|
+
// src/core/matching/possessive-keywords.ts
|
|
1802
2100
|
function getPossessiveReference(profile, keyword) {
|
|
1803
2101
|
return profile?.possessive?.keywords?.[keyword];
|
|
1804
2102
|
}
|
|
@@ -2143,1076 +2441,61 @@ function tokenToSemanticValue(token) {
|
|
|
2143
2441
|
return createLiteral(token.normalized || token.value);
|
|
2144
2442
|
}
|
|
2145
2443
|
case "identifier": {
|
|
2146
|
-
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
2147
|
-
return createReference(token.value);
|
|
2148
|
-
}
|
|
2149
|
-
const identLower = safeToLowerCase(token.value);
|
|
2150
|
-
if (isValidReference(identLower)) {
|
|
2151
|
-
return createReference(identLower);
|
|
2152
|
-
}
|
|
2153
|
-
return { type: "expression", raw: token.value };
|
|
2154
|
-
}
|
|
2155
|
-
case "url":
|
|
2156
|
-
return createLiteral(token.value, "string");
|
|
2157
|
-
default:
|
|
2158
|
-
return null;
|
|
2159
|
-
}
|
|
2160
|
-
}
|
|
2161
|
-
function parseLiteralValue(value) {
|
|
2162
|
-
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
2163
|
-
const inner = value.slice(1, -1);
|
|
2164
|
-
return createLiteral(inner, "string");
|
|
2165
|
-
}
|
|
2166
|
-
if (value === "true") return createLiteral(true, "boolean");
|
|
2167
|
-
if (value === "false") return createLiteral(false, "boolean");
|
|
2168
|
-
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
2169
|
-
if (durationMatch) {
|
|
2170
|
-
const num2 = parseFloat(durationMatch[1]);
|
|
2171
|
-
const unit = durationMatch[2];
|
|
2172
|
-
if (unit) {
|
|
2173
|
-
return createLiteral(value, "duration");
|
|
2174
|
-
}
|
|
2175
|
-
return createLiteral(num2, "number");
|
|
2176
|
-
}
|
|
2177
|
-
const num = parseFloat(value);
|
|
2178
|
-
if (!isNaN(num)) {
|
|
2179
|
-
return createLiteral(num, "number");
|
|
2180
|
-
}
|
|
2181
|
-
return createLiteral(value, "string");
|
|
2182
|
-
}
|
|
2183
|
-
var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
2184
|
-
function skipNoiseWords(tokens) {
|
|
2185
|
-
const token = tokens.peek();
|
|
2186
|
-
if (!token) return;
|
|
2187
|
-
const tokenLower = safeToLowerCase(token.value);
|
|
2188
|
-
if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
2189
|
-
const mark = tokens.mark();
|
|
2190
|
-
tokens.advance();
|
|
2191
|
-
const nextToken = tokens.peek();
|
|
2192
|
-
if (nextToken && nextToken.kind === "selector") {
|
|
2193
|
-
return;
|
|
2194
|
-
}
|
|
2195
|
-
tokens.reset(mark);
|
|
2196
|
-
}
|
|
2197
|
-
if (tokenLower === "class") {
|
|
2198
|
-
tokens.advance();
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
|
|
2202
|
-
// src/core/matching/stock-confidence.ts
|
|
2203
|
-
function scoreParts(ctx) {
|
|
2204
|
-
const { pattern, captured } = ctx;
|
|
2205
|
-
let score = 0;
|
|
2206
|
-
let maxScore = 0;
|
|
2207
|
-
const hasDefault = (role) => {
|
|
2208
|
-
return pattern.extraction?.[role]?.default !== void 0;
|
|
2209
|
-
};
|
|
2210
|
-
for (const token of pattern.template.tokens) {
|
|
2211
|
-
if (token.type === "role") {
|
|
2212
|
-
maxScore += 1;
|
|
2213
|
-
if (captured.has(token.role)) {
|
|
2214
|
-
score += 1;
|
|
2215
|
-
}
|
|
2216
|
-
} else if (token.type === "group") {
|
|
2217
|
-
for (const subToken of token.tokens) {
|
|
2218
|
-
if (subToken.type === "role") {
|
|
2219
|
-
const roleHasDefault = hasDefault(subToken.role);
|
|
2220
|
-
const weight = 0.8;
|
|
2221
|
-
maxScore += weight;
|
|
2222
|
-
if (captured.has(subToken.role)) {
|
|
2223
|
-
score += weight;
|
|
2224
|
-
} else if (roleHasDefault) {
|
|
2225
|
-
score += weight * 0.6;
|
|
2226
|
-
}
|
|
2227
|
-
}
|
|
2228
|
-
}
|
|
2229
|
-
}
|
|
2230
|
-
}
|
|
2231
|
-
const roleCoverage = maxScore > 0 ? score / maxScore : 1;
|
|
2232
|
-
let baseConfidence = roleCoverage;
|
|
2233
|
-
let stemPenalty = 0;
|
|
2234
|
-
if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
|
|
2235
|
-
stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
|
|
2236
|
-
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
2237
|
-
}
|
|
2238
|
-
const vsoBoost = calculateVSOConfidenceBoost(pattern);
|
|
2239
|
-
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
2240
|
-
const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
|
|
2241
|
-
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
2242
|
-
return {
|
|
2243
|
-
roleCoverage,
|
|
2244
|
-
stemPenalty,
|
|
2245
|
-
languageBoost: vsoBoost,
|
|
2246
|
-
languageAdjustment: prepositionAdjustment,
|
|
2247
|
-
final: baseConfidence
|
|
2248
|
-
};
|
|
2249
|
-
}
|
|
2250
|
-
function calculateVSOConfidenceBoost(pattern) {
|
|
2251
|
-
if (pattern.language !== "ar") {
|
|
2252
|
-
return 0;
|
|
2253
|
-
}
|
|
2254
|
-
const firstToken = pattern.template.tokens[0];
|
|
2255
|
-
if (!firstToken || firstToken.type !== "literal") {
|
|
2256
|
-
return 0;
|
|
2257
|
-
}
|
|
2258
|
-
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
2259
|
-
"\u0628\u062F\u0644",
|
|
2260
|
-
"\u063A\u064A\u0631",
|
|
2261
|
-
"\u0623\u0636\u0641",
|
|
2262
|
-
"\u0623\u0632\u0644",
|
|
2263
|
-
"\u0636\u0639",
|
|
2264
|
-
"\u0627\u062C\u0639\u0644",
|
|
2265
|
-
"\u0639\u064A\u0646",
|
|
2266
|
-
"\u0632\u062F",
|
|
2267
|
-
"\u0627\u0646\u0642\u0635",
|
|
2268
|
-
"\u0633\u062C\u0644",
|
|
2269
|
-
"\u0623\u0638\u0647\u0631",
|
|
2270
|
-
"\u0623\u062E\u0641",
|
|
2271
|
-
"\u0634\u063A\u0644",
|
|
2272
|
-
"\u0623\u0631\u0633\u0644",
|
|
2273
|
-
"\u0631\u0643\u0632",
|
|
2274
|
-
"\u0634\u0648\u0634",
|
|
2275
|
-
"\u062A\u0648\u0642\u0641",
|
|
2276
|
-
"\u0627\u0646\u0633\u062E",
|
|
2277
|
-
"\u0627\u062D\u0630\u0641",
|
|
2278
|
-
"\u0627\u0635\u0646\u0639",
|
|
2279
|
-
"\u0627\u0646\u062A\u0638\u0631",
|
|
2280
|
-
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
2281
|
-
"\u0623\u0648"
|
|
2282
|
-
]);
|
|
2283
|
-
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
2284
|
-
return 0.15;
|
|
2285
|
-
}
|
|
2286
|
-
if (firstToken.alternatives) {
|
|
2287
|
-
for (const alt of firstToken.alternatives) {
|
|
2288
|
-
if (ARABIC_VERBS.has(alt)) {
|
|
2289
|
-
return 0.15;
|
|
2290
|
-
}
|
|
2291
|
-
}
|
|
2292
|
-
}
|
|
2293
|
-
return 0;
|
|
2294
|
-
}
|
|
2295
|
-
function arabicPrepositionDisambiguation(pattern, captured) {
|
|
2296
|
-
if (pattern.language !== "ar") {
|
|
2297
|
-
return 0;
|
|
2298
|
-
}
|
|
2299
|
-
let adjustment = 0;
|
|
2300
|
-
const PREFERRED_PREPOSITIONS = {
|
|
2301
|
-
patient: ["\u0639\u0644\u0649"],
|
|
2302
|
-
// element selectors prefer على (on/upon)
|
|
2303
|
-
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2304
|
-
// destination prefers إلى (to)
|
|
2305
|
-
source: ["\u0645\u0646"],
|
|
2306
|
-
// source prefers من (from)
|
|
2307
|
-
agent: ["\u0645\u0646"],
|
|
2308
|
-
// agent/by prefers من (from/by)
|
|
2309
|
-
manner: ["\u0628"],
|
|
2310
|
-
// manner prefers ب (with/by)
|
|
2311
|
-
style: ["\u0628"],
|
|
2312
|
-
// style prefers ب (with)
|
|
2313
|
-
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2314
|
-
// target state prefers إلى (to)
|
|
2315
|
-
method: ["\u0628"]
|
|
2316
|
-
// method prefers ب (with/by)
|
|
2317
|
-
};
|
|
2318
|
-
for (const [role, value] of captured.entries()) {
|
|
2319
|
-
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
2320
|
-
if (!preferred || preferred.length === 0) {
|
|
2321
|
-
continue;
|
|
2322
|
-
}
|
|
2323
|
-
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
2324
|
-
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
2325
|
-
const usedPreposition = metadata.prepositionValue;
|
|
2326
|
-
if (preferred.includes(usedPreposition)) {
|
|
2327
|
-
adjustment += 0.1;
|
|
2328
|
-
} else {
|
|
2329
|
-
adjustment -= 0.1;
|
|
2330
|
-
}
|
|
2331
|
-
}
|
|
2332
|
-
}
|
|
2333
|
-
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
2334
|
-
}
|
|
2335
|
-
var stockConfidenceModel = {
|
|
2336
|
-
calculate: (ctx) => scoreParts(ctx).final,
|
|
2337
|
-
calculateWithBreakdown: (ctx) => scoreParts(ctx)
|
|
2338
|
-
};
|
|
2339
|
-
|
|
2340
|
-
// src/core/pattern-matching/pattern-matcher.ts
|
|
2341
|
-
var _PatternMatcher = class _PatternMatcher {
|
|
2342
|
-
constructor() {
|
|
2343
|
-
/** Debug logger */
|
|
2344
|
-
this.logger = createLogger("pattern-matcher");
|
|
2345
|
-
/**
|
|
2346
|
-
* Track stem matches for confidence calculation.
|
|
2347
|
-
* This is set during matching and read during confidence calculation.
|
|
2348
|
-
*/
|
|
2349
|
-
this.stemMatchCount = 0;
|
|
2350
|
-
this.totalKeywordMatches = 0;
|
|
2351
|
-
}
|
|
2352
|
-
/**
|
|
2353
|
-
* Try to match a single pattern against the token stream.
|
|
2354
|
-
* Returns the match result or null if no match.
|
|
2355
|
-
*
|
|
2356
|
-
* @param tokens - Token stream to match against
|
|
2357
|
-
* @param pattern - Pattern to match
|
|
2358
|
-
* @param profile - Optional language profile for possessive handling
|
|
2359
|
-
*/
|
|
2360
|
-
matchPattern(tokens, pattern, profile) {
|
|
2361
|
-
const mark = tokens.mark();
|
|
2362
|
-
const captured = /* @__PURE__ */ new Map();
|
|
2363
|
-
this.logger.debug("========================================");
|
|
2364
|
-
this.logger.debug("matchPattern ENTRY");
|
|
2365
|
-
this.logger.debug("Pattern ID:", pattern.id);
|
|
2366
|
-
this.logger.debug("Pattern command:", pattern.command);
|
|
2367
|
-
this.logger.debug("Pattern language:", pattern.language);
|
|
2368
|
-
this.logger.debug("Pattern template:", JSON.stringify(pattern.template, null, 2));
|
|
2369
|
-
if (this.logger.isEnabled()) {
|
|
2370
|
-
const firstTokens = [];
|
|
2371
|
-
for (let i = 0; i < 10; i++) {
|
|
2372
|
-
const t = tokens.peek(i);
|
|
2373
|
-
if (t)
|
|
2374
|
-
firstTokens.push({
|
|
2375
|
-
type: t.type,
|
|
2376
|
-
value: t.value,
|
|
2377
|
-
kind: t.kind
|
|
2378
|
-
});
|
|
2379
|
-
else break;
|
|
2380
|
-
}
|
|
2381
|
-
this.logger.debug("Input tokens (first 10):", firstTokens);
|
|
2382
|
-
this.logger.debug("Profile code:", profile?.code);
|
|
2383
|
-
}
|
|
2384
|
-
this.currentProfile = profile;
|
|
2385
|
-
this.stemMatchCount = 0;
|
|
2386
|
-
this.totalKeywordMatches = 0;
|
|
2387
|
-
this.logger.debug("--- Calling matchTokenSequence ---");
|
|
2388
|
-
this.logger.debug("Pattern tokens to match:", JSON.stringify(pattern.template.tokens, null, 2));
|
|
2389
|
-
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
2390
|
-
this.logger.debug("matchTokenSequence returned:", success);
|
|
2391
|
-
this.logger.debug(
|
|
2392
|
-
"Captured roles:",
|
|
2393
|
-
Array.from(captured.entries()).map(([k, v]) => [k, JSON.stringify(v)])
|
|
2394
|
-
);
|
|
2395
|
-
if (!success) {
|
|
2396
|
-
this.logger.debug(">>> MATCH FAILED - resetting token position");
|
|
2397
|
-
tokens.reset(mark);
|
|
2398
|
-
return null;
|
|
2399
|
-
}
|
|
2400
|
-
const confidence = stockConfidenceModel.calculate({
|
|
2401
|
-
pattern,
|
|
2402
|
-
captured,
|
|
2403
|
-
stemMatchCount: this.stemMatchCount,
|
|
2404
|
-
totalKeywordMatches: this.totalKeywordMatches
|
|
2405
|
-
});
|
|
2406
|
-
this.applyExtractionRules(pattern, captured);
|
|
2407
|
-
return {
|
|
2408
|
-
pattern,
|
|
2409
|
-
captured,
|
|
2410
|
-
consumedTokens: tokens.position() - mark.position,
|
|
2411
|
-
confidence
|
|
2412
|
-
};
|
|
2413
|
-
}
|
|
2414
|
-
/**
|
|
2415
|
-
* Try to match multiple patterns, return the best match.
|
|
2416
|
-
*
|
|
2417
|
-
* @param tokens - Token stream to match against
|
|
2418
|
-
* @param patterns - Candidate patterns to try
|
|
2419
|
-
* @param profile - Optional language profile for possessive handling
|
|
2420
|
-
*/
|
|
2421
|
-
matchBest(tokens, patterns, profile) {
|
|
2422
|
-
const matches = [];
|
|
2423
|
-
for (const pattern of patterns) {
|
|
2424
|
-
const mark = tokens.mark();
|
|
2425
|
-
const result = this.matchPattern(tokens, pattern, profile);
|
|
2426
|
-
if (result) {
|
|
2427
|
-
matches.push(result);
|
|
2428
|
-
}
|
|
2429
|
-
tokens.reset(mark);
|
|
2430
|
-
}
|
|
2431
|
-
if (matches.length === 0) {
|
|
2432
|
-
return null;
|
|
2433
|
-
}
|
|
2434
|
-
matches.sort((a, b) => {
|
|
2435
|
-
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
2436
|
-
if (priorityDiff !== 0) return priorityDiff;
|
|
2437
|
-
const confidenceDiff = b.confidence - a.confidence;
|
|
2438
|
-
if (Math.abs(confidenceDiff) > 1e-3) return confidenceDiff;
|
|
2439
|
-
return b.consumedTokens - a.consumedTokens;
|
|
2440
|
-
});
|
|
2441
|
-
const best = matches[0];
|
|
2442
|
-
this.matchPattern(tokens, best.pattern);
|
|
2443
|
-
return best;
|
|
2444
|
-
}
|
|
2445
|
-
/**
|
|
2446
|
-
* Match a sequence of pattern tokens against the token stream.
|
|
2447
|
-
*
|
|
2448
|
-
* Supports bounded single-step backtracking: if an optional role consumes
|
|
2449
|
-
* a token and the immediately following pattern token fails, the matcher
|
|
2450
|
-
* resets to before the optional role and retries the failed token.
|
|
2451
|
-
*/
|
|
2452
|
-
matchTokenSequence(tokens, patternTokens, captured, parentStopMarkers) {
|
|
2453
|
-
const firstPatternToken = patternTokens[0];
|
|
2454
|
-
const patternExpectsConjunction = firstPatternToken?.type === "literal" && (firstPatternToken.value === "and" || firstPatternToken.value === "then" || firstPatternToken.alternatives?.includes("and") || firstPatternToken.alternatives?.includes("then"));
|
|
2455
|
-
if (this.currentProfile?.code === "ar" && !patternExpectsConjunction) {
|
|
2456
|
-
while (tokens.peek()?.kind === "conjunction") {
|
|
2457
|
-
tokens.advance();
|
|
2458
|
-
}
|
|
2459
|
-
}
|
|
2460
|
-
let prevOptionalMark = null;
|
|
2461
|
-
let prevOptionalRole = null;
|
|
2462
|
-
for (let i = 0; i < patternTokens.length; i++) {
|
|
2463
|
-
const patternToken = patternTokens[i];
|
|
2464
|
-
this.logger.debug(" >> Matching pattern token:", JSON.stringify(patternToken, null, 2));
|
|
2465
|
-
const currTok = tokens.peek();
|
|
2466
|
-
this.logger.debug(
|
|
2467
|
-
" >> Current input token:",
|
|
2468
|
-
currTok ? JSON.stringify({
|
|
2469
|
-
type: currTok.type,
|
|
2470
|
-
value: currTok.value,
|
|
2471
|
-
kind: currTok.kind
|
|
2472
|
-
}) : "EOF"
|
|
2473
|
-
);
|
|
2474
|
-
if (patternToken.type === "role" && patternToken.greedy) {
|
|
2475
|
-
const stopMarkers = this.collectStopMarkers(patternTokens, i + 1);
|
|
2476
|
-
if (parentStopMarkers) {
|
|
2477
|
-
for (const m of parentStopMarkers) stopMarkers.add(m);
|
|
2478
|
-
}
|
|
2479
|
-
const values = [];
|
|
2480
|
-
while (!tokens.isAtEnd()) {
|
|
2481
|
-
const nextToken = tokens.peek();
|
|
2482
|
-
if (!nextToken) break;
|
|
2483
|
-
if (this.isStopMarker(nextToken, stopMarkers)) break;
|
|
2484
|
-
values.push(nextToken.value);
|
|
2485
|
-
tokens.advance();
|
|
2486
|
-
}
|
|
2487
|
-
if (values.length > 0) {
|
|
2488
|
-
captured.set(patternToken.role, { type: "expression", raw: values.join(" ") });
|
|
2489
|
-
prevOptionalMark = null;
|
|
2490
|
-
prevOptionalRole = null;
|
|
2491
|
-
continue;
|
|
2492
|
-
} else if (patternToken.optional) {
|
|
2493
|
-
continue;
|
|
2494
|
-
} else {
|
|
2495
|
-
return false;
|
|
2496
|
-
}
|
|
2497
|
-
}
|
|
2498
|
-
if (patternToken.type === "group") {
|
|
2499
|
-
const siblingStopMarkers = this.collectStopMarkers(patternTokens, i + 1);
|
|
2500
|
-
if (parentStopMarkers) {
|
|
2501
|
-
for (const m of parentStopMarkers) siblingStopMarkers.add(m);
|
|
2502
|
-
}
|
|
2503
|
-
const groupMatched = this.matchGroupToken(
|
|
2504
|
-
tokens,
|
|
2505
|
-
patternToken,
|
|
2506
|
-
captured,
|
|
2507
|
-
siblingStopMarkers
|
|
2508
|
-
);
|
|
2509
|
-
if (groupMatched) {
|
|
2510
|
-
prevOptionalMark = null;
|
|
2511
|
-
prevOptionalRole = null;
|
|
2512
|
-
continue;
|
|
2513
|
-
}
|
|
2514
|
-
if (this.isOptional(patternToken)) {
|
|
2515
|
-
continue;
|
|
2516
|
-
}
|
|
2517
|
-
return false;
|
|
2518
|
-
}
|
|
2519
|
-
const isOptionalRole = patternToken.type === "role" && patternToken.optional === true;
|
|
2520
|
-
const markBefore = isOptionalRole ? tokens.mark() : null;
|
|
2521
|
-
const isRoleToken = patternToken.type === "role";
|
|
2522
|
-
if (isRoleToken && !isOptionalRole) {
|
|
2523
|
-
const nextPT = i + 1 < patternTokens.length ? patternTokens[i + 1] : null;
|
|
2524
|
-
if (nextPT?.type === "literal") {
|
|
2525
|
-
const currentToken = tokens.peek();
|
|
2526
|
-
if (currentToken && this.getMatchType(currentToken, nextPT.value) !== "none") {
|
|
2527
|
-
this.logger.debug(
|
|
2528
|
-
" >> MARKER-STEAL GUARD: current token matches next literal, skipping role"
|
|
2529
|
-
);
|
|
2530
|
-
this.logger.debug(" >> Token match FAILED");
|
|
2531
|
-
if (this.isOptional(patternToken)) {
|
|
2532
|
-
continue;
|
|
2533
|
-
}
|
|
2534
|
-
if (prevOptionalMark && prevOptionalRole) {
|
|
2535
|
-
this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
|
|
2536
|
-
tokens.reset(prevOptionalMark);
|
|
2537
|
-
captured.delete(prevOptionalRole);
|
|
2538
|
-
prevOptionalMark = null;
|
|
2539
|
-
prevOptionalRole = null;
|
|
2540
|
-
const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
|
|
2541
|
-
this.logger.debug(" >> Backtrack retry result:", retryMatched);
|
|
2542
|
-
if (retryMatched) {
|
|
2543
|
-
continue;
|
|
2544
|
-
}
|
|
2545
|
-
}
|
|
2546
|
-
return false;
|
|
2547
|
-
}
|
|
2548
|
-
}
|
|
2549
|
-
}
|
|
2550
|
-
const matched = this.matchPatternToken(tokens, patternToken, captured);
|
|
2551
|
-
this.logger.debug(" >> Match result:", matched);
|
|
2552
|
-
if (matched) {
|
|
2553
|
-
if (isOptionalRole) {
|
|
2554
|
-
prevOptionalMark = markBefore;
|
|
2555
|
-
prevOptionalRole = patternToken.role;
|
|
2556
|
-
} else {
|
|
2557
|
-
prevOptionalMark = null;
|
|
2558
|
-
prevOptionalRole = null;
|
|
2559
|
-
}
|
|
2560
|
-
continue;
|
|
2561
|
-
}
|
|
2562
|
-
this.logger.debug(" >> Token match FAILED");
|
|
2563
|
-
if (this.isOptional(patternToken)) {
|
|
2564
|
-
continue;
|
|
2565
|
-
}
|
|
2566
|
-
if (prevOptionalMark && prevOptionalRole) {
|
|
2567
|
-
this.logger.debug(" >> BACKTRACKING: undoing optional role", prevOptionalRole);
|
|
2568
|
-
tokens.reset(prevOptionalMark);
|
|
2569
|
-
captured.delete(prevOptionalRole);
|
|
2570
|
-
prevOptionalMark = null;
|
|
2571
|
-
prevOptionalRole = null;
|
|
2572
|
-
const retryMatched = this.matchPatternToken(tokens, patternToken, captured);
|
|
2573
|
-
this.logger.debug(" >> Backtrack retry result:", retryMatched);
|
|
2574
|
-
if (retryMatched) {
|
|
2575
|
-
continue;
|
|
2576
|
-
}
|
|
2577
|
-
}
|
|
2578
|
-
return false;
|
|
2579
|
-
}
|
|
2580
|
-
return true;
|
|
2581
|
-
}
|
|
2582
|
-
/**
|
|
2583
|
-
* Match a single pattern token against the current position in the stream.
|
|
2584
|
-
*/
|
|
2585
|
-
matchPatternToken(tokens, patternToken, captured) {
|
|
2586
|
-
switch (patternToken.type) {
|
|
2587
|
-
case "literal":
|
|
2588
|
-
return this.matchLiteralToken(tokens, patternToken);
|
|
2589
|
-
case "role":
|
|
2590
|
-
return readRoleValue(tokens, patternToken, captured, this.currentProfile);
|
|
2591
|
-
case "group":
|
|
2592
|
-
return this.matchGroupToken(tokens, patternToken, captured);
|
|
2593
|
-
default:
|
|
2594
|
-
return false;
|
|
2595
|
-
}
|
|
2596
|
-
}
|
|
2597
|
-
/**
|
|
2598
|
-
* Match a literal pattern token (keyword or particle).
|
|
2599
|
-
*/
|
|
2600
|
-
matchLiteralToken(tokens, patternToken) {
|
|
2601
|
-
const token = tokens.peek();
|
|
2602
|
-
this.logger.debug(" >>> matchLiteralToken: expecting", patternToken.value);
|
|
2603
|
-
this.logger.debug(
|
|
2604
|
-
" >>> matchLiteralToken: got token",
|
|
2605
|
-
token ? JSON.stringify({
|
|
2606
|
-
type: token.type,
|
|
2607
|
-
value: token.value,
|
|
2608
|
-
kind: token.kind
|
|
2609
|
-
}) : "null"
|
|
2610
|
-
);
|
|
2611
|
-
if (!token) {
|
|
2612
|
-
this.logger.debug(" >>> matchLiteralToken: FAIL - no token");
|
|
2613
|
-
return false;
|
|
2614
|
-
}
|
|
2615
|
-
const matchType = this.getMatchType(token, patternToken.value);
|
|
2616
|
-
this.logger.debug(
|
|
2617
|
-
" >>> matchType for",
|
|
2618
|
-
token.value,
|
|
2619
|
-
"vs",
|
|
2620
|
-
patternToken.value,
|
|
2621
|
-
":",
|
|
2622
|
-
matchType
|
|
2623
|
-
);
|
|
2624
|
-
if (matchType !== "none") {
|
|
2625
|
-
this.totalKeywordMatches++;
|
|
2626
|
-
if (matchType === "stem") {
|
|
2627
|
-
this.stemMatchCount++;
|
|
2628
|
-
}
|
|
2629
|
-
tokens.advance();
|
|
2630
|
-
return true;
|
|
2631
|
-
}
|
|
2632
|
-
if (patternToken.alternatives) {
|
|
2633
|
-
for (const alt of patternToken.alternatives) {
|
|
2634
|
-
const altMatchType = this.getMatchType(token, alt);
|
|
2635
|
-
if (altMatchType !== "none") {
|
|
2636
|
-
this.totalKeywordMatches++;
|
|
2637
|
-
if (altMatchType === "stem") {
|
|
2638
|
-
this.stemMatchCount++;
|
|
2639
|
-
}
|
|
2640
|
-
tokens.advance();
|
|
2641
|
-
return true;
|
|
2642
|
-
}
|
|
2643
|
-
}
|
|
2644
|
-
}
|
|
2645
|
-
return false;
|
|
2646
|
-
}
|
|
2647
|
-
/**
|
|
2648
|
-
* Match a group pattern token (optional sequence).
|
|
2649
|
-
* When the group's leading marker isn't at the current position, scans ahead
|
|
2650
|
-
* up to MAX_MARKER_SCAN tokens to find it. This allows unmarked tokens
|
|
2651
|
-
* (e.g., a value like 'hello') to sit between groups without blocking later
|
|
2652
|
-
* marker matches.
|
|
2653
|
-
*/
|
|
2654
|
-
matchGroupToken(tokens, patternToken, captured, parentStopMarkers) {
|
|
2655
|
-
const mark = tokens.mark();
|
|
2656
|
-
const capturedBefore = new Set(captured.keys());
|
|
2657
|
-
const success = this.matchTokenSequence(
|
|
2658
|
-
tokens,
|
|
2659
|
-
patternToken.tokens,
|
|
2660
|
-
captured,
|
|
2661
|
-
parentStopMarkers
|
|
2662
|
-
);
|
|
2663
|
-
if (success) return true;
|
|
2664
|
-
tokens.reset(mark);
|
|
2665
|
-
for (const role of captured.keys()) {
|
|
2666
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2667
|
-
}
|
|
2668
|
-
if (!patternToken.optional) return false;
|
|
2669
|
-
const leadingMarker = this.getGroupLeadingMarker(patternToken);
|
|
2670
|
-
if (leadingMarker) {
|
|
2671
|
-
for (let offset = 1; offset <= _PatternMatcher.MAX_MARKER_SCAN; offset++) {
|
|
2672
|
-
const ahead = tokens.peek(offset);
|
|
2673
|
-
if (!ahead) break;
|
|
2674
|
-
const aheadValue = (ahead.normalized || ahead.value).toLowerCase();
|
|
2675
|
-
if (aheadValue === leadingMarker) {
|
|
2676
|
-
this.logger.debug(
|
|
2677
|
-
" >> MARKER SCAN: found",
|
|
2678
|
-
leadingMarker,
|
|
2679
|
-
"at offset",
|
|
2680
|
-
offset,
|
|
2681
|
-
"- skipping intervening tokens"
|
|
2682
|
-
);
|
|
2683
|
-
for (let s = 0; s < offset; s++) tokens.advance();
|
|
2684
|
-
const retrySuccess = this.matchTokenSequence(
|
|
2685
|
-
tokens,
|
|
2686
|
-
patternToken.tokens,
|
|
2687
|
-
captured,
|
|
2688
|
-
parentStopMarkers
|
|
2689
|
-
);
|
|
2690
|
-
if (retrySuccess) return true;
|
|
2691
|
-
tokens.reset(mark);
|
|
2692
|
-
for (const role of captured.keys()) {
|
|
2693
|
-
if (!capturedBefore.has(role)) captured.delete(role);
|
|
2694
|
-
}
|
|
2695
|
-
break;
|
|
2696
|
-
}
|
|
2697
|
-
}
|
|
2698
|
-
}
|
|
2699
|
-
return true;
|
|
2700
|
-
}
|
|
2701
|
-
/**
|
|
2702
|
-
* Get the leading marker literal from an optional group.
|
|
2703
|
-
* Returns the lowercase marker value, or null if the group
|
|
2704
|
-
* doesn't start with a literal token (e.g., SOV groups where
|
|
2705
|
-
* the role comes before the marker).
|
|
2706
|
-
*/
|
|
2707
|
-
getGroupLeadingMarker(group) {
|
|
2708
|
-
const first = group.tokens[0];
|
|
2709
|
-
if (first?.type === "literal") return first.value.toLowerCase();
|
|
2710
|
-
return null;
|
|
2711
|
-
}
|
|
2712
|
-
/**
|
|
2713
|
-
* Get the type of match for a token against a value.
|
|
2714
|
-
* Used for confidence calculation.
|
|
2715
|
-
*/
|
|
2716
|
-
getMatchType(token, value) {
|
|
2717
|
-
if (token.value === value) return "exact";
|
|
2718
|
-
if (token.normalized === value) return "normalized";
|
|
2719
|
-
if (token.stem === value && token.stemConfidence !== void 0 && token.stemConfidence >= 0.7) {
|
|
2720
|
-
return "stem";
|
|
2721
|
-
}
|
|
2722
|
-
if (token.kind === "keyword" && safeToLowerCase(token.value) === value.toLowerCase()) {
|
|
2723
|
-
return "case-insensitive";
|
|
2724
|
-
}
|
|
2725
|
-
return "none";
|
|
2726
|
-
}
|
|
2727
|
-
/**
|
|
2728
|
-
* Collect literal values from upcoming pattern tokens that act as stop markers
|
|
2729
|
-
* for greedy role capture. Returns the set of lowercase values.
|
|
2730
|
-
*/
|
|
2731
|
-
collectStopMarkers(patternTokens, startIndex) {
|
|
2732
|
-
const markers = /* @__PURE__ */ new Set();
|
|
2733
|
-
for (let j = startIndex; j < patternTokens.length; j++) {
|
|
2734
|
-
const pt = patternTokens[j];
|
|
2735
|
-
if (pt.type === "literal") {
|
|
2736
|
-
markers.add(pt.value.toLowerCase());
|
|
2737
|
-
if (pt.alternatives) {
|
|
2738
|
-
for (const alt of pt.alternatives) {
|
|
2739
|
-
markers.add(alt.toLowerCase());
|
|
2740
|
-
}
|
|
2741
|
-
}
|
|
2742
|
-
break;
|
|
2743
|
-
}
|
|
2744
|
-
if (pt.type === "group") {
|
|
2745
|
-
for (const gt of pt.tokens) {
|
|
2746
|
-
if (gt.type === "literal") {
|
|
2747
|
-
markers.add(gt.value.toLowerCase());
|
|
2748
|
-
if (gt.alternatives) {
|
|
2749
|
-
for (const alt of gt.alternatives) {
|
|
2750
|
-
markers.add(alt.toLowerCase());
|
|
2751
|
-
}
|
|
2752
|
-
}
|
|
2753
|
-
break;
|
|
2754
|
-
}
|
|
2755
|
-
}
|
|
2756
|
-
if (!pt.optional) break;
|
|
2757
|
-
}
|
|
2758
|
-
}
|
|
2759
|
-
return markers;
|
|
2760
|
-
}
|
|
2761
|
-
/**
|
|
2762
|
-
* Check if a token matches any stop marker for greedy capture.
|
|
2763
|
-
*/
|
|
2764
|
-
isStopMarker(token, stopMarkers) {
|
|
2765
|
-
if (stopMarkers.size === 0) return false;
|
|
2766
|
-
if (stopMarkers.has(token.value.toLowerCase())) return true;
|
|
2767
|
-
if (token.normalized && stopMarkers.has(token.normalized.toLowerCase())) return true;
|
|
2768
|
-
return false;
|
|
2769
|
-
}
|
|
2770
|
-
// ==========================================================================
|
|
2771
|
-
// Depth Limits for Expression Parsing (security hardening)
|
|
2772
|
-
// ==========================================================================
|
|
2773
|
-
/**
|
|
2774
|
-
* Apply extraction rules to fill in static values and defaults for missing roles.
|
|
2775
|
-
*/
|
|
2776
|
-
applyExtractionRules(pattern, captured) {
|
|
2777
|
-
for (const [role, rule] of Object.entries(pattern.extraction)) {
|
|
2778
|
-
if (!captured.has(role)) {
|
|
2779
|
-
if (rule.value !== void 0) {
|
|
2780
|
-
captured.set(role, { type: "literal", value: rule.value });
|
|
2781
|
-
} else if (rule.default) {
|
|
2782
|
-
captured.set(role, rule.default);
|
|
2783
|
-
}
|
|
2784
|
-
}
|
|
2785
|
-
}
|
|
2786
|
-
}
|
|
2787
|
-
/**
|
|
2788
|
-
* Check if a pattern token is optional.
|
|
2789
|
-
*/
|
|
2790
|
-
isOptional(patternToken) {
|
|
2791
|
-
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
2792
|
-
}
|
|
2793
|
-
// ===========================================================================
|
|
2794
|
-
// English Idiom Support - Noise Word Handling
|
|
2795
|
-
// ===========================================================================
|
|
2796
|
-
/**
|
|
2797
|
-
* Extract event modifiers from the token stream.
|
|
2798
|
-
* Event modifiers are .once, .debounce(N), .throttle(N), .queue(strategy)
|
|
2799
|
-
* that can appear after event names.
|
|
2800
|
-
*
|
|
2801
|
-
* Returns EventModifiers object or undefined if no modifiers found.
|
|
2802
|
-
*/
|
|
2803
|
-
extractEventModifiers(tokens) {
|
|
2804
|
-
const modifiers = {};
|
|
2805
|
-
let foundModifier = false;
|
|
2806
|
-
while (!tokens.isAtEnd()) {
|
|
2807
|
-
const token = tokens.peek();
|
|
2808
|
-
if (!token || token.kind !== "event-modifier") {
|
|
2809
|
-
break;
|
|
2810
|
-
}
|
|
2811
|
-
const metadata = token.metadata;
|
|
2812
|
-
if (!metadata) {
|
|
2813
|
-
break;
|
|
2814
|
-
}
|
|
2815
|
-
foundModifier = true;
|
|
2816
|
-
switch (metadata.modifierName) {
|
|
2817
|
-
case "once":
|
|
2818
|
-
modifiers.once = true;
|
|
2819
|
-
break;
|
|
2820
|
-
case "debounce":
|
|
2821
|
-
if (typeof metadata.value === "number") {
|
|
2822
|
-
modifiers.debounce = metadata.value;
|
|
2823
|
-
}
|
|
2824
|
-
break;
|
|
2825
|
-
case "throttle":
|
|
2826
|
-
if (typeof metadata.value === "number") {
|
|
2827
|
-
modifiers.throttle = metadata.value;
|
|
2828
|
-
}
|
|
2829
|
-
break;
|
|
2830
|
-
case "queue":
|
|
2831
|
-
if (metadata.value === "first" || metadata.value === "last" || metadata.value === "all" || metadata.value === "none") {
|
|
2832
|
-
modifiers.queue = metadata.value;
|
|
2833
|
-
}
|
|
2834
|
-
break;
|
|
2835
|
-
}
|
|
2836
|
-
tokens.advance();
|
|
2837
|
-
}
|
|
2838
|
-
return foundModifier ? modifiers : void 0;
|
|
2839
|
-
}
|
|
2840
|
-
};
|
|
2841
|
-
/** Maximum tokens to scan ahead when looking for a group's leading marker */
|
|
2842
|
-
_PatternMatcher.MAX_MARKER_SCAN = 3;
|
|
2843
|
-
var PatternMatcher = _PatternMatcher;
|
|
2844
|
-
var patternMatcher = new PatternMatcher();
|
|
2845
|
-
function matchPattern(tokens, pattern) {
|
|
2846
|
-
return patternMatcher.matchPattern(tokens, pattern);
|
|
2847
|
-
}
|
|
2848
|
-
function matchBest(tokens, patterns) {
|
|
2849
|
-
return patternMatcher.matchBest(tokens, patterns);
|
|
2850
|
-
}
|
|
2851
|
-
|
|
2852
|
-
// src/core/matching/match-skeleton.ts
|
|
2853
|
-
function groupHasRole(group) {
|
|
2854
|
-
return group.tokens.some((t) => t.type === "role" || t.type === "group" && groupHasRole(t));
|
|
2855
|
-
}
|
|
2856
|
-
var MatchSkeleton = class {
|
|
2857
|
-
/**
|
|
2858
|
-
* @param makeReader - builds the grammar's reader, handed this matcher's view
|
|
2859
|
-
* of the match in progress (one reader per matcher).
|
|
2860
|
-
*/
|
|
2861
|
-
constructor(makeReader, confidenceModel) {
|
|
2862
|
-
/**
|
|
2863
|
-
* The pattern tokens still to match after the one being matched, through
|
|
2864
|
-
* every enclosing group: a value stops at any marker the pattern still owes.
|
|
2865
|
-
*/
|
|
2866
|
-
this.patternRest = [];
|
|
2867
|
-
/**
|
|
2868
|
-
* Whether the role slot being matched directly follows a literal the pattern
|
|
2869
|
-
* matched: its marker (`[por {quantity}]`), or the command's verb.
|
|
2870
|
-
*/
|
|
2871
|
-
this.slotAfterLiteral = false;
|
|
2872
|
-
/**
|
|
2873
|
-
* Selective memoization cache (Phase 6.1).
|
|
2874
|
-
* Caches pattern match results keyed by `tokenPosition:patternId`.
|
|
2875
|
-
* Cleared before each top-level matchBest() call.
|
|
2876
|
-
*/
|
|
2877
|
-
this.matchCache = /* @__PURE__ */ new Map();
|
|
2878
|
-
/**
|
|
2879
|
-
* Stream index where each role's capture began, for the current
|
|
2880
|
-
* `matchPattern` call. Rebuilt per call, written on the same success path as
|
|
2881
|
-
* `captured` — see `PatternMatchResult.roleStarts`.
|
|
2882
|
-
*/
|
|
2883
|
-
this.roleStarts = /* @__PURE__ */ new Map();
|
|
2884
|
-
/**
|
|
2885
|
-
* Track stem matches for confidence calculation.
|
|
2886
|
-
* This is set during matching and read during confidence calculation.
|
|
2887
|
-
*/
|
|
2888
|
-
this.stemMatchCount = 0;
|
|
2889
|
-
this.totalKeywordMatches = 0;
|
|
2890
|
-
this.confidenceModel = confidenceModel;
|
|
2891
|
-
this.reader = makeReader({
|
|
2892
|
-
patternRest: () => this.patternRest,
|
|
2893
|
-
slotAfterLiteral: () => this.slotAfterLiteral,
|
|
2894
|
-
wouldMatch: (pt, token) => this.patternTokenWouldMatch(pt, token)
|
|
2895
|
-
});
|
|
2896
|
-
}
|
|
2897
|
-
/**
|
|
2898
|
-
* Try to match a single pattern against the token stream.
|
|
2899
|
-
* Returns the match result or null if no match.
|
|
2900
|
-
*/
|
|
2901
|
-
matchPattern(tokens, pattern) {
|
|
2902
|
-
const mark = tokens.mark();
|
|
2903
|
-
const captured = /* @__PURE__ */ new Map();
|
|
2904
|
-
this.reader.beginPattern(pattern);
|
|
2905
|
-
this.stemMatchCount = 0;
|
|
2906
|
-
this.totalKeywordMatches = 0;
|
|
2907
|
-
this.roleStarts = /* @__PURE__ */ new Map();
|
|
2908
|
-
this.patternRest = [];
|
|
2909
|
-
const success = this.matchTokenSequence(tokens, pattern.template.tokens, captured);
|
|
2910
|
-
if (!success) {
|
|
2911
|
-
tokens.reset(mark);
|
|
2912
|
-
return null;
|
|
2913
|
-
}
|
|
2914
|
-
const confidence = this.confidenceModel.calculate({
|
|
2915
|
-
pattern,
|
|
2916
|
-
captured,
|
|
2917
|
-
stemMatchCount: this.stemMatchCount,
|
|
2918
|
-
totalKeywordMatches: this.totalKeywordMatches
|
|
2919
|
-
});
|
|
2920
|
-
this.applyExtractionRules(pattern, captured);
|
|
2921
|
-
return {
|
|
2922
|
-
pattern,
|
|
2923
|
-
captured,
|
|
2924
|
-
consumedTokens: tokens.position() - mark.position,
|
|
2925
|
-
confidence,
|
|
2926
|
-
roleStarts: this.roleStarts,
|
|
2927
|
-
...this.reader.resultFields?.()
|
|
2928
|
-
};
|
|
2929
|
-
}
|
|
2930
|
-
/**
|
|
2931
|
-
* Try to match multiple patterns, return the best match.
|
|
2932
|
-
*/
|
|
2933
|
-
matchBest(tokens, patterns) {
|
|
2934
|
-
this.matchCache.clear();
|
|
2935
|
-
const matches = [];
|
|
2936
|
-
const startPos = tokens.position();
|
|
2937
|
-
for (const pattern of patterns) {
|
|
2938
|
-
const mark = tokens.mark();
|
|
2939
|
-
const cacheKey = `${startPos}:${pattern.id}`;
|
|
2940
|
-
let result;
|
|
2941
|
-
if (this.matchCache.has(cacheKey)) {
|
|
2942
|
-
result = this.matchCache.get(cacheKey);
|
|
2943
|
-
} else {
|
|
2944
|
-
result = this.matchPattern(tokens, pattern);
|
|
2945
|
-
this.matchCache.set(cacheKey, result);
|
|
2946
|
-
}
|
|
2947
|
-
if (result) {
|
|
2948
|
-
matches.push(result);
|
|
2949
|
-
}
|
|
2950
|
-
tokens.reset(mark);
|
|
2951
|
-
}
|
|
2952
|
-
if (matches.length === 0) {
|
|
2953
|
-
return null;
|
|
2954
|
-
}
|
|
2955
|
-
matches.sort((a, b) => {
|
|
2956
|
-
const priorityDiff = b.pattern.priority - a.pattern.priority;
|
|
2957
|
-
if (priorityDiff !== 0) return priorityDiff;
|
|
2958
|
-
return b.confidence - a.confidence;
|
|
2959
|
-
});
|
|
2960
|
-
const best = matches[0];
|
|
2961
|
-
this.matchPattern(tokens, best.pattern);
|
|
2962
|
-
return best;
|
|
2963
|
-
}
|
|
2964
|
-
/**
|
|
2965
|
-
* Match a sequence of pattern tokens against the token stream.
|
|
2966
|
-
*/
|
|
2967
|
-
matchTokenSequence(tokens, patternTokens, captured, nextAfterSequence) {
|
|
2968
|
-
this.reader.skipLeading?.(tokens, patternTokens);
|
|
2969
|
-
for (let i = 0; i < patternTokens.length; i++) {
|
|
2970
|
-
const patternToken = patternTokens[i];
|
|
2971
|
-
this.reader.beforePatternToken?.(tokens, patternTokens, i, captured);
|
|
2972
|
-
if (this.reader.maySkipSlot(
|
|
2973
|
-
patternToken,
|
|
2974
|
-
tokens.peek(),
|
|
2975
|
-
patternTokens[i + 1] ?? nextAfterSequence
|
|
2976
|
-
)) {
|
|
2977
|
-
const skipMark = tokens.mark();
|
|
2978
|
-
const speculative = new Map(captured);
|
|
2979
|
-
if (this.matchTokenSequence(
|
|
2980
|
-
tokens,
|
|
2981
|
-
patternTokens.slice(i + 1),
|
|
2982
|
-
speculative,
|
|
2983
|
-
nextAfterSequence
|
|
2984
|
-
) && this.reader.atClauseEnd(tokens)) {
|
|
2985
|
-
captured.clear();
|
|
2986
|
-
for (const [role, value] of speculative) captured.set(role, value);
|
|
2987
|
-
return true;
|
|
2988
|
-
}
|
|
2989
|
-
tokens.reset(skipMark);
|
|
2990
|
-
}
|
|
2991
|
-
const outerRest = this.patternRest;
|
|
2992
|
-
this.patternRest = [...patternTokens.slice(i + 1), ...outerRest];
|
|
2993
|
-
this.slotAfterLiteral = patternToken.type === "role" && patternTokens[i - 1]?.type === "literal";
|
|
2994
|
-
const matched = this.matchPatternToken(
|
|
2995
|
-
tokens,
|
|
2996
|
-
patternToken,
|
|
2997
|
-
captured,
|
|
2998
|
-
patternTokens[i + 1] ?? nextAfterSequence
|
|
2999
|
-
);
|
|
3000
|
-
this.patternRest = outerRest;
|
|
3001
|
-
if (!matched) {
|
|
3002
|
-
if (this.isOptional(patternToken)) {
|
|
3003
|
-
continue;
|
|
3004
|
-
}
|
|
3005
|
-
return false;
|
|
3006
|
-
}
|
|
3007
|
-
}
|
|
3008
|
-
return true;
|
|
3009
|
-
}
|
|
3010
|
-
/**
|
|
3011
|
-
* Match a single pattern token against the current position in the stream.
|
|
3012
|
-
*/
|
|
3013
|
-
matchPatternToken(tokens, patternToken, captured, nextPatternToken) {
|
|
3014
|
-
switch (patternToken.type) {
|
|
3015
|
-
case "literal":
|
|
3016
|
-
return this.matchLiteralToken(tokens, patternToken);
|
|
3017
|
-
case "role": {
|
|
3018
|
-
const roleStart = tokens.position();
|
|
3019
|
-
const matchedRole = this.reader.readRole(tokens, patternToken, captured, nextPatternToken);
|
|
3020
|
-
if (matchedRole && captured.has(patternToken.role)) {
|
|
3021
|
-
this.roleStarts.set(patternToken.role, roleStart);
|
|
3022
|
-
}
|
|
3023
|
-
return matchedRole;
|
|
3024
|
-
}
|
|
3025
|
-
case "group":
|
|
3026
|
-
return this.matchGroupToken(tokens, patternToken, captured, nextPatternToken);
|
|
3027
|
-
default:
|
|
3028
|
-
return false;
|
|
3029
|
-
}
|
|
3030
|
-
}
|
|
3031
|
-
/**
|
|
3032
|
-
* Match a literal pattern token (keyword or particle).
|
|
3033
|
-
*/
|
|
3034
|
-
matchLiteralToken(tokens, patternToken) {
|
|
3035
|
-
const token = tokens.peek();
|
|
3036
|
-
if (!token) return false;
|
|
3037
|
-
const matchType = this.getMatchType(token, patternToken.value);
|
|
3038
|
-
if (matchType !== "none") {
|
|
3039
|
-
this.totalKeywordMatches++;
|
|
3040
|
-
if (matchType === "stem") {
|
|
3041
|
-
this.stemMatchCount++;
|
|
3042
|
-
}
|
|
3043
|
-
tokens.advance();
|
|
3044
|
-
return true;
|
|
3045
|
-
}
|
|
3046
|
-
if (patternToken.alternatives) {
|
|
3047
|
-
for (const alt of patternToken.alternatives) {
|
|
3048
|
-
const altMatchType = this.getMatchType(token, alt);
|
|
3049
|
-
if (altMatchType !== "none") {
|
|
3050
|
-
this.totalKeywordMatches++;
|
|
3051
|
-
if (altMatchType === "stem") {
|
|
3052
|
-
this.stemMatchCount++;
|
|
3053
|
-
}
|
|
3054
|
-
tokens.advance();
|
|
3055
|
-
return true;
|
|
3056
|
-
}
|
|
3057
|
-
}
|
|
3058
|
-
}
|
|
3059
|
-
for (const value of [patternToken.value, ...patternToken.alternatives ?? []]) {
|
|
3060
|
-
for (const sep of ["-", "_"]) {
|
|
3061
|
-
if (value.includes(sep) && this.tryMatchShatteredCompound(tokens, value, sep)) {
|
|
3062
|
-
this.totalKeywordMatches++;
|
|
3063
|
-
return true;
|
|
3064
|
-
}
|
|
3065
|
-
}
|
|
3066
|
-
}
|
|
3067
|
-
return false;
|
|
3068
|
-
}
|
|
3069
|
-
/**
|
|
3070
|
-
* Match a compound keyword (`titik-henti`, `по_умолчанию`) against a
|
|
3071
|
-
* shattered token run (`titik` `-` `henti` / `по` `_` `умолчанию`): word
|
|
3072
|
-
* segments must be identifier/keyword/particle tokens equal to the expected
|
|
3073
|
-
* segments (case-insensitive; particle because prepositions like ru `по` /
|
|
3074
|
-
* uk `за` lead some underscore compounds), separators lone `sep` tokens.
|
|
3075
|
-
* Consumes the run on success, resets the stream on failure.
|
|
3076
|
-
*/
|
|
3077
|
-
tryMatchShatteredCompound(tokens, expected, sep) {
|
|
3078
|
-
const segments = expected.split(sep);
|
|
3079
|
-
if (segments.length < 2 || segments.some((s) => s.length === 0)) return false;
|
|
3080
|
-
const mark = tokens.mark();
|
|
3081
|
-
for (let i = 0; i < segments.length; i++) {
|
|
3082
|
-
if (i > 0) {
|
|
3083
|
-
const sepToken = tokens.peek();
|
|
3084
|
-
if (!sepToken || sepToken.value !== sep) {
|
|
3085
|
-
tokens.reset(mark);
|
|
3086
|
-
return false;
|
|
3087
|
-
}
|
|
3088
|
-
tokens.advance();
|
|
3089
|
-
}
|
|
3090
|
-
const word = tokens.peek();
|
|
3091
|
-
if (!word || word.kind !== "identifier" && word.kind !== "keyword" && word.kind !== "particle" || word.value.toLowerCase() !== segments[i].toLowerCase()) {
|
|
3092
|
-
tokens.reset(mark);
|
|
3093
|
-
return false;
|
|
3094
|
-
}
|
|
3095
|
-
tokens.advance();
|
|
3096
|
-
}
|
|
3097
|
-
return true;
|
|
3098
|
-
}
|
|
3099
|
-
/**
|
|
3100
|
-
* Match a group pattern token (optional sequence).
|
|
3101
|
-
*/
|
|
3102
|
-
matchGroupToken(tokens, patternToken, captured, nextPatternToken) {
|
|
3103
|
-
const mark = tokens.mark();
|
|
3104
|
-
const capturedBefore = new Map(captured);
|
|
3105
|
-
let success = this.matchTokenSequence(tokens, patternToken.tokens, captured, nextPatternToken);
|
|
3106
|
-
if (success && tokens.position() > mark.position && groupHasRole(patternToken) && [...captured].every(([role, value]) => capturedBefore.get(role) === value) && captured.size === capturedBefore.size) {
|
|
3107
|
-
success = false;
|
|
3108
|
-
}
|
|
3109
|
-
if (success) {
|
|
3110
|
-
const markerLiterals = [];
|
|
3111
|
-
for (const inner of patternToken.tokens) {
|
|
3112
|
-
if (inner.type === "group" && inner.renderRequired) {
|
|
3113
|
-
for (const t of inner.tokens) {
|
|
3114
|
-
if (t.type === "literal") markerLiterals.push(t.value, ...t.alternatives ?? []);
|
|
3115
|
-
}
|
|
3116
|
-
}
|
|
3117
|
-
}
|
|
3118
|
-
if (markerLiterals.length > 0) {
|
|
3119
|
-
const addedRole = [...captured.keys()].some((role) => !capturedBefore.has(role));
|
|
3120
|
-
if (addedRole) {
|
|
3121
|
-
const last = tokens.tokens[tokens.position() - 1];
|
|
3122
|
-
const markerSeen = last !== void 0 && tokens.position() > mark.position && markerLiterals.some((lit) => this.getMatchType(last, lit) !== "none");
|
|
3123
|
-
if (!markerSeen) success = false;
|
|
3124
|
-
}
|
|
3125
|
-
}
|
|
3126
|
-
}
|
|
3127
|
-
if (!success) {
|
|
3128
|
-
tokens.reset(mark);
|
|
3129
|
-
for (const role of captured.keys()) {
|
|
3130
|
-
if (!capturedBefore.has(role)) {
|
|
3131
|
-
captured.delete(role);
|
|
3132
|
-
}
|
|
2444
|
+
if (typeof token.value === "string" && token.value.startsWith(":")) {
|
|
2445
|
+
return createReference(token.value);
|
|
3133
2446
|
}
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
}
|
|
2447
|
+
const identLower = safeToLowerCase(token.value);
|
|
2448
|
+
if (isValidReference(identLower)) {
|
|
2449
|
+
return createReference(identLower);
|
|
3138
2450
|
}
|
|
3139
|
-
return
|
|
2451
|
+
return { type: "expression", raw: token.value };
|
|
3140
2452
|
}
|
|
3141
|
-
|
|
2453
|
+
case "url":
|
|
2454
|
+
return createLiteral(token.value, "string");
|
|
2455
|
+
default:
|
|
2456
|
+
return null;
|
|
3142
2457
|
}
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
patternTokenWouldMatch(pt, token) {
|
|
3149
|
-
if (!pt) return false;
|
|
3150
|
-
if (pt.type === "literal") {
|
|
3151
|
-
if (this.getMatchType(token, pt.value) !== "none") return true;
|
|
3152
|
-
return !!pt.alternatives?.some((a) => this.getMatchType(token, a) !== "none");
|
|
3153
|
-
}
|
|
3154
|
-
if (pt.type === "group") {
|
|
3155
|
-
return this.patternTokenWouldMatch(pt.tokens?.[0], token);
|
|
3156
|
-
}
|
|
3157
|
-
return false;
|
|
2458
|
+
}
|
|
2459
|
+
function parseLiteralValue(value) {
|
|
2460
|
+
if (value.startsWith('"') || value.startsWith("'") || value.startsWith("`") || value.startsWith("\u300C")) {
|
|
2461
|
+
const inner = value.slice(1, -1);
|
|
2462
|
+
return createLiteral(inner, "string");
|
|
3158
2463
|
}
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
if (
|
|
3166
|
-
|
|
3167
|
-
return "stem";
|
|
3168
|
-
}
|
|
3169
|
-
if (token.kind === "keyword" && token.value.toLowerCase() === value.toLowerCase()) {
|
|
3170
|
-
return "case-insensitive";
|
|
3171
|
-
}
|
|
3172
|
-
if (token.kind === "keyword") {
|
|
3173
|
-
const strippedToken = stripOptionalDiacritics(token.value).toLowerCase();
|
|
3174
|
-
if (strippedToken && strippedToken === stripOptionalDiacritics(value).toLowerCase()) {
|
|
3175
|
-
return "case-insensitive";
|
|
3176
|
-
}
|
|
2464
|
+
if (value === "true") return createLiteral(true, "boolean");
|
|
2465
|
+
if (value === "false") return createLiteral(false, "boolean");
|
|
2466
|
+
const durationMatch = value.match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/);
|
|
2467
|
+
if (durationMatch) {
|
|
2468
|
+
const num2 = parseFloat(durationMatch[1]);
|
|
2469
|
+
const unit = durationMatch[2];
|
|
2470
|
+
if (unit) {
|
|
2471
|
+
return createLiteral(value, "duration");
|
|
3177
2472
|
}
|
|
3178
|
-
return "
|
|
2473
|
+
return createLiteral(num2, "number");
|
|
3179
2474
|
}
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
role,
|
|
3198
|
-
rule.valueIsExpression ? { type: "expression", raw: rule.value } : { type: "literal", value: rule.value }
|
|
3199
|
-
);
|
|
3200
|
-
} else if (rule.default) {
|
|
3201
|
-
const implicitDefault = {
|
|
3202
|
-
...rule.default,
|
|
3203
|
-
implicit: true
|
|
3204
|
-
};
|
|
3205
|
-
captured.set(role, implicitDefault);
|
|
3206
|
-
}
|
|
2475
|
+
const num = parseFloat(value);
|
|
2476
|
+
if (!isNaN(num)) {
|
|
2477
|
+
return createLiteral(num, "number");
|
|
2478
|
+
}
|
|
2479
|
+
return createLiteral(value, "string");
|
|
2480
|
+
}
|
|
2481
|
+
var ENGLISH_NOISE_WORDS = /* @__PURE__ */ new Set(["the", "a", "an"]);
|
|
2482
|
+
function skipNoiseWords(tokens) {
|
|
2483
|
+
const token = tokens.peek();
|
|
2484
|
+
if (!token) return;
|
|
2485
|
+
const tokenLower = safeToLowerCase(token.value);
|
|
2486
|
+
if (ENGLISH_NOISE_WORDS.has(tokenLower)) {
|
|
2487
|
+
const mark = tokens.mark();
|
|
2488
|
+
tokens.advance();
|
|
2489
|
+
const nextToken = tokens.peek();
|
|
2490
|
+
if (nextToken && nextToken.kind === "selector") {
|
|
2491
|
+
return;
|
|
3207
2492
|
}
|
|
2493
|
+
tokens.reset(mark);
|
|
3208
2494
|
}
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
*/
|
|
3212
|
-
isOptional(patternToken) {
|
|
3213
|
-
return patternToken.type !== "literal" && patternToken.optional === true;
|
|
2495
|
+
if (tokenLower === "class") {
|
|
2496
|
+
tokens.advance();
|
|
3214
2497
|
}
|
|
3215
|
-
}
|
|
2498
|
+
}
|
|
3216
2499
|
|
|
3217
2500
|
// src/core/matching/stock-reader.ts
|
|
3218
2501
|
function greedyStopMarkers(rest) {
|
|
@@ -3313,6 +2596,144 @@ var StockValueReader = class {
|
|
|
3313
2596
|
function createStockReader(profiles = {}) {
|
|
3314
2597
|
return (view) => new StockValueReader(view, profiles);
|
|
3315
2598
|
}
|
|
2599
|
+
|
|
2600
|
+
// src/core/matching/stock-confidence.ts
|
|
2601
|
+
function scoreParts(ctx) {
|
|
2602
|
+
const { pattern, captured } = ctx;
|
|
2603
|
+
let score = 0;
|
|
2604
|
+
let maxScore = 0;
|
|
2605
|
+
const hasDefault = (role) => {
|
|
2606
|
+
return pattern.extraction?.[role]?.default !== void 0;
|
|
2607
|
+
};
|
|
2608
|
+
for (const token of pattern.template.tokens) {
|
|
2609
|
+
if (token.type === "role") {
|
|
2610
|
+
maxScore += 1;
|
|
2611
|
+
if (captured.has(token.role)) {
|
|
2612
|
+
score += 1;
|
|
2613
|
+
}
|
|
2614
|
+
} else if (token.type === "group") {
|
|
2615
|
+
for (const subToken of token.tokens) {
|
|
2616
|
+
if (subToken.type === "role") {
|
|
2617
|
+
const roleHasDefault = hasDefault(subToken.role);
|
|
2618
|
+
const weight = 0.8;
|
|
2619
|
+
maxScore += weight;
|
|
2620
|
+
if (captured.has(subToken.role)) {
|
|
2621
|
+
score += weight;
|
|
2622
|
+
} else if (roleHasDefault) {
|
|
2623
|
+
score += weight * 0.6;
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
const roleCoverage = maxScore > 0 ? score / maxScore : 1;
|
|
2630
|
+
let baseConfidence = roleCoverage;
|
|
2631
|
+
let stemPenalty = 0;
|
|
2632
|
+
if (ctx.stemMatchCount > 0 && ctx.totalKeywordMatches > 0) {
|
|
2633
|
+
stemPenalty = ctx.stemMatchCount / ctx.totalKeywordMatches * 0.15;
|
|
2634
|
+
baseConfidence = Math.max(0.5, baseConfidence - stemPenalty);
|
|
2635
|
+
}
|
|
2636
|
+
const vsoBoost = calculateVSOConfidenceBoost(pattern);
|
|
2637
|
+
baseConfidence = Math.min(1, baseConfidence + vsoBoost);
|
|
2638
|
+
const prepositionAdjustment = arabicPrepositionDisambiguation(pattern, captured);
|
|
2639
|
+
baseConfidence = Math.max(0, Math.min(1, baseConfidence + prepositionAdjustment));
|
|
2640
|
+
return {
|
|
2641
|
+
roleCoverage,
|
|
2642
|
+
stemPenalty,
|
|
2643
|
+
languageBoost: vsoBoost,
|
|
2644
|
+
languageAdjustment: prepositionAdjustment,
|
|
2645
|
+
final: baseConfidence
|
|
2646
|
+
};
|
|
2647
|
+
}
|
|
2648
|
+
function calculateVSOConfidenceBoost(pattern) {
|
|
2649
|
+
if (pattern.language !== "ar") {
|
|
2650
|
+
return 0;
|
|
2651
|
+
}
|
|
2652
|
+
const firstToken = pattern.template.tokens[0];
|
|
2653
|
+
if (!firstToken || firstToken.type !== "literal") {
|
|
2654
|
+
return 0;
|
|
2655
|
+
}
|
|
2656
|
+
const ARABIC_VERBS = /* @__PURE__ */ new Set([
|
|
2657
|
+
"\u0628\u062F\u0644",
|
|
2658
|
+
"\u063A\u064A\u0631",
|
|
2659
|
+
"\u0623\u0636\u0641",
|
|
2660
|
+
"\u0623\u0632\u0644",
|
|
2661
|
+
"\u0636\u0639",
|
|
2662
|
+
"\u0627\u062C\u0639\u0644",
|
|
2663
|
+
"\u0639\u064A\u0646",
|
|
2664
|
+
"\u0632\u062F",
|
|
2665
|
+
"\u0627\u0646\u0642\u0635",
|
|
2666
|
+
"\u0633\u062C\u0644",
|
|
2667
|
+
"\u0623\u0638\u0647\u0631",
|
|
2668
|
+
"\u0623\u062E\u0641",
|
|
2669
|
+
"\u0634\u063A\u0644",
|
|
2670
|
+
"\u0623\u0631\u0633\u0644",
|
|
2671
|
+
"\u0631\u0643\u0632",
|
|
2672
|
+
"\u0634\u0648\u0634",
|
|
2673
|
+
"\u062A\u0648\u0642\u0641",
|
|
2674
|
+
"\u0627\u0646\u0633\u062E",
|
|
2675
|
+
"\u0627\u062D\u0630\u0641",
|
|
2676
|
+
"\u0627\u0635\u0646\u0639",
|
|
2677
|
+
"\u0627\u0646\u062A\u0638\u0631",
|
|
2678
|
+
"\u0627\u0646\u062A\u0642\u0627\u0644",
|
|
2679
|
+
"\u0623\u0648"
|
|
2680
|
+
]);
|
|
2681
|
+
if (ARABIC_VERBS.has(firstToken.value)) {
|
|
2682
|
+
return 0.15;
|
|
2683
|
+
}
|
|
2684
|
+
if (firstToken.alternatives) {
|
|
2685
|
+
for (const alt of firstToken.alternatives) {
|
|
2686
|
+
if (ARABIC_VERBS.has(alt)) {
|
|
2687
|
+
return 0.15;
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
return 0;
|
|
2692
|
+
}
|
|
2693
|
+
function arabicPrepositionDisambiguation(pattern, captured) {
|
|
2694
|
+
if (pattern.language !== "ar") {
|
|
2695
|
+
return 0;
|
|
2696
|
+
}
|
|
2697
|
+
let adjustment = 0;
|
|
2698
|
+
const PREFERRED_PREPOSITIONS = {
|
|
2699
|
+
patient: ["\u0639\u0644\u0649"],
|
|
2700
|
+
// element selectors prefer على (on/upon)
|
|
2701
|
+
destination: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2702
|
+
// destination prefers إلى (to)
|
|
2703
|
+
source: ["\u0645\u0646"],
|
|
2704
|
+
// source prefers من (from)
|
|
2705
|
+
agent: ["\u0645\u0646"],
|
|
2706
|
+
// agent/by prefers من (from/by)
|
|
2707
|
+
manner: ["\u0628"],
|
|
2708
|
+
// manner prefers ب (with/by)
|
|
2709
|
+
style: ["\u0628"],
|
|
2710
|
+
// style prefers ب (with)
|
|
2711
|
+
goal: ["\u0625\u0644\u0649", "\u0627\u0644\u0649"],
|
|
2712
|
+
// target state prefers إلى (to)
|
|
2713
|
+
method: ["\u0628"]
|
|
2714
|
+
// method prefers ب (with/by)
|
|
2715
|
+
};
|
|
2716
|
+
for (const [role, value] of captured.entries()) {
|
|
2717
|
+
const preferred = PREFERRED_PREPOSITIONS[role];
|
|
2718
|
+
if (!preferred || preferred.length === 0) {
|
|
2719
|
+
continue;
|
|
2720
|
+
}
|
|
2721
|
+
const metadata = "metadata" in value ? value.metadata : void 0;
|
|
2722
|
+
if (metadata && typeof metadata.prepositionValue === "string") {
|
|
2723
|
+
const usedPreposition = metadata.prepositionValue;
|
|
2724
|
+
if (preferred.includes(usedPreposition)) {
|
|
2725
|
+
adjustment += 0.1;
|
|
2726
|
+
} else {
|
|
2727
|
+
adjustment -= 0.1;
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
return Math.max(-0.1, Math.min(0.1, adjustment));
|
|
2732
|
+
}
|
|
2733
|
+
var stockConfidenceModel = {
|
|
2734
|
+
calculate: (ctx) => scoreParts(ctx).final,
|
|
2735
|
+
calculateWithBreakdown: (ctx) => scoreParts(ctx)
|
|
2736
|
+
};
|
|
3316
2737
|
export {
|
|
3317
2738
|
BaseMorphologicalNormalizer,
|
|
3318
2739
|
BaseTokenizer,
|
|
@@ -3320,7 +2741,6 @@ export {
|
|
|
3320
2741
|
DEFAULT_PUNCTUATION,
|
|
3321
2742
|
MatchSkeleton,
|
|
3322
2743
|
OperatorExtractor,
|
|
3323
|
-
PatternMatcher,
|
|
3324
2744
|
PunctuationExtractor,
|
|
3325
2745
|
StockValueReader,
|
|
3326
2746
|
TokenStreamImpl,
|
|
@@ -3374,11 +2794,8 @@ export {
|
|
|
3374
2794
|
isUrlStart,
|
|
3375
2795
|
isVariableRef,
|
|
3376
2796
|
isWhitespace,
|
|
3377
|
-
matchBest,
|
|
3378
|
-
matchPattern,
|
|
3379
2797
|
noChange,
|
|
3380
2798
|
normalized,
|
|
3381
|
-
patternMatcher,
|
|
3382
2799
|
readGreedy,
|
|
3383
2800
|
readRoleValue,
|
|
3384
2801
|
stockConfidenceModel,
|