참고소스 수정본
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
"""The base EntityResolver class does not enforce
|
||||
a specific signature for the run method, which makes it very flexible.
|
||||
"""
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
import neo4j
|
||||
from neo4j_graphrag.experimental.components.resolver import EntityResolver
|
||||
from neo4j_graphrag.experimental.components.types import ResolutionStats
|
||||
|
||||
|
||||
class MyEntityResolver(EntityResolver):
|
||||
def __init__(
|
||||
self,
|
||||
driver: neo4j.Driver,
|
||||
filter_query: Optional[str] = None,
|
||||
) -> None:
|
||||
super().__init__(driver, filter_query)
|
||||
|
||||
async def run(self, *args: Any, **kwargs: Any) -> ResolutionStats:
|
||||
# logic here
|
||||
return ResolutionStats(
|
||||
number_of_nodes_to_resolve=0,
|
||||
number_of_created_nodes=0,
|
||||
)
|
||||
@@ -0,0 +1,36 @@
|
||||
"""The FuzzyMatchResolver merges nodes with same label
|
||||
and similar textual properties (by default using the "name" property) based on RapidFuzz
|
||||
for string matching.
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
from neo4j_graphrag.experimental.components.resolver import (
|
||||
FuzzyMatchResolver,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import ResolutionStats
|
||||
|
||||
import neo4j
|
||||
|
||||
|
||||
async def main(driver: neo4j.Driver) -> None:
|
||||
resolver = FuzzyMatchResolver(
|
||||
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
|
||||
# and the neo4j database where data is updated
|
||||
# neo4j_database="neo4j",
|
||||
)
|
||||
res: ResolutionStats = await resolver.run()
|
||||
print(res)
|
||||
@@ -0,0 +1,36 @@
|
||||
"""The SinglePropertyExactMatchResolver merge nodes with same label
|
||||
and exact same property value (by default using the "name" property).
|
||||
|
||||
If some nodes need to be excluded from the resolution, for instance nodes
|
||||
created from a previous run, 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 (
|
||||
SinglePropertyExactMatchResolver,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import ResolutionStats
|
||||
|
||||
|
||||
async def main(driver: neo4j.Driver) -> None:
|
||||
resolver = SinglePropertyExactMatchResolver(
|
||||
driver,
|
||||
# let's filter out some entities assuming the EntityToExclude label
|
||||
# was manually added to nodes in the db
|
||||
filter_query="WHERE NOT entity:EntityToExclude",
|
||||
# another example: in some cases, we do not want to merge
|
||||
# entities whose name is John Doe because we don't know if it
|
||||
# corresponds to the same real person
|
||||
# filter_query="WHERE entity.name <> 'John Doe'",
|
||||
# optionally, change the property used for resolution (default is "name")
|
||||
# resolve_property="name",
|
||||
# and the neo4j database where data is updated
|
||||
# neo4j_database="neo4j",
|
||||
)
|
||||
res: ResolutionStats = await resolver.run()
|
||||
print(res)
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user