PR2WordCloud/src/main/java/domain/WordCloudCreator.java

55 lines
2.3 KiB
Java

package domain;
import java.io.*;
import java.util.Map;
public class WordCloudCreator {
private int maxFontSize = 70;
public boolean insertWordsIntoTemplate(Map<String, Integer> wordMap) {
File templateFile = new File("wordcloud.html"); // Template in project directory
File outputFile = new File("createdHTML.html"); // Output in project directory
try (BufferedReader reader = new BufferedReader(new FileReader(templateFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
StringBuilder htmlContent = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
htmlContent.append(line).append("\n");
}
//AI generated lines below
// Generated 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 = Math.min(10 + frequency * 2, maxFontSize); // 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);
return true;
} catch (IOException e) {
throw new RuntimeException("Error processing HTML template", e);
}
}
}