-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBM25InvertedIndex.java
220 lines (191 loc) · 7.89 KB
/
BM25InvertedIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.thealgorithms.searches;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Inverted Index implementation with BM25 Scoring for movie search.
* This class supports adding movie documents and searching for terms
* within those documents using the BM25 algorithm.
* @author Prayas Kumar (https://github.com/prayas7102)
*/
class Movie {
int docId; // Unique identifier for the movie
String name; // Movie name
double imdbRating; // IMDb rating of the movie
int releaseYear; // Year the movie was released
String content; // Full text content (could be the description or script)
/**
* Constructor for the Movie class.
* @param docId Unique identifier for the movie.
* @param name Name of the movie.
* @param imdbRating IMDb rating of the movie.
* @param releaseYear Release year of the movie.
* @param content Content or description of the movie.
*/
Movie(int docId, String name, double imdbRating, int releaseYear, String content) {
this.docId = docId;
this.name = name;
this.imdbRating = imdbRating;
this.releaseYear = releaseYear;
this.content = content;
}
/**
* Get all the words from the movie's name and content.
* Converts the name and content to lowercase and splits on non-word characters.
* @return Array of words from the movie name and content.
*/
public String[] getWords() {
return (name + " " + content).toLowerCase().split("\\W+");
}
@Override
public String toString() {
return "Movie{"
+ "docId=" + docId + ", name='" + name + '\'' + ", imdbRating=" + imdbRating + ", releaseYear=" + releaseYear + '}';
}
}
class SearchResult {
int docId; // Unique identifier of the movie document
double relevanceScore; // Relevance score based on the BM25 algorithm
/**
* Constructor for SearchResult class.
* @param docId Document ID (movie) for this search result.
* @param relevanceScore The relevance score based on BM25 scoring.
*/
SearchResult(int docId, double relevanceScore) {
this.docId = docId;
this.relevanceScore = relevanceScore;
}
public int getDocId() {
return docId;
}
@Override
public String toString() {
return "SearchResult{"
+ "docId=" + docId + ", relevanceScore=" + relevanceScore + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SearchResult that = (SearchResult) o;
return docId == that.docId && Double.compare(that.relevanceScore, relevanceScore) == 0;
}
@Override
public int hashCode() {
return Objects.hash(docId, relevanceScore);
}
public double getRelevanceScore() {
return this.relevanceScore;
}
}
public final class BM25InvertedIndex {
private Map<String, Map<Integer, Integer>> index; // Inverted index mapping terms to document id and frequency
private Map<Integer, Movie> movies; // Mapping of movie document IDs to Movie objects
private int totalDocuments; // Total number of movies/documents
private double avgDocumentLength; // Average length of documents (number of words)
private static final double K = 1.5; // BM25 tuning parameter, controls term frequency saturation
private static final double B = 0.75; // BM25 tuning parameter, controls length normalization
/**
* Constructor for BM25InvertedIndex.
* Initializes the inverted index and movie storage.
*/
BM25InvertedIndex() {
index = new HashMap<>();
movies = new HashMap<>();
totalDocuments = 0;
avgDocumentLength = 0.0;
}
/**
* Add a movie to the index.
* @param docId Unique identifier for the movie.
* @param name Name of the movie.
* @param imdbRating IMDb rating of the movie.
* @param releaseYear Release year of the movie.
* @param content Content or description of the movie.
*/
public void addMovie(int docId, String name, double imdbRating, int releaseYear, String content) {
Movie movie = new Movie(docId, name, imdbRating, releaseYear, content);
movies.put(docId, movie);
totalDocuments++;
// Get words (terms) from the movie's name and content
String[] terms = movie.getWords();
int docLength = terms.length;
// Update the average document length
avgDocumentLength = (avgDocumentLength * (totalDocuments - 1) + docLength) / totalDocuments;
// Update the inverted index
for (String term : terms) {
// Create a new entry if the term is not yet in the index
index.putIfAbsent(term, new HashMap<>());
// Get the list of documents containing the term
Map<Integer, Integer> docList = index.get(term);
if (docList == null) {
docList = new HashMap<>();
index.put(term, docList); // Ensure docList is added to the index
}
// Increment the term frequency in this document
docList.put(docId, docList.getOrDefault(docId, 0) + 1);
}
}
public int getMoviesLength() {
return movies.size();
}
/**
* Search for documents containing a term using BM25 scoring.
* @param term The search term.
* @return A list of search results sorted by relevance score.
*/
public List<SearchResult> search(String term) {
term = term.toLowerCase(); // Normalize search term
if (!index.containsKey(term)) {
return new ArrayList<>(); // Return empty list if term not found
}
Map<Integer, Integer> termDocs = index.get(term); // Documents containing the term
List<SearchResult> results = new ArrayList<>();
// Compute IDF for the search term
double idf = computeIDF(termDocs.size());
// Calculate relevance scores for all documents containing the term
for (Map.Entry<Integer, Integer> entry : termDocs.entrySet()) {
int docId = entry.getKey();
int termFrequency = entry.getValue();
Movie movie = movies.get(docId);
if (movie == null) {
continue; // Skip this document if movie doesn't exist
}
double docLength = movie.getWords().length;
// Compute BM25 relevance score
double score = computeBM25Score(termFrequency, docLength, idf);
results.add(new SearchResult(docId, score));
}
// Sort the results by relevance score in descending order
results.sort((r1, r2) -> Double.compare(r2.relevanceScore, r1.relevanceScore));
return results;
}
/**
* Compute the BM25 score for a given term and document.
* @param termFrequency The frequency of the term in the document.
* @param docLength The length of the document.
* @param idf The inverse document frequency of the term.
* @return The BM25 relevance score for the term in the document.
*/
private double computeBM25Score(int termFrequency, double docLength, double idf) {
double numerator = termFrequency * (K + 1);
double denominator = termFrequency + K * (1 - B + B * (docLength / avgDocumentLength));
return idf * (numerator / denominator);
}
/**
* Compute the inverse document frequency (IDF) of a term.
* The IDF measures the importance of a term across the entire document set.
* @param docFrequency The number of documents that contain the term.
* @return The inverse document frequency (IDF) value.
*/
private double computeIDF(int docFrequency) {
// Total number of documents in the index
return Math.log((totalDocuments - docFrequency + 0.5) / (docFrequency + 0.5) + 1);
}
}