Skip to content
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

Cu 86c0p7y10 implement levenshtein distance comparison #336

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

MatthewErispe
Copy link
Collaborator

@MatthewErispe MatthewErispe commented Oct 17, 2024

Summary by CodeRabbit

  • New Features
    • Introduced new similarity scoring mechanisms: Soundex and Levenshtein similarity.
    • Added a new enum for structured similarity function selection, enhancing type safety.
  • Bug Fixes
    • Improved error handling and logging in the scoring framework.
  • Refactor
    • Updated similarity function retrieval to use an enum, improving maintainability and extensibility.

@rcrichton
Copy link
Member

Copy link
Contributor

coderabbitai bot commented Oct 17, 2024

Walkthrough

The changes in LinkerProbabilistic.java enhance the similarity scoring mechanisms by introducing new classes for Soundex and Levenshtein similarity calculations. The method for retrieving similarity functions is refactored to use an enum, improving type safety. Additionally, updates are made to the mapping of probabilistic fields to utilize this new enum-based retrieval. The constructor for ProbabilisticField is adjusted to correctly initialize the new similarity functions, and minor updates to error handling and logging are included to integrate these changes into the existing framework.

Changes

File Path Change Summary
JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java - Added SoundexSimilarity, LevenshteinSimilarity, and LevenshteinSimilarityPercentage classes.
- Refactored getSimilarityFunction to use SimilarityFunctionName enum.
- Updated mappings for probabilistic fields to use the new enum.
- Modified ProbabilisticField constructor for new similarity functions.
- Minor adjustments to error handling and logging in updateMU.

Possibly related PRs

Poem

In the land of code where rabbits play,
New scoring methods hop in today.
With Soundex and Levenshtein, oh what a treat,
Our similarity scores now dance on their feet!
Hopping through enums, so clear and bright,
A joyful leap into coding delight! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7fa8954 and 4d97e1d.

📒 Files selected for processing (1)
  • JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (2)
JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (2)

356-358: Avoid magic numbers in similarity functions

In LevenshteinSimilarity, returning 0.5 for empty inputs is a magic number that may confuse readers.

Define a constant to represent the default similarity score for empty inputs:

private static final double DEFAULT_EMPTY_INPUT_SCORE = 0.0;

Then use it in your method:

if (StringUtils.isEmpty(left) || StringUtils.isEmpty(right)) {
-   return 0.5;
+   return DEFAULT_EMPTY_INPUT_SCORE;
}

80-88: Alphabetize the SimilarityFunctionName enum constants

