From 81ba9eb66f8284d8760012973f84cdbe617ceb94 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:16:34 -0700 Subject: init --- debugging/InvertedIndex.java | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 debugging/InvertedIndex.java (limited to 'debugging/InvertedIndex.java') diff --git a/debugging/InvertedIndex.java b/debugging/InvertedIndex.java new file mode 100644 index 0000000..65284f7 --- /dev/null +++ b/debugging/InvertedIndex.java @@ -0,0 +1,52 @@ +import java.util.*; + +public class InvertedIndex { + public static void main(String[] args) { + List docs = new ArrayList<>(); + docs.add("Raiders of the Lost Ark"); + docs.add("The Temple of Doom"); + docs.add("The Last Crusade"); + + Map> result = createIndex(docs); + System.out.println(docs); + System.out.println(); + System.out.println(result); + } + + // TODO: Write and document your createIndex method here + + public static Map> createIndex(List docs) { + Map> index = new TreeMap<>(); + Set uniqueWords = getUniqueWords(docs); + + for (String uniqueWord : uniqueWords) { + index.put(uniqueWord.toLowerCase(), new HashSet()); + } + + for (String word : index.keySet()) { + for (int i = 0; i < docs.size(); i++) { + Scanner wordScanner = new Scanner(docs.get(i)); + while (wordScanner.hasNext()) { + if (wordScanner.next().equalsIgnoreCase(word)) { + index.get(word).add(docs.get(i)); + } + } + } + } + return index; + } + + public static Set getUniqueWords(List docs) { + Set uniqueWords = new HashSet<>(); + for (String title : docs) { + Scanner titleScanner = new Scanner(title); + while (titleScanner.hasNext()) { + uniqueWords.add(titleScanner.next()); + } + titleScanner.close(); + } + return uniqueWords; + } + + +} -- cgit v1.2.3