Merge pull request 'added check if letter and build Hashmap counter for existing words' (#3) from FileManager into main
Reviewed-on: #3main
commit
b88f377ace
|
@ -2,40 +2,100 @@ package domain;
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.text.PDFTextStripper;
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
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 {
|
public class FileManager {
|
||||||
|
File inputFile;
|
||||||
|
String originalPath;
|
||||||
|
String goalPath;
|
||||||
|
|
||||||
public void loadFile() {
|
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 {
|
try {
|
||||||
JFileChooser fileChooser = new JFileChooser();
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
|
fileChooser.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
|
||||||
|
|
||||||
int result = fileChooser.showOpenDialog(null);
|
int result = fileChooser.showOpenDialog(null);
|
||||||
|
|
||||||
if (result == JFileChooser.APPROVE_OPTION) {
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||||||
File selectedFile = fileChooser.getSelectedFile();
|
inputFile = fileChooser.getSelectedFile();
|
||||||
System.out.println("Ausgewählte Datei: " + selectedFile.getAbsolutePath());
|
}
|
||||||
|
return inputFile;
|
||||||
|
} catch (HeadlessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try (PDDocument document = PDDocument.load(selectedFile)) {
|
public boolean processFile(File inputFile){
|
||||||
|
boolean success = false;
|
||||||
|
try {
|
||||||
|
PDDocument document = null;
|
||||||
|
HashMap<String, Integer> filteredWords = new HashMap<>();
|
||||||
|
if(inputFile != null) {
|
||||||
|
document = PDDocument.load(inputFile);
|
||||||
PDFTextStripper pdfStripper = new PDFTextStripper();
|
PDFTextStripper pdfStripper = new PDFTextStripper();
|
||||||
String text = pdfStripper.getText(document);
|
String text = pdfStripper.getText(document);
|
||||||
String regex = "[,\\s\\.:/]";
|
String splittedText = "[,\\s\\.:/!§$%&/()=?+*~#.;_<>^°]";
|
||||||
String[] words = text.split(regex);
|
String[] textWords = text.split(splittedText);
|
||||||
for(String word : words) {
|
for(String word : textWords){
|
||||||
System.out.println(word);
|
// 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 {
|
else {
|
||||||
System.out.println("Dateiauswahl abgebrochen.");
|
filteredWords.put(word, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for(Map.Entry e : filteredWords.entrySet()){
|
||||||
|
System.out.println(e.getKey() + " = " + e.getValue());
|
||||||
|
}
|
||||||
|
if (document != null) {
|
||||||
|
document.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
success = true;
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveFile(){}
|
public void saveFile(){}
|
||||||
|
|
|
@ -1,4 +1,30 @@
|
||||||
package facade;
|
package facade;
|
||||||
|
|
||||||
|
import domain.FileManager;
|
||||||
|
import domain.PictureManager;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
public class WordCloudManager {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,52 @@
|
||||||
package tui;
|
package tui;
|
||||||
|
|
||||||
import domain.FileManager;
|
import domain.FileManager;
|
||||||
|
import facade.WordCloudManager;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class TUI {
|
public class TUI {
|
||||||
|
boolean isRunning;
|
||||||
|
|
||||||
public TUI(){
|
public TUI(){
|
||||||
|
isRunning = true;
|
||||||
tui();
|
tui();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tui() {
|
public void tui() {
|
||||||
Scanner scan = new Scanner(System.in);
|
Scanner scan = new Scanner(System.in);
|
||||||
|
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();
|
int option = scan.nextInt();
|
||||||
|
WordCloudManager wcm = new WordCloudManager();
|
||||||
|
|
||||||
switch (option) {
|
switch (option) {
|
||||||
case (0):
|
case (0):
|
||||||
//Load File
|
//Load File Path
|
||||||
FileManager fileManager = new FileManager();
|
wcm.loadFilePath();
|
||||||
fileManager.loadFile();
|
break;
|
||||||
case (1):
|
case (1):
|
||||||
//Save File
|
//Load File GUI
|
||||||
|
if(wcm.loadFileGUI()) {
|
||||||
|
System.out.println("File loaded successful!\n");
|
||||||
|
}
|
||||||
|
break;
|
||||||
case (2):
|
case (2):
|
||||||
//Show Picture
|
//Save Picture
|
||||||
|
break;
|
||||||
case (3):
|
case (3):
|
||||||
|
//Show Picture
|
||||||
|
break;
|
||||||
|
case (4):
|
||||||
//Exit
|
//Exit
|
||||||
|
isRunning = false;
|
||||||
|
System.out.println("Close Program!");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
scan.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue