refactoring

main
Daniel Fromm 2025-05-13 11:38:59 +02:00
parent f256a6eacc
commit f8ecd6a679
3 changed files with 17 additions and 10 deletions

View File

@ -8,11 +8,13 @@ public class WordCloudCreator {
//Create html file with clickable words //Create html file with clickable words
public boolean insertWordsIntoTemplate(Map<String, Integer> wordMap) { public boolean insertWordsIntoTemplate(Map<String, Integer> wordMap) {
File templateFile = new File("wordcloud.html"); // Template in project directory File inputFile = new File("wordcloud.html"); // Template in project directory
File outputFile = new File("createdHTML.html"); // Output in project directory File outputFile = new File("createdHTML.html"); // Output in project directory
File csvFile = new File("createdWordcloud.csv"); // CSV output in project directory
try (BufferedReader reader = new BufferedReader(new FileReader(templateFile)); try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
BufferedWriter csvWriter = new BufferedWriter(new FileWriter(csvFile))) {
StringBuilder htmlContent = new StringBuilder(); StringBuilder htmlContent = new StringBuilder();
String line; String line;
@ -20,20 +22,26 @@ public class WordCloudCreator {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
htmlContent.append(line).append("\n"); htmlContent.append(line).append("\n");
} }
//AI generated lines below
// With help from AI to generate code to write the html file
// Generated clickable word entries with font size based on frequency // Generated clickable word entries with font size based on frequency
StringBuilder wordEntries = new StringBuilder(); StringBuilder wordEntries = new StringBuilder();
int id = 1; int id = 1;
csvWriter.write("Word,Frequency\n"); // Write CSV header
for (Map.Entry<String, Integer> entry : wordMap.entrySet()) { for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
String word = entry.getKey(); String word = entry.getKey();
int frequency = entry.getValue(); int frequency = entry.getValue();
int fontSize = Math.min(10 + frequency * 2, maxFontSize); // Example: Base size 10px, increase by 2px per frequency int fontSize = Math.min(10 + frequency * 2, maxFontSize); // Example: Base size 10px, increase by 2px per frequency
wordEntries.append(String.format( wordEntries.append(String.format(
"<span id=\"%d\" class=\"wrd\" style=\"font-size:%dpx; margin-right:10px\">" + "<span id=\"%d\" class=\"wrd\" style=\"font-size:%dpx; margin-right:10px\">" +
"<a href=\"https://www.google.com/search?q=%s\" target=\"_blank\">%s</a>" + "<a href=\"https://www.google.com/search?q=%s\" target=\"_blank\">%s</a>" +
"</span>\n", "</span>\n",
id++, fontSize, word, word id++, fontSize, word, word
)); ));
// Write word and frequency to CSV
csvWriter.write(String.format("%s,%d\n", word, frequency));
} }
// Replace placeholder inside the div // Replace placeholder inside the div
@ -47,10 +55,9 @@ public class WordCloudCreator {
// Write the updated HTML to the output file // Write the updated HTML to the output file
writer.write(updatedHtml); writer.write(updatedHtml);
return true; return true;
}
} catch (IOException e) { catch (IOException e) {
throw new RuntimeException("Error processing HTML template", e); throw new RuntimeException("Error processing HTML template or CSV file", e);
} }
} }
} }

View File

@ -4,4 +4,4 @@ public class Main {
public static void main(String[]args){ public static void main(String[]args){
new TUI(); new TUI();
} }
} }

View File

@ -118,7 +118,7 @@ public class TUI {
if(wcm.createWordCloud()) { if(wcm.createWordCloud()) {
System.out.println("HTML File created!\n"); System.out.println("HTML File created!\n");
fMenu = false; fMenu = false;
System.out.println("Close Program!\n"); System.out.println("Close File menu!\n");
} }
else { else {
System.out.println("HTML FIle not created!\n"); System.out.println("HTML FIle not created!\n");