-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: implement rapidfuzz #3
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4c79bf
refactor: RapidFuzz implementation
rmfranken bf258d6
fix: make matching thresholds consistent
rmfranken 5d97898
fix: typo in URI
rmfranken f8bd6d3
fix: make use of fuzzy flag
rmfranken d604eb5
feat: add logging to see what is replaced
rmfranken 7a9a374
chore: cleanup
rmfranken 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,70 @@ | ||
| import pytest | ||
| from rdflib import Graph, URIRef, Literal, Namespace | ||
| from strings2things.app.core.rdf_transformer import RDFTransformer | ||
| from strings2things.app.core.transformation_log import TransformationLog | ||
|
|
||
| EX = Namespace("http://example.org/") | ||
| EX = Namespace("http://example.org/ontology#") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def label_map(): | ||
| # Ontology label map: canonical labels → IRIs | ||
| return { | ||
| "geology": "http://example.org/ontology#Geology", | ||
| "biology": "http://example.org/ontology#Biology", | ||
| "geology": str(EX.Geology), | ||
| "biology": str(EX.Biology), | ||
| "physics": str(EX.Physics), | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def input_graph(): | ||
| g = Graph() | ||
| g.add((EX.subj1, EX.hasCategory, Literal("Geology"))) | ||
| g.add((EX.subj2, EX.hasCategory, Literal("UnknownLabel"))) | ||
| g.add((EX.subj3, EX.hasValue, URIRef("http://example.org/someIRI"))) | ||
| # Exact match example | ||
| g.add((EX.subj1, EX.hasCategory, Literal("geology"))) | ||
|
|
||
| # Fuzzy match example (slightly misspelled) | ||
| g.add((EX.subj2, EX.hasCategory, Literal("biolgy"))) | ||
|
|
||
| # Unknown label (should remain unchanged) | ||
| g.add((EX.subj3, EX.hasCategory, Literal("unknownlabel"))) | ||
|
|
||
| # Non-literal value (should remain untouched) | ||
| g.add((EX.subj4, EX.hasValue, URIRef("http://example.org/someIRI"))) | ||
| return g | ||
|
|
||
|
|
||
| def test_rdf_transformer(label_map, input_graph): | ||
| transformer = RDFTransformer(label_map) | ||
| def test_rdf_transformer_combined(label_map, input_graph): | ||
| # Initialize transformer with fuzzy matching enabled | ||
| transformer = RDFTransformer(label_map, fuzzy=True, fuzzy_threshold=90) | ||
| output_graph = transformer.transform(input_graph) | ||
|
|
||
| # --- Check graph triples --- | ||
| # Original triple must remain | ||
| assert (EX.subj1, EX.hasCategory, Literal("Geology")) in output_graph | ||
| # --- 1. Check graph triples --- | ||
|
|
||
| # Transformed triple must be present | ||
| expected_iri = URIRef("http://example.org/ontology#Geology") | ||
| assert (EX.subj1, EX.hasCategory, expected_iri) in output_graph | ||
| # Exact match: literal replaced by IRI | ||
| assert (EX.subj1, EX.hasCategory, URIRef("http://example.org/ontology#Geology")) in output_graph | ||
|
|
||
| # UnknownLabel should remain unchanged (no IRI added) | ||
| assert (EX.subj2, EX.hasCategory, Literal("UnknownLabel")) in output_graph | ||
| assert len([t for t in output_graph.triples((EX.subj2, EX.hasCategory, None))]) == 1 | ||
| # Fuzzy match: literal replaced by IRI | ||
| assert (EX.subj2, EX.hasCategory, URIRef("http://example.org/ontology#Biology")) in output_graph | ||
|
|
||
| # Non-literals should remain untouched | ||
| assert (EX.subj3, EX.hasValue, URIRef("http://example.org/someIRI")) in output_graph | ||
| # Unknown label: literal remains unchanged | ||
| assert (EX.subj3, EX.hasCategory, Literal("unknownlabel")) in output_graph | ||
|
|
||
| # --- Check log entries --- | ||
| # Non-literals remain untouched | ||
| assert (EX.subj4, EX.hasValue, URIRef("http://example.org/someIRI")) in output_graph | ||
|
|
||
| # --- 2. Check log entries --- | ||
| log = transformer.log.entries | ||
| geology_log = next(e for e in log if e["original"] == "Geology") | ||
|
|
||
| # Exact match log | ||
| geology_log = next(e for e in log if e["original"] == "geology") | ||
| assert geology_log["replacement"] == "http://example.org/ontology#Geology" | ||
| assert geology_log["reason"] == "unambiguous match" | ||
| assert geology_log["reason"] == "exact match" | ||
|
|
||
| # Fuzzy match log | ||
| biolgy_log = next(e for e in log if e["original"] == "biolgy") | ||
| assert biolgy_log["replacement"] == "http://example.org/ontology#Biology" | ||
| assert "fuzzy match" in biolgy_log["reason"] | ||
|
|
||
| unknown_log = next(e for e in log if e["original"] == "UnknownLabel") | ||
| # Unknown label log | ||
| unknown_log = next(e for e in log if e["original"] == "unknownlabel") | ||
| assert unknown_log["replacement"] is None | ||
| assert unknown_log["reason"] == "no match in label map" | ||
| assert unknown_log["reason"] == "no match found" |
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.