-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexer.java
More file actions
26 lines (21 loc) · 997 Bytes
/
Copy pathIndexer.java
File metadata and controls
26 lines (21 loc) · 997 Bytes
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
//This builds an indented index mapping words to the pages where they appear
import java.util.*;
public class Indexer {
private Map<String, List<String>> invertedIndex = new HashMap<>();
// Index the crawled content
public void index(Map<String, String> crawledData) {
for (Map.Entry<String, String> entry : crawledData.entrySet()) {
String url = entry.getKey();
String content = entry.getValue();
String[] words = content.split("\\W+"); // Tokenization
for (String word : words) {
word = word.toLowerCase(); // Convert to lowercase for consistency
invertedIndex.putIfAbsent(word, new ArrayList<>());
invertedIndex.get(word).add(url); // Add URL to the index
}
}
}
public List<String> search(String query) {
return invertedIndex.getOrDefault(query.toLowerCase(), new ArrayList<>());
}
}