Compare commits

..

No commits in common. "3aeee2014610f80295dad4b66aa32ebc5b24354b" and "95b6c5ffaacb2f676482139e777b3739eaca1576" have entirely different histories.

6 changed files with 139 additions and 153 deletions

View File

@ -6,19 +6,13 @@ import java.awt.*;
import java.io.File;
public class FileLoader {
private File filePath;
private File stopwordsPath;
private String fileFormat;
private String stopwordFormat;
private File inputFile;
public FileLoader() {
this.filePath = null;
this.stopwordsPath = null;
this.fileFormat = "";
this.stopwordFormat = "";
this.inputFile = null;
}
//AI Method with swing FileChooser with changes to run in this program
public void loadFileGUI() {
//KI erstellte Methode mit anpassungen
public File loadFileGUI() {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
@ -28,21 +22,17 @@ public class FileLoader {
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
if (filePath == null) {
filePath = fileChooser.getSelectedFile();
fileFormat = getFileFormat(filePath);
} else {
stopwordsPath = fileChooser.getSelectedFile();
stopwordFormat = getFileFormat(stopwordsPath);
}
inputFile = fileChooser.getSelectedFile();
}
return inputFile;
} catch (HeadlessException e) {
throw new RuntimeException(e);
}
}
public String getFileFormat(File path) {
String fileName = path.getName();
//Methode sucht das Datei Format für spätere Verwendung
public String getFileFormat(File file) {
String fileName = file.getName();
String fileFormat = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
switch (fileFormat.toLowerCase()) {
@ -58,19 +48,4 @@ public class FileLoader {
return "File format not supported";
}
}
public File getFilePath() {
return filePath;
}
public File getStopwordsPath() {
return stopwordsPath;
}
public String getFileFormat() {
return fileFormat;
}
public String getStopwordFormat() {
return stopwordFormat;
}
}

View File

@ -20,9 +20,25 @@ import java.util.*;
import java.io.*;
public class TextProcessing {
private boolean stemming = false;
private int maxWords = 0;
private Set<String> stopwordList = new HashSet<>();
private boolean stemming;
private int maxWords;
// für spätere verwendung mit umfangreichen anpassungen
// public boolean isStemming() {
// return stemming;
// }
//
// public int getMaxWords() {
// return maxWords;
// }
//
// public void setStemming(boolean stemming) {
// this.stemming = stemming;
// }
//
public void setMaxWords(int maxWords) {
this.maxWords = maxWords;
}
public String formatToText(File file, String format) {
try {
@ -67,24 +83,7 @@ public class TextProcessing {
return "Nothing found!";
}
// public String fileToTextString(File path, String format) {
// String text = formatToText(path, format);
// return text;
// }
public void textToSetStopwords(Map<String, Integer> words) {
Set<String> stopwords = new HashSet<>();
for (Map.Entry<String, Integer> entry : words.entrySet()) {
stopwords.add(entry.getKey());
}
stopwordList.addAll(stopwords);
}
public void addToStopWords(String stopword) {
stopwordList.add(stopword);
}
public Map<String, Integer> maxShowWords(Map<String, Integer> words, int maxWords) {
public Map<String, Integer> maxShowWords(Map<String, Integer> words) {
HashMap <String, Integer> cuttedHashmap = new HashMap<>();
int index = maxWords;
for (String word : words.keySet()) {
@ -96,26 +95,15 @@ public class TextProcessing {
return cuttedHashmap;
}
public Map<String, Integer> sortList(Map<String, Integer> unsortedMap) {
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(unsortedMap.entrySet());
entryList.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue())); //Ki erstellte Zeile
Map<String, Integer> sortedMap = new TreeMap<>();
for (Map.Entry<String, Integer> entry : entryList) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
public Map<String, Integer> tokenizingFile(String text) {
//KI Methode die abgeändert wurde, damit sie in dieses Programm passt
public Map<String, Integer> tokenizingFile(String text, Set<String> stopwords) {
Map<String, Integer> words = new HashMap<>();
if (text == null || text.isBlank()) {
return words;
}
CharArraySet luceneStopwords =
stopwordList != null ? new CharArraySet(stopwordList, true) : CharArraySet.EMPTY_SET;
stopwords != null ? new CharArraySet(stopwords, true) : CharArraySet.EMPTY_SET;
try (Analyzer analyzer = new StandardAnalyzer(luceneStopwords)) {
TokenStream tokenStream = analyzer.tokenStream(null, text);
@ -126,16 +114,12 @@ public class TextProcessing {
String word = charTermAttribute.toString();
if (words.containsKey(word)) {
words.compute(word, (k, counter) -> counter + 1);
} else {
words.put(word, 1);
}
}
else {
words.put(word, 1);
}
}
tokenStream.end();
if (maxWords > 0) {
Map<String, Integer> sortedWords;
sortedWords = maxShowWords(sortList(words), maxWords);
return sortedWords;
}
}
catch (IOException e) {
throw new RuntimeException(e);
@ -143,15 +127,31 @@ public class TextProcessing {
return words;
}
public void setStemming(boolean stemming) {
this.stemming = stemming;
public Set<String> textToSetStopwords(Map<String, Integer> words) {
Set<String> stopwordList = new HashSet<>();
for (Map.Entry<String, Integer> entry : words.entrySet()) {
stopwordList.add(entry.getKey());
}
return stopwordList;
}
public void setMaxWords(int maxWords) {
this.maxWords = maxWords;
public String fileToTextString(File path, String format) {
String text = formatToText(path, format);
return text;
}
public Map<String, Integer> sortList(Map<String, Integer> unsortedMap) {
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(unsortedMap.entrySet());
entryList.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue())); //Ki erstellte Zeile
Map<String, Integer> sortedMap = new LinkedHashMap<>();
for (Map.Entry<String, Integer> entry : entryList) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
// public Map<String, Integer> stemming(Map<String, Integer> wordList) {
// Map<String, Integer> wordCounts = new HashMap<>();

View File

@ -5,13 +5,12 @@ import java.util.Map;
public class WordCloudCreator {
//AI Method but mit massive changes
public boolean insertWordsIntoTemplate(Map<String, Integer> wordMap) {
public void 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
File outputFile = new File("output.html"); // Output in project directory
if (!templateFile.exists()) {
throw new RuntimeException("File not found!");
throw new RuntimeException("Template file 'wordcloud.html' not found in project directory.");
}
try (BufferedReader reader = new BufferedReader(new FileReader(templateFile));
@ -20,6 +19,7 @@ public class WordCloudCreator {
StringBuilder htmlContent = new StringBuilder();
String line;
// Read the HTML template
while ((line = reader.readLine()) != null) {
htmlContent.append(line).append("\n");
}
@ -54,6 +54,5 @@ public class WordCloudCreator {
} catch (IOException e) {
throw new RuntimeException("Error processing HTML template", e);
}
return true;
}
}

View File

@ -4,67 +4,87 @@ import domain.FileLoader;
import domain.WordCloudCreator;
import domain.TextProcessing;
import java.util.Map;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class WordCloudManager {
private FileLoader fl;
private TextProcessing tp;
private WordCloudCreator wcm;
private FileLoader fileLoader;
private TextProcessing processing;
private WordCloudCreator creator;
private File filePath;
private File stopwordsPath;
private String fileFormat;
private String fileFormathStopwords;
private String text;
Set<String> stopwordList = new HashSet<>();
private HashMap<String, Integer> wordMap;
public WordCloudManager() {
fl = new FileLoader();
tp = new TextProcessing();
wcm = new WordCloudCreator();
fileLoader = new FileLoader();
processing = new TextProcessing();
creator = new WordCloudCreator();
fileFormat = "";
fileFormathStopwords = "";
}
public boolean loadFile() {
fl.loadFileGUI();
if (fl.getFilePath().length() > 0) {
System.out.println(fl.getFilePath());
System.out.println(fl.getStopwordsPath());
public boolean loadFileGUI() {
if (filePath == null) {
filePath = fileLoader.loadFileGUI();
fileFormat = fileLoader.getFileFormat(filePath);
System.out.println("File: " + filePath);
System.out.println("File: " + stopwordsPath);
} else {
stopwordsPath = fileLoader.loadFileGUI();
fileFormathStopwords = fileLoader.getFileFormat(stopwordsPath);
System.out.println("File: " + filePath);
System.out.println("File: " + stopwordsPath);
}
if (filePath.length() > 0) {
return true;
} else {
System.out.println(fl.getFilePath());
System.out.println(fl.getStopwordsPath());
return false;
}
}
public boolean loadStopwords() {
if(fl.getStopwordsPath() != null) {
String stopwordfile = tp.formatToText(fl.getStopwordsPath(), fl.getStopwordFormat());
Map<String, Integer> tokinizedWords = tp.tokenizingFile(stopwordfile);
tp.textToSetStopwords(tokinizedWords);
return true;
}
else {
return false;
}
public void addToStopWords(String extraStopword) {
stopwordList.add(extraStopword);
System.out.println(stopwordList);
}
public void addToStopWords(String stopword) {
tp.addToStopWords(stopword);
public void setStopWords() {
Set<String> stopwords = processing.textToSetStopwords(processing.tokenizingFile(processing.
formatToText(stopwordsPath, fileFormathStopwords), null));
stopwordList.addAll(stopwords);
System.out.println(stopwordList);
}
public void setMaxWords(int maxWords) {
tp.setMaxWords(maxWords);
// für spätere Verwendung mit umfangreichen Änderungen im Code
// public void stemming(String approval) {
// if(approval.equals("yes")) {
// processing.setStemming(true);
// }
// }
public void maxWordsInList(int number) {
processing.setMaxWords(number);
}
public void setStemming(boolean stemming) {
if(stemming) {
tp.setStemming(true);
}
public void tokenizingText() {
wordMap = (HashMap<String, Integer>) processing.tokenizingFile(processing.fileToTextString(filePath, fileFormat)
, !stopwordList.isEmpty() ? stopwordList : null);
System.out.println(wordMap.keySet() + "\n" + wordMap.values());
}
public boolean createWordCloud() {
if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(),fl.getFileFormat())))) {
fl = null;
tp = null;
fl = new FileLoader();
tp = new TextProcessing();
return true;
}
return false;
public void cutWordsList() {
wordMap = (HashMap<String, Integer>) processing.maxShowWords(processing.sortList(wordMap));
processing.sortList(wordMap);
System.out.println(wordMap.keySet() + "\n" + wordMap.values());
}
public void createWordCloud() {
creator.insertWordsIntoTemplate(wordMap);
}
}

View File

@ -1,6 +1,7 @@
package tui;
public class Main {
//startet die GUI
public static void main(String[]args){
new TUI();
}

View File

@ -14,6 +14,7 @@ public class TUI {
public TUI(){
wcm = new WordCloudManager();
isRunning = true;
fMenu = true;
scan = new Scanner(System.in);
tui();
@ -27,9 +28,8 @@ public class TUI {
switch (option) {
case(0):
//Load File GUI
if(wcm.loadFile()) {
if(wcm.loadFileGUI()) {
System.out.println("File loaded successful!\n");
fMenu = true;
fileMenu();
} else {
System.out.println("File cannot be loaded!\n");
@ -56,49 +56,40 @@ public class TUI {
switch(option) {
case (0):
// Load stopwords file
if(wcm.loadFile()) {
if(wcm.loadStopwords()) {
System.out.println("File loaded successful!");
}
else{
System.out.println("Cannot load one more File!");
}
if(wcm.loadFileGUI()) {
System.out.println("File loaded successful!\n");
wcm.setStopWords();
} else {
System.out.println("File cannot be loaded!");
System.out.println("File cannot be loaded!\n");
}
break;
case(1):
// Add more stopwords
System.out.println("Type your stopword: ");
System.out.println("Type your stopword:\n");
String input = scan.nextLine();
wcm.addToStopWords(input);
break;
case(2):
// Set number of max words
System.out.println("How much max words do you want? ");
int number = Integer.parseInt(scan.nextLine());
wcm.setMaxWords(number);
wcm.maxWordsInList(number);
wcm.cutWordsList();
break;
case(3):
// Set Stemming
System.out.println("Stemming: Input 'yes' or 'no'? ");
String stemmingOption = scan.nextLine();
if(stemmingOption.equals("yes")) {
wcm.setStemming(true);
}
System.out.println("Set Stemming: Input 'yes' or 'no'");
// String stemmingOption = scan.nextLine();
// wcm.stemming(stemmingOption);
break;
case(4):
//Create WordCloud
if(wcm.createWordCloud()) {
System.out.println("HTML File created!");
fMenu = false;
}
else {
System.out.println("HTML FIle not created!");
}
wcm.tokenizingText();
wcm.createWordCloud();
fMenu = false;
System.out.println("HTML File created!");
break;
case(5):
//Exit Filemenu
//Exit filemenu
fMenu = false;
System.out.println("Close Program!");
break;