59 lines
2.6 KiB
Java
59 lines
2.6 KiB
Java
package domain;
|
|
|
|
import java.io.*;
|
|
import java.util.Map;
|
|
|
|
public class WordCloudCreator {
|
|
|
|
//Ki erstellte Methode wegen Zeitgründen und Krankheitsgründen, dennoch anpassungen in großen Maße waren notwendig
|
|
public void insertWordsIntoTemplate(Map<String, Integer> 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<String, Integer> 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(
|
|
"<span id=\"%d\" class=\"wrd\" style=\"font-size:%dpx;\">" +
|
|
"<a href=\"https://www.google.com/search?q=%s\" target=\"_blank\">%s</a>" +
|
|
"</span>\n",
|
|
id++, fontSize, word, word
|
|
));
|
|
}
|
|
|
|
// Replace placeholder inside the div
|
|
String updatedHtml = htmlContent.toString();
|
|
if (updatedHtml.contains("<!-- TODO: Hier die generierten Tags einsetzen -->")) {
|
|
updatedHtml = updatedHtml.replace("<!-- TODO: Hier die generierten Tags einsetzen -->", 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);
|
|
}
|
|
}
|
|
} |