@okforest/okf 0.11.0 → 0.12.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/dist/index.d.mts CHANGED
@@ -49,10 +49,17 @@ export declare function buildInventory(tree: Tree, options?: {
49
49
  * - case and punctuation: `SaaS Application`, `saas-application`;
50
50
  * - singular and plural: `Policy`, `Policies`;
51
51
  * - spelling: an edit distance of 1 (values of 5+ characters) or 2 (10+),
52
- * unless one value contains the other.
52
+ * two letters swapped counting as one (`Metirc`), unless one value
53
+ * contains the other, or their digits differ (`q1-goals`, `q2-goals`).
53
54
  */
54
55
  export declare function findNearDuplicates(counts: readonly Count[], field: NearDuplicates["field"]): NearDuplicates[];
55
- /** Levenshtein distance, stopping early once it exceeds `limit`. */
56
+ /**
57
+ * Edit distance: insertions, deletions, substitutions, and swaps of two
58
+ * neighbouring characters (`Metirc`, `Metric`), each one edit (optimal
59
+ * string alignment). Stops early once it exceeds `limit`: a row's smallest
60
+ * value is at most one more than the row before's, so once a row passes the
61
+ * limit, no later row, swaps included, comes back under it.
62
+ */
56
63
  export declare function editDistance(a: string, b: string, limit: number): number;
57
64
  /** Convert a glob (relative to the tree root) to a regular expression.
58
65
  * `*` and `?` stay within one path segment, `**` crosses segments, and a
package/dist/index.mjs CHANGED
@@ -84,7 +84,8 @@ function sorted(map) {
84
84
  * - case and punctuation: `SaaS Application`, `saas-application`;
85
85
  * - singular and plural: `Policy`, `Policies`;
86
86
  * - spelling: an edit distance of 1 (values of 5+ characters) or 2 (10+),
87
- * unless one value contains the other.
87
+ * two letters swapped counting as one (`Metirc`), unless one value
88
+ * contains the other, or their digits differ (`q1-goals`, `q2-goals`).
88
89
  */
89
90
  function findNearDuplicates(counts, field) {
90
91
  const parent = counts.map((_, i) => i);
@@ -112,7 +113,7 @@ function findNearDuplicates(counts, field) {
112
113
  if (a === "" || b === "") continue;
113
114
  if (a === b) union(i, j, "case-or-punctuation");
114
115
  else if (singular[i] === singular[j]) union(i, j, "plural");
115
- else if (!a.includes(b) && !b.includes(a)) {
116
+ else if (!a.includes(b) && !b.includes(a) && digits(a) === digits(b)) {
116
117
  const shortest = Math.min(a.length, b.length);
117
118
  const allowed = shortest >= 10 ? 2 : shortest >= 5 ? 1 : 0;
118
119
  if (allowed > 0 && editDistance(a, b, allowed) <= allowed) union(i, j, "spelling");
@@ -134,26 +135,36 @@ function findNearDuplicates(counts, field) {
134
135
  reasons: order.filter((r) => reasons.get(root)?.has(r))
135
136
  }));
136
137
  }
138
+ const digits = (value) => value.replace(/\D/g, "");
137
139
  function singularize(word) {
138
140
  if (word.endsWith("ies") && word.length > 4) return `${word.slice(0, -3)}y`;
139
141
  if (/(?:ses|xes|zes|ches|shes)$/.test(word)) return word.slice(0, -2);
140
142
  if (word.endsWith("s") && !word.endsWith("ss") && word.length > 3) return word.slice(0, -1);
141
143
  return word;
142
144
  }
143
- /** Levenshtein distance, stopping early once it exceeds `limit`. */
145
+ /**
146
+ * Edit distance: insertions, deletions, substitutions, and swaps of two
147
+ * neighbouring characters (`Metirc`, `Metric`), each one edit (optimal
148
+ * string alignment). Stops early once it exceeds `limit`: a row's smallest
149
+ * value is at most one more than the row before's, so once a row passes the
150
+ * limit, no later row, swaps included, comes back under it.
151
+ */
144
152
  function editDistance(a, b, limit) {
145
153
  if (Math.abs(a.length - b.length) > limit) return limit + 1;
154
+ let before = [];
146
155
  let previous = Array.from({ length: b.length + 1 }, (_, j) => j);
147
156
  for (let i = 1; i <= a.length; i++) {
148
157
  const current = [i];
149
158
  let rowMin = i;
150
159
  for (let j = 1; j <= b.length; j++) {
151
160
  const cost = a[i - 1] === b[j - 1] ? 0 : 1;
152
- const value = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
161
+ let value = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
162
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) value = Math.min(value, before[j - 2] + 1);
153
163
  current.push(value);
154
164
  rowMin = Math.min(rowMin, value);
155
165
  }
156
166
  if (rowMin > limit) return limit + 1;
167
+ before = previous;
157
168
  previous = current;
158
169
  }
159
170
  return previous[b.length];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okforest/okf",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Read an Open Knowledge Format (OKF) v0.2 tree into a model: concepts, frontmatter, links, backlinks, trust tiers, staleness, and a metadata inventory. No SDKs or frameworks; runs in Node, browsers, and Workers.",
5
5
  "keywords": [
6
6
  "okf",