diff --git a/src/main/java/domain/WordCloudCreator.java b/src/main/java/domain/WordCloudCreator.java new file mode 100644 index 0000000..87cf955 --- /dev/null +++ b/src/main/java/domain/WordCloudCreator.java @@ -0,0 +1,59 @@ +package domain; + +import java.io.*; +import java.util.Map; + +public class WordCloudCreator { + + //Ki erstellte Methode aus Zeitgründen und Krankheitsgründen + 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); + } + } +} \ No newline at end of file