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; import java.io.File;
public class FileLoader { public class FileLoader {
private File filePath; private File inputFile;
private File stopwordsPath;
private String fileFormat;
private String stopwordFormat;
public FileLoader() { public FileLoader() {
this.filePath = null; this.inputFile = null;
this.stopwordsPath = null;
this.fileFormat = "";
this.stopwordFormat = "";
} }
//AI Method with swing FileChooser with changes to run in this program //KI erstellte Methode mit anpassungen
public void loadFileGUI() { public File loadFileGUI() {
try { try {
JFileChooser fileChooser = new JFileChooser(); JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf")); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
@ -28,21 +22,17 @@ public class FileLoader {
int result = fileChooser.showOpenDialog(null); int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) { if (result == JFileChooser.APPROVE_OPTION) {
if (filePath == null) { inputFile = fileChooser.getSelectedFile();
filePath = fileChooser.getSelectedFile();
fileFormat = getFileFormat(filePath);
} else {
stopwordsPath = fileChooser.getSelectedFile();
stopwordFormat = getFileFormat(stopwordsPath);
}
} }
return inputFile;
} catch (HeadlessException e) { } catch (HeadlessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public String getFileFormat(File path) { //Methode sucht das Datei Format für spätere Verwendung
String fileName = path.getName(); public String getFileFormat(File file) {
String fileName = file.getName();
String fileFormat = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : ""; String fileFormat = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
switch (fileFormat.toLowerCase()) { switch (fileFormat.toLowerCase()) {
@ -58,19 +48,4 @@ public class FileLoader {
return "File format not supported"; 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.*; import java.io.*;
public class TextProcessing { public class TextProcessing {
private boolean stemming = false; private boolean stemming;
private int maxWords = 0; private int maxWords;
private Set<String> stopwordList = new HashSet<>();
// 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) { public String formatToText(File file, String format) {
try { try {
@ -67,24 +83,7 @@ public class TextProcessing {
return "Nothing found!"; return "Nothing found!";
} }
// public String fileToTextString(File path, String format) { public Map<String, Integer> maxShowWords(Map<String, Integer> words) {
// 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) {
HashMap <String, Integer> cuttedHashmap = new HashMap<>(); HashMap <String, Integer> cuttedHashmap = new HashMap<>();
int index = maxWords; int index = maxWords;
for (String word : words.keySet()) { for (String word : words.keySet()) {
@ -96,26 +95,15 @@ public class TextProcessing {
return cuttedHashmap; return cuttedHashmap;
} }
public Map<String, Integer> sortList(Map<String, Integer> unsortedMap) { //KI Methode die abgeändert wurde, damit sie in dieses Programm passt
List<Map.Entry<String, Integer>> entryList = new ArrayList<>(unsortedMap.entrySet()); public Map<String, Integer> tokenizingFile(String text, Set<String> stopwords) {
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) {
Map<String, Integer> words = new HashMap<>(); Map<String, Integer> words = new HashMap<>();
if (text == null || text.isBlank()) { if (text == null || text.isBlank()) {
return words; return words;
} }
CharArraySet luceneStopwords = 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)) { try (Analyzer analyzer = new StandardAnalyzer(luceneStopwords)) {
TokenStream tokenStream = analyzer.tokenStream(null, text); TokenStream tokenStream = analyzer.tokenStream(null, text);
@ -126,16 +114,12 @@ public class TextProcessing {
String word = charTermAttribute.toString(); String word = charTermAttribute.toString();
if (words.containsKey(word)) { if (words.containsKey(word)) {
words.compute(word, (k, counter) -> counter + 1); words.compute(word, (k, counter) -> counter + 1);
} else { }
words.put(word, 1); else {
} words.put(word, 1);
}
} }
tokenStream.end(); tokenStream.end();
if (maxWords > 0) {
Map<String, Integer> sortedWords;
sortedWords = maxShowWords(sortList(words), maxWords);
return sortedWords;
}
} }
catch (IOException e) { catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -143,15 +127,31 @@ public class TextProcessing {
return words; return words;
} }
public void setStemming(boolean stemming) { public Set<String> textToSetStopwords(Map<String, Integer> words) {
this.stemming = stemming; Set<String> stopwordList = new HashSet<>();
for (Map.Entry<String, Integer> entry : words.entrySet()) {
stopwordList.add(entry.getKey());
}
return stopwordList;
} }
public void setMaxWords(int maxWords) { public String fileToTextString(File path, String format) {
this.maxWords = maxWords; 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) { // public Map<String, Integer> stemming(Map<String, Integer> wordList) {
// Map<String, Integer> wordCounts = new HashMap<>(); // Map<String, Integer> wordCounts = new HashMap<>();

View File

@ -5,13 +5,12 @@ import java.util.Map;
public class WordCloudCreator { public class WordCloudCreator {
//AI Method but mit massive changes public void insertWordsIntoTemplate(Map<String, Integer> wordMap) {
public boolean insertWordsIntoTemplate(Map<String, Integer> wordMap) {
File templateFile = new File("wordcloud.html"); // Template in project directory 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()) { 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)); try (BufferedReader reader = new BufferedReader(new FileReader(templateFile));
@ -20,6 +19,7 @@ public class WordCloudCreator {
StringBuilder htmlContent = new StringBuilder(); StringBuilder htmlContent = new StringBuilder();
String line; String line;
// Read the HTML template
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
htmlContent.append(line).append("\n"); htmlContent.append(line).append("\n");
} }
@ -54,6 +54,5 @@ public class WordCloudCreator {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error processing HTML template", 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.WordCloudCreator;
import domain.TextProcessing; 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 { public class WordCloudManager {
private FileLoader fl; private FileLoader fileLoader;
private TextProcessing tp; private TextProcessing processing;
private WordCloudCreator wcm; 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() { public WordCloudManager() {
fl = new FileLoader(); fileLoader = new FileLoader();
tp = new TextProcessing(); processing = new TextProcessing();
wcm = new WordCloudCreator(); creator = new WordCloudCreator();
fileFormat = "";
fileFormathStopwords = "";
} }
public boolean loadFile() { public boolean loadFileGUI() {
fl.loadFileGUI(); if (filePath == null) {
if (fl.getFilePath().length() > 0) { filePath = fileLoader.loadFileGUI();
System.out.println(fl.getFilePath()); fileFormat = fileLoader.getFileFormat(filePath);
System.out.println(fl.getStopwordsPath()); 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; return true;
} else { } else {
System.out.println(fl.getFilePath());
System.out.println(fl.getStopwordsPath());
return false; return false;
} }
} }
public boolean loadStopwords() { public void addToStopWords(String extraStopword) {
if(fl.getStopwordsPath() != null) { stopwordList.add(extraStopword);
String stopwordfile = tp.formatToText(fl.getStopwordsPath(), fl.getStopwordFormat()); System.out.println(stopwordList);
Map<String, Integer> tokinizedWords = tp.tokenizingFile(stopwordfile);
tp.textToSetStopwords(tokinizedWords);
return true;
}
else {
return false;
}
} }
public void addToStopWords(String stopword) { public void setStopWords() {
tp.addToStopWords(stopword); Set<String> stopwords = processing.textToSetStopwords(processing.tokenizingFile(processing.
formatToText(stopwordsPath, fileFormathStopwords), null));
stopwordList.addAll(stopwords);
System.out.println(stopwordList);
} }
public void setMaxWords(int maxWords) { // für spätere Verwendung mit umfangreichen Änderungen im Code
tp.setMaxWords(maxWords); // 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) { public void tokenizingText() {
if(stemming) { wordMap = (HashMap<String, Integer>) processing.tokenizingFile(processing.fileToTextString(filePath, fileFormat)
tp.setStemming(true); , !stopwordList.isEmpty() ? stopwordList : null);
} System.out.println(wordMap.keySet() + "\n" + wordMap.values());
} }
public boolean createWordCloud() { public void cutWordsList() {
if(wcm.insertWordsIntoTemplate(tp.tokenizingFile(tp.formatToText(fl.getFilePath(),fl.getFileFormat())))) { wordMap = (HashMap<String, Integer>) processing.maxShowWords(processing.sortList(wordMap));
fl = null; processing.sortList(wordMap);
tp = null; System.out.println(wordMap.keySet() + "\n" + wordMap.values());
fl = new FileLoader(); }
tp = new TextProcessing();
return true; public void createWordCloud() {
} creator.insertWordsIntoTemplate(wordMap);
return false;
} }
} }

View File

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

View File

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