Repository: TheAlgorithms/Java
Pull request #5696 (2024-10-10 07:05:36)

Add smoothing constant to IDF formula in BM25 to prevent negative scores
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5696?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 59.73%. Comparing base [(`6535d47`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/6535d4755d8bed3cd410571568d0200f01bd806a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`9349bc3`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9349bc3e6548b6c1014269758923b0225fdc9ea0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #5696   +/-   ##
=========================================
  Coverage     59.72%   59.73%           
  Complexity     3854     3854           
=========================================
  Files           560      560           
  Lines         16011    16011           
  Branches       3072     3072           
=========================================
+ Hits           9563     9564    +1     
+ Misses         6045     6044    -1     
  Partials        403      403           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5696?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/searches/BM25InvertedIndex.java b/src/main/java/com/thealgorithms/searches/BM25InvertedIndex.java
index 1cfd2bba..aeddc591 100644
--- a/src/main/java/com/thealgorithms/searches/BM25InvertedIndex.java
+++ b/src/main/java/com/thealgorithms/searches/BM25InvertedIndex.java
@@ -215,6 +215,6 @@ public final class BM25InvertedIndex {
      */
     private double computeIDF(int docFrequency) {
         // Total number of documents in the index
-        return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5));
+        return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5) + 1);
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java b/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java
index 8595e0a0..2017c11d 100644
--- a/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java
+++ b/src/test/java/com/thealgorithms/searches/BM25InvertedIndexTest.java
@@ -50,13 +50,15 @@ class BM25InvertedIndexTest {
         // Perform search for the term "good"
         List<SearchResult> results = index.search("good");
         assertFalse(results.isEmpty());
-
+        for (SearchResult result : results) {
+            System.out.println(result);
+        }
         // Validate the ranking based on the provided relevance scores
-        assertEquals(6, results.get(0).getDocId()); // It's a Wonderful Life should be ranked 1st
-        assertEquals(7, results.get(1).getDocId()); // The Pursuit of Happyness should be ranked 2nd
+        assertEquals(1, results.get(0).getDocId()); // The Shawshank Redemption should be ranked 1st
+        assertEquals(8, results.get(1).getDocId()); // A Few Good Men should be ranked 2nd
         assertEquals(5, results.get(2).getDocId()); // Good Will Hunting should be ranked 3rd
-        assertEquals(8, results.get(3).getDocId()); // A Few Good Men should be ranked 4th
-        assertEquals(1, results.get(4).getDocId()); // The Shawshank Redemption should be ranked 5th
+        assertEquals(7, results.get(3).getDocId()); // The Pursuit of Happyness should be ranked 4th
+        assertEquals(6, results.get(4).getDocId()); // It's a Wonderful Life should be ranked 5th
 
         // Ensure the relevance scores are in descending order
         for (int i = 0; i < results.size() - 1; i++) {