refactored FileManager and added Classes FileLoader, TextProcessing. PDF opening is functioning.
parent
92fbcc8d49
commit
af33d8a566
|
@ -0,0 +1,53 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FileLoader {
|
||||||
|
private File inputFile;
|
||||||
|
|
||||||
|
public FileLoader() {
|
||||||
|
this.inputFile = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File loadFileGUI() {
|
||||||
|
try {
|
||||||
|
JFileChooser fileChooser = new JFileChooser();
|
||||||
|
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
|
||||||
|
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
|
||||||
|
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Word Documents", "docx"));
|
||||||
|
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PowerPoint Presentations", "pptx"));
|
||||||
|
int result = fileChooser.showOpenDialog(null);
|
||||||
|
|
||||||
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||||||
|
inputFile = fileChooser.getSelectedFile();
|
||||||
|
}
|
||||||
|
return inputFile;
|
||||||
|
} catch (HeadlessException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileFormat(File file) {
|
||||||
|
String fileName = file.getName();
|
||||||
|
String fileFormat = fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
|
||||||
|
|
||||||
|
switch (fileFormat.toLowerCase()) {
|
||||||
|
case "pdf":
|
||||||
|
return "pdf";
|
||||||
|
case "txt":
|
||||||
|
return "txt";
|
||||||
|
case "docx":
|
||||||
|
return "docx";
|
||||||
|
case "pptx":
|
||||||
|
return "pptx";
|
||||||
|
default:
|
||||||
|
return "File format not supported";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,115 +1,26 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import java.io.File;
|
||||||
import org.apache.pdfbox.text.PDFTextStripper;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
public class FileManager {
|
public class FileManager {
|
||||||
File inputFile;
|
FileLoader fileLoader = new FileLoader();
|
||||||
String originalPath;
|
TextProcessing textProcessing = new TextProcessing();
|
||||||
String goalPath;
|
private File file;
|
||||||
|
|
||||||
public FileManager() {
|
public String loadFile() {
|
||||||
originalPath = "quelle.pdf";
|
file = fileLoader.loadFileGUI();
|
||||||
goalPath = "ziel.txt";
|
String fileFormat = fileLoader.getFileFormat(file);
|
||||||
inputFile = null;
|
String text = textProcessing.formatToText(file, fileFormat);
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream loadFilePath() {
|
public HashMap tokenizingText(String text) {
|
||||||
InputStream in;
|
HashMap<String, Integer> wordMap = textProcessing.tokenizingText(text);
|
||||||
OutputStream out = null;
|
return wordMap;
|
||||||
|
|
||||||
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.addChoosableFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
|
|
||||||
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
|
|
||||||
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Word Documents", "docx"));
|
|
||||||
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PowerPoint Presentations", "pptx"));
|
|
||||||
int result = fileChooser.showOpenDialog(null);
|
|
||||||
|
|
||||||
if (result == JFileChooser.APPROVE_OPTION) {
|
|
||||||
inputFile = fileChooser.getSelectedFile();
|
|
||||||
}
|
|
||||||
return inputFile;
|
|
||||||
} catch (HeadlessException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashMap tokenizingText(File inputFile){
|
|
||||||
HashMap<String, Integer> filteredWords = new HashMap<>();
|
|
||||||
try {
|
|
||||||
PDDocument document = null;
|
|
||||||
if(inputFile != null) {
|
|
||||||
document = PDDocument.load(inputFile);
|
|
||||||
PDFTextStripper pdfStripper = new PDFTextStripper();
|
|
||||||
String text = pdfStripper.getText(document);
|
|
||||||
|
|
||||||
//Tokenizing der Wörter
|
|
||||||
String splittedText = "[,\\s\\.:/!§$%&/()=?+*~#.;_<>^°\"']";
|
|
||||||
String[] textWords = text.split(splittedText);
|
|
||||||
for(String word : textWords){
|
|
||||||
if (filteredWords.containsKey(word)) {
|
|
||||||
filteredWords.compute(word, (k, counter) -> counter + 1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
filteredWords.put(word, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(Map.Entry e : filteredWords.entrySet()){
|
|
||||||
System.out.println(e.getKey() + " = " + e.getValue());
|
|
||||||
}
|
|
||||||
if (document != null) {
|
|
||||||
document.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return filteredWords;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashMap maxShowWords(int number, HashMap<String, Integer> words) {
|
|
||||||
HashMap <String, Integer> cuttedHashmap = new HashMap<>();
|
|
||||||
int index = number;
|
|
||||||
for (String word : words.keySet()) {
|
|
||||||
if(index > 0) {
|
|
||||||
cuttedHashmap.put(word, words.get(word));
|
|
||||||
}
|
|
||||||
index--;
|
|
||||||
}
|
|
||||||
return cuttedHashmap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveFile(){}
|
public void saveFile(){}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
package domain;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.text.PDFTextStripper;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class TextProcessing {
|
||||||
|
|
||||||
|
public String formatToText(File file, String format) {
|
||||||
|
PDDocument document;
|
||||||
|
try {
|
||||||
|
if (file != null) {
|
||||||
|
switch (format) {
|
||||||
|
case "txt":
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "pdf":
|
||||||
|
document = PDDocument.load(file);
|
||||||
|
PDFTextStripper pdfStripper = new PDFTextStripper();
|
||||||
|
return pdfStripper.getText(document);
|
||||||
|
|
||||||
|
case "docx":
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "pptx":
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
return "Nothing found!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap maxShowWords(int number, HashMap<String, Integer> words) {
|
||||||
|
HashMap <String, Integer> cuttedHashmap = new HashMap<>();
|
||||||
|
int index = number;
|
||||||
|
for (String word : words.keySet()) {
|
||||||
|
if(index > 0) {
|
||||||
|
cuttedHashmap.put(word, words.get(word));
|
||||||
|
}
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
return cuttedHashmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HashMap tokenizingText(String text){
|
||||||
|
HashMap<String, Integer> filteredWords = new HashMap<>();
|
||||||
|
try {
|
||||||
|
//Tokenizing der Wörter
|
||||||
|
String splitter = "[,\\s\\.:/!§$%&/()=?+*~#.;_<>^°\"']";
|
||||||
|
String[] textWords = text.split(splitter);
|
||||||
|
for(String word : textWords){
|
||||||
|
if (filteredWords.containsKey(word)) {
|
||||||
|
filteredWords.compute(word, (k, counter) -> counter + 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
filteredWords.put(word, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
return filteredWords;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,9 +4,8 @@ import domain.FileManager;
|
||||||
import domain.PictureManager;
|
import domain.PictureManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class WordCloudManager {
|
public class WordCloudManager {
|
||||||
FileManager fileManager;
|
FileManager fileManager;
|
||||||
|
@ -18,18 +17,14 @@ public class WordCloudManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean loadFileGUI() {
|
public boolean loadFileGUI() {
|
||||||
File inputFile = fileManager.loadFileGUI();
|
|
||||||
HashMap wordMap = fileManager.tokenizingText(inputFile);
|
String fileText = fileManager.loadFile();
|
||||||
if(wordMap == null) {
|
HashMap wordMap = fileManager.tokenizingText(fileText);
|
||||||
return false;
|
if(wordMap != null) {
|
||||||
}
|
|
||||||
else {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadFilePath() {
|
|
||||||
OutputStream inputFile = fileManager.loadFilePath();
|
|
||||||
// fileManager.processFile(null, inputFile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package tui;
|
package tui;
|
||||||
|
|
||||||
import domain.FileManager;
|
|
||||||
import facade.WordCloudManager;
|
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 {
|
||||||
|
@ -17,16 +14,14 @@ public class TUI {
|
||||||
|
|
||||||
public void tui() {
|
public void tui() {
|
||||||
Scanner scan = new Scanner(System.in);
|
Scanner scan = new Scanner(System.in);
|
||||||
while(isRunning) {
|
WordCloudManager wcm = new WordCloudManager();
|
||||||
|
// while(isRunning) {
|
||||||
System.out.println("Welcome to Word Cloud.\nMenu:\n\n(0) Load File from main path\n(1) Load File with Gui" +
|
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");
|
"\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 Path
|
//Load File Path
|
||||||
wcm.loadFilePath();
|
|
||||||
break;
|
break;
|
||||||
case (1):
|
case (1):
|
||||||
//Load File GUI
|
//Load File GUI
|
||||||
|
@ -48,7 +43,7 @@ public class TUI {
|
||||||
System.out.println("Close Program!");
|
System.out.println("Close Program!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
// }
|
||||||
scan.close();
|
scan.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue