-
Notifications
You must be signed in to change notification settings - Fork 103
Add SpaCy Semantic match resolver for KG Builder #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
NathalieCharbel
wants to merge
17
commits into
neo4j:main
from
NathalieCharbel:kg-builder-entity-resolution-spacy
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
18e6b7e
Add SpaCySemanticMatchResolver
NathalieCharbel f4a9bb5
Improve comments
NathalieCharbel cd99060
Correct Return of Cypher query
NathalieCharbel 997018e
Handle cases where spacy model is not yet downloaded
NathalieCharbel 13a89e6
Add unit tests
NathalieCharbel 9732246
Update CHANGELOG, docs, and examples
NathalieCharbel 088488f
Ruff
NathalieCharbel 5c86573
Fix poetry lock file
NathalieCharbel 7bb1aba
Fix required python version for spacy
NathalieCharbel e4dc3e3
Update supported Python range
NathalieCharbel 9b905dc
Fix mypy errors
NathalieCharbel 4f79f58
Ruff again
NathalieCharbel f55f8be
Remove marker for spacy in toml
NathalieCharbel 0adf071
Downgrade to an older spacy version
NathalieCharbel 4f4eb06
Add log message when spaCy model is downloaded
NathalieCharbel 3c0fffa
Remove unnecessary examples
NathalieCharbel f041b59
Fix formatting issue
NathalieCharbel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
examples/customize/build_graph/components/resolvers/simple_entity_resolver.py
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
examples/customize/build_graph/components/resolvers/spacy_entity_resolver_pre_filter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""The SpaCySemanticMatchResolver merges nodes with same label | ||
and similar textual properties (by default using the "name" property) based on spaCy | ||
embeddings and cosine similarities of embedding vectors. | ||
|
||
If the resolution is intended to be applied only on some nodes, for instance nodes that | ||
belong to a specific document, a "WHERE" query can be added. The only variable in the | ||
query scope is "entity". | ||
|
||
WARNING: this process is destructive, initial nodes are deleted and replaced | ||
by the resolved ones, but all relationships are kept. | ||
See apoc.refactor.mergeNodes documentation for more details. | ||
""" | ||
|
||
import neo4j | ||
from neo4j_graphrag.experimental.components.resolver import ( | ||
SpaCySemanticMatchResolver, | ||
) | ||
from neo4j_graphrag.experimental.components.types import ResolutionStats | ||
|
||
|
||
async def main(driver: neo4j.Driver) -> None: | ||
resolver = SpaCySemanticMatchResolver( | ||
driver, | ||
# let's filter all entities that belong to a certain docId | ||
filter_query="WHERE (entity)-[:FROM_CHUNK]->(:Chunk)-[:FROM_DOCUMENT]->(doc:" | ||
"Document {id = 'docId'}", | ||
# optionally, change the properties used for resolution (default is "name") | ||
# resolve_properties=["name", "ssn"], | ||
# the similarity threshold (default is 0.8) | ||
# similarity_threshold=0.9 | ||
# the spaCy trained model (default is "en_core_web_lg") | ||
# spacy_model="en_core_web_sm" | ||
# and the neo4j database where data is updated | ||
# neo4j_database="neo4j", | ||
) | ||
res: ResolutionStats = await resolver.run() | ||
print(res) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.