Skip to content

Commit f1133fb

Browse files
committed
修复 commonAttributes -- 应该是交集不是并集
1 parent be4d2b5 commit f1133fb

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
修复 `commonAttributes` -- 应该是交集不是并集
1+
修复 `generateConcepts`
22

33
`concept-graph/` -- 从 `generateConcepts` 生成有向图
44

src/context/commonAttributes.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ test("commonAttributes", () => {
1919

2020
assert.deepStrictEqual(
2121
commonAttributes(context, ["earth", "mars", "jupiter"]),
22-
new Set([
23-
"size.small",
24-
"size.large",
25-
"distance-from-sun.near",
26-
"distance-from-sun.far",
27-
"moon.yes",
28-
]),
22+
new Set(["moon.yes"]),
2923
)
3024
})

src/context/commonAttributes.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
import type { Attribute, Context, Entity } from "./index.js"
1+
import { setIntersection } from "../utils/Set.js"
2+
import {
3+
attributesOf,
4+
type Attribute,
5+
type Context,
6+
type Entity,
7+
} from "./index.js"
28

39
export function commonAttributes(
410
context: Context,
511
inputEntities: ReadonlySet<Entity> | ReadonlyArray<Entity>,
612
): ReadonlySet<Attribute> {
7-
inputEntities = new Set(inputEntities)
13+
inputEntities = Array.from(inputEntities)
14+
if (inputEntities.length === 0) {
15+
return context.attributes
16+
}
817

9-
const resultAttributes = new Set<Attribute>()
10-
for (const inputEntity of inputEntities) {
11-
for (const attribute of context.entityAttributeIndex.get(inputEntity) || [])
12-
resultAttributes.add(attribute)
18+
const [firstEntity, ...restEntities] = inputEntities
19+
let resultAttributes = attributesOf(context, firstEntity)
20+
for (const entity of restEntities) {
21+
resultAttributes = setIntersection(
22+
resultAttributes,
23+
attributesOf(context, entity),
24+
)
1325
}
1426

1527
return resultAttributes

0 commit comments

Comments
 (0)