From b0cc3a5ce9bc7232eb65c124b1e416f54043e8e9 Mon Sep 17 00:00:00 2001 From: Daniel Fromm <3015351@stud.hs-mannheim.de> Date: Sat, 3 May 2025 01:43:29 +0200 Subject: [PATCH] added check if letter and build Hashmap counter for existing words --- src/main/java/domain/FileManager.java | 96 ++++++++++++++++++---- src/main/java/facade/WordCloudManager.java | 26 ++++++ src/main/java/tui/TUI.java | 46 ++++++++--- 3 files changed, 138 insertions(+), 30 deletions(-) diff --git a/src/main/java/domain/FileManager.java b/src/main/java/domain/FileManager.java index 8c563e2..b0d83ec 100644 --- a/src/main/java/domain/FileManager.java +++ b/src/main/java/domain/FileManager.java @@ -2,40 +2,100 @@ package domain; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; + import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; -import java.io.File; + +import java.awt.*; +import java.io.*; +import java.util.HashMap; +import java.util.Map; + public class FileManager { + File inputFile; + String originalPath; + String goalPath; - public void loadFile() { - try{ + public FileManager() { + originalPath = "quelle.pdf"; + goalPath = "ziel.txt"; + inputFile = null; + } + + public OutputStream loadFilePath() { + InputStream in; + OutputStream out = null; + + try { + in = new FileInputStream(originalPath); + out = new FileOutputStream(goalPath); + + byte[] buffer = new byte[1024]; + int gelesen; + + while ((gelesen = in.read(buffer)) > -1) { + out.write(buffer, 0, gelesen); + } + + in.close(); + out.close(); + return out; + } + catch (IOException e) { + e.printStackTrace(); + } + return out; + } + + public File loadFileGUI() { + try { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf")); - int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { - File selectedFile = fileChooser.getSelectedFile(); - System.out.println("Ausgewählte Datei: " + selectedFile.getAbsolutePath()); + inputFile = fileChooser.getSelectedFile(); + } + return inputFile; + } catch (HeadlessException e) { + throw new RuntimeException(e); + } + } - try (PDDocument document = PDDocument.load(selectedFile)) { - PDFTextStripper pdfStripper = new PDFTextStripper(); - String text = pdfStripper.getText(document); - String regex = "[,\\s\\.:/]"; - String[] words = text.split(regex); - for(String word : words) { - System.out.println(word); + public boolean processFile(File inputFile){ + boolean success = false; + try { + PDDocument document = null; + HashMap filteredWords = new HashMap<>(); + if(inputFile != null) { + document = PDDocument.load(inputFile); + PDFTextStripper pdfStripper = new PDFTextStripper(); + String text = pdfStripper.getText(document); + String splittedText = "[,\\s\\.:/!§$%&/()=?+*~#.;_<>^°]"; + String[] textWords = text.split(splittedText); + for(String word : textWords){ +// if(Character.isLetter(word.charAt(0)) && Character.isLetter((word.charAt(word.length() - 1)))) { + if (filteredWords.containsKey(word)) { + filteredWords.compute(word, (k, counter) -> counter + 1); + } +// } + else { + filteredWords.put(word, 0); } } + for(Map.Entry e : filteredWords.entrySet()){ + System.out.println(e.getKey() + " = " + e.getValue()); + } + if (document != null) { + document.close(); + } } - else { - System.out.println("Dateiauswahl abgebrochen."); - } - - } catch (Exception e) { + success = true; + } catch (Exception e){ e.printStackTrace(); } + return success; } public void saveFile(){} diff --git a/src/main/java/facade/WordCloudManager.java b/src/main/java/facade/WordCloudManager.java index da42f98..338c014 100644 --- a/src/main/java/facade/WordCloudManager.java +++ b/src/main/java/facade/WordCloudManager.java @@ -1,4 +1,30 @@ package facade; +import domain.FileManager; +import domain.PictureManager; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.OutputStream; + public class WordCloudManager { + FileManager fileManager; + PictureManager pictureManager; + + public WordCloudManager() { + fileManager = new FileManager(); + pictureManager = new PictureManager(); + } + + public boolean loadFileGUI() { + boolean success = false; + File inputFile = fileManager.loadFileGUI(); + success = fileManager.processFile(inputFile); + return success; + } + + public void loadFilePath() { + OutputStream inputFile = fileManager.loadFilePath(); +// fileManager.processFile(null, inputFile); + } } diff --git a/src/main/java/tui/TUI.java b/src/main/java/tui/TUI.java index b68fb1a..b20fe62 100644 --- a/src/main/java/tui/TUI.java +++ b/src/main/java/tui/TUI.java @@ -1,30 +1,52 @@ package tui; import domain.FileManager; +import facade.WordCloudManager; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Scanner; public class TUI { + boolean isRunning; public TUI(){ + isRunning = true; tui(); } public void tui() { Scanner scan = new Scanner(System.in); - int option = scan.nextInt(); + while(isRunning) { + System.out.println("Welcome to Word Cloud.\nMenu:\n\n(0) Load File from main path\n(1) Load File with Gui" + + "\n(2) Save File\n(3) Show Picture\n(4) Exit"); + int option = scan.nextInt(); + WordCloudManager wcm = new WordCloudManager(); - switch (option) { - case (0): - //Load File - FileManager fileManager = new FileManager(); - fileManager.loadFile(); - case (1): - //Save File - case (2): - //Show Picture - case (3): - //Exit + switch (option) { + case (0): + //Load File Path + wcm.loadFilePath(); + break; + case (1): + //Load File GUI + if(wcm.loadFileGUI()) { + System.out.println("File loaded successful!\n"); + } + break; + case (2): + //Save Picture + break; + case (3): + //Show Picture + break; + case (4): + //Exit + isRunning = false; + System.out.println("Close Program!"); + break; + } } + scan.close(); } }