For better readability and maintainability, consider alphabetizing the constants in the SimilarityFunctionName enum.

 public enum SimilarityFunctionName {
-    JARO_WINKLER_SIMILARITY,
-    JARO_SIMILARITY,
-    JACCARD_SIMILARITY,
-    SOUNDEX_SIMILARITY,
-    EXACT_SIMILARITY,
-    LEVENSHTEIN_SIMILARITY,
-    LEVENSHTEIN_SIMILARITY_PERCENTAGE
+    EXACT_SIMILARITY,
+    JACCARD_SIMILARITY,
+    JARO_SIMILARITY,
+    JARO_WINKLER_SIMILARITY,
+    LEVENSHTEIN_SIMILARITY,
+    LEVENSHTEIN_SIMILARITY_PERCENTAGE,
+    SOUNDEX_SIMILARITY
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 76b7f24 and fe8c001.

📒 Files selected for processing (1)
  • JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (5 hunks)
🧰 Additional context used

…ackend/LinkerProbabilistic.java

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between fe8c001 and b9aa5c7.

📒 Files selected for processing (1)
  • JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (5 hunks)
🧰 Additional context used
🔇 Additional comments (3)
JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (3)

4-4: LGTM: New imports and similarity function declarations

The new imports for Soundex and LevenshteinDistance, along with the static field declarations for SoundexSimilarity, LevenshteinSimilarity, and LevenshteinSimilarityPercentage, are correctly added to support the new similarity functions.

Also applies to: 8-8, 32-34


348-363: ⚠️ Potential issue

Adjust LevenshteinSimilarity to return a normalized similarity score

The LevenshteinSimilarity class currently returns the raw Levenshtein distance, which represents dissimilarity rather than similarity. To maintain consistency with other similarity functions, it should return a normalized similarity score between 0.0 and 1.0.

Consider modifying the apply method as follows:

- return Double.valueOf(levenshteinDistance.apply(left, right));
+ int distance = levenshteinDistance.apply(left, right);
+ int maxLength = Math.max(left.length(), right.length());
+ return 1.0 - ((double) distance / maxLength);

This change ensures that a higher score indicates greater similarity, consistent with other similarity functions.

Likely invalid or redundant comment.


80-105: 🛠️ Refactor suggestion

Refactor getSimilarityFunction using a Map for better scalability

The SimilarityFunctionName enum and getSimilarityFunction method update improve type safety and readability. However, the switch statement can be replaced with a more efficient and scalable Map-based approach.

Consider refactoring the method as follows:

+private static final Map<SimilarityFunctionName, SimilarityScore<Double>> SIMILARITY_FUNCTION_MAP = Map.of(
+    SimilarityFunctionName.JARO_WINKLER_SIMILARITY, JARO_WINKLER_SIMILARITY,
+    SimilarityFunctionName.JARO_SIMILARITY, JARO_SIMILARITY,
+    SimilarityFunctionName.JACCARD_SIMILARITY, JACCARD_SIMILARITY,
+    SimilarityFunctionName.SOUNDEX_SIMILARITY, SOUNDEX_SIMILARITY,
+    SimilarityFunctionName.LEVENSHTEIN_SIMILARITY, LEVENSHTEIN_SIMILARITY,
+    SimilarityFunctionName.LEVENSHTEIN_SIMILARITY_PERCENTAGE, LEVENSHTEIN_SIMILARITY_PERCENTAGE,
+    SimilarityFunctionName.EXACT_SIMILARITY, EXACT_SIMILARITY
+);

-static SimilarityScore<Double> getSimilarityFunction(final SimilarityFunctionName func) {
-   switch (func) {
-       case JARO_WINKLER_SIMILARITY:
-           return JARO_WINKLER_SIMILARITY;
-       // Other cases...
-       default:
-           return EXACT_SIMILARITY;
-   }
+static SimilarityScore<Double> getSimilarityFunction(final SimilarityFunctionName func) {
+   return SIMILARITY_FUNCTION_MAP.getOrDefault(func, EXACT_SIMILARITY);
}

This approach is more maintainable and performs better, especially as more similarity functions are added.

Likely invalid or redundant comment.

MatthewErispe and others added 4 commits October 17, 2024 11:45
…ackend/LinkerProbabilistic.java

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ackend/LinkerProbabilistic.java

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (2)

33-34: Approve changes with suggestion for error handling

The new static fields and changes to existing fields improve type safety by using the SimilarityFunctionName enum. However, there's a potential for IllegalArgumentException if the similarity score string doesn't match any enum constant.

Consider adding error handling:

-.map(f -> new ProbabilisticField(getSimilarityFunction(SimilarityFunctionName.valueOf(f.similarityScore())), f.comparisonLevels(), f.m(), f.u()))
+.map(f -> {
+    SimilarityFunctionName functionName;
+    try {
+        functionName = SimilarityFunctionName.valueOf(f.similarityScore());
+    } catch (IllegalArgumentException e) {
+        LOGGER.error("Invalid similarity function name: {}", f.similarityScore(), e);
+        functionName = SimilarityFunctionName.EXACT_SIMILARITY; // Fallback to a default
+    }
+    return new ProbabilisticField(getSimilarityFunction(functionName), f.comparisonLevels(), f.m(), f.u());
+})

This change would provide more robust error handling and logging.

Also applies to: 44-44, 48-48, 52-52, 73-73


298-315: Consider adjusting empty string handling in SoundexSimilarity

The SoundexSimilarity class is implemented correctly. However, returning 0.0 for empty or null strings might not be appropriate in all cases. Consider standardizing this approach across all similarity functions:

if (StringUtils.isEmpty(left) || StringUtils.isEmpty(right)) {
-   return 0.0;
+   return 0.5;
}

This change aligns with the ExactSimilarity implementation and provides a neutral similarity score when inputs are invalid.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b9aa5c7 and 7fa8954.

📒 Files selected for processing (1)
  • JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (6 hunks)
🧰 Additional context used
🔇 Additional comments (2)
JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/backend/LinkerProbabilistic.java (2)

4-8: LGTM: New imports added correctly

The new imports for Soundex, LevenshteinDistance, and Map are correctly added and necessary for the new similarity functions being introduced.

Also applies to: 20-20


80-99: LGTM: Improved structure with enum and map

The introduction of the SimilarityFunctionName enum and SIMILARITY_FUNCTION_MAP, along with the updated getSimilarityFunction method, greatly improves the code structure, type safety, and maintainability. This change makes it easier to add new similarity functions in the future and reduces the likelihood of errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants