package domain; import java.io.*; import java.util.Map; public class WordCloudCreator { public void insertWordsIntoTemplate(Map wordMap) { File templateFile = new File("wordcloud.html"); // Template in project directory File outputFile = new File("output.html"); // Output in project directory if (!templateFile.exists()) { throw new RuntimeException("Template file 'wordcloud.html' not found in project directory."); } try (BufferedReader reader = new BufferedReader(new FileReader(templateFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { StringBuilder htmlContent = new StringBuilder(); String line; // Read the HTML template while ((line = reader.readLine()) != null) { htmlContent.append(line).append("\n"); } // Generate clickable word entries with font size based on frequency StringBuilder wordEntries = new StringBuilder(); int id = 1; for (Map.Entry entry : wordMap.entrySet()) { String word = entry.getKey(); int frequency = entry.getValue(); int fontSize = (int) ((float) 12 + frequency * 1.5); // Example: Base size 10px, increase by 2px per frequency wordEntries.append(String.format( "" + "%s" + "\n", id++, fontSize, word, word )); } // Replace placeholder inside the div String updatedHtml = htmlContent.toString(); if (updatedHtml.contains("")) { updatedHtml = updatedHtml.replace("", wordEntries); } else { throw new RuntimeException("Placeholder for tags not found in the template."); } // Write the updated HTML to the output file writer.write(updatedHtml); System.out.println("Output file 'output.html' created successfully!"); } catch (IOException e) { throw new RuntimeException("Error processing HTML template", e); } } }