refactoring
parent
f256a6eacc
commit
f8ecd6a679
|
@ -8,11 +8,13 @@ public class WordCloudCreator {
|
|||
|
||||
//Create html file with clickable words
|
||||
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 csvFile = new File("createdWordcloud.csv"); // CSV output in project directory
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(templateFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
|
||||
BufferedWriter csvWriter = new BufferedWriter(new FileWriter(csvFile))) {
|
||||
|
||||
StringBuilder htmlContent = new StringBuilder();
|
||||
String line;
|
||||
|
@ -20,20 +22,26 @@ public class WordCloudCreator {
|
|||
while ((line = reader.readLine()) != null) {
|
||||
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
|
||||
StringBuilder wordEntries = new StringBuilder();
|
||||
int id = 1;
|
||||
csvWriter.write("Word,Frequency\n"); // Write CSV header
|
||||
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; margin-right:10px\">" +
|
||||
"<a href=\"https://www.google.com/search?q=%s\" target=\"_blank\">%s</a>" +
|
||||
"</span>\n",
|
||||
id++, fontSize, word, word
|
||||
));
|
||||
|
||||
// Write word and frequency to CSV
|
||||
csvWriter.write(String.format("%s,%d\n", word, frequency));
|
||||
}
|
||||
|
||||
// Replace placeholder inside the div
|
||||
|
@ -47,10 +55,9 @@ public class WordCloudCreator {
|
|||
// Write the updated HTML to the output file
|
||||
writer.write(updatedHtml);
|
||||
return true;
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error processing HTML template", e);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException("Error processing HTML template or CSV file", e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -4,4 +4,4 @@ public class Main {
|
|||
public static void main(String[]args){
|
||||
new TUI();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -118,7 +118,7 @@ public class TUI {
|
|||
if(wcm.createWordCloud()) {
|
||||
System.out.println("HTML File created!\n");
|
||||
fMenu = false;
|
||||
System.out.println("Close Program!\n");
|
||||
System.out.println("Close File menu!\n");
|
||||
}
|
||||
else {
|
||||
System.out.println("HTML FIle not created!\n");
|
||||
|
|
Loading…
Reference in New Issue