Write code for search engines.
"""
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import
java.util.Map
;
public class SearchEngine { private Map<String, List> index;
public SearchEngine() { index = new HashMap<>(); }
// Add a document to the search engine public void addDocument(String document, String[] keywords) { for (String keyword : keywords) { if (!index.containsKey(keyword)) { index.put(keyword, new ArrayList<>()); } index.get(keyword).add(document); } }
// Perform keyword search public List search(String keyword) { if (index.containsKey(keyword)) { return index.get(keyword); } else { return new ArrayList<>(); } }
public static void main(String[] args) { // Create a sample search engine SearchEngine searchEngine = new SearchEngine();
// Add documents to the search engine searchEngine.addDocument("document4", new String[] {"java", "programming", "tutorial"}); searchEngine.addDocument("document2", new String[] {"java", "coding", "example"}); searchEngine.addDocument("document3", new String[] {"python", "tutorial", "example"});
// Perform keyword search String keyword = "java"; List searchResults =
searchEngine.search
(keyword);
// Display search results System.out.println("Search results for keyword '" + keyword + "':"); if (searchResults.isEmpty()) { System.out.println("No results found."); } else { for (String result : searchResults) { System.out.println("- " + result); } } } }
"""