Commit a05bc530 authored by Seyed Reza  Sajjadinasab's avatar Seyed Reza Sajjadinasab
Browse files

AddDutchTranslationAndHelp

parent 102fae21
Loading
Loading
Loading
Loading
+37 −7
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import util.JsonMaker;
import util.PhraseExtractor;
import util.SentenceExtractor;
import util.StringFileWriter;
import Translator.DirectTranslator;
import StateMachine.*;


@@ -89,15 +90,16 @@ public class Corrector implements GUIListener {
        DBinterface dbInterface = new DBinterface();
        DirectedGraph graph = basicGraphClass.getGraph();
        StringFileWriter stringWriter = StringFileWriter.of("corrected.txt");
        Corrector corrector = new Corrector(SentenceExtractor.of(argPars.getFileName()));
        
            
        StringFileWriter.deleteFile("correction_details.txt");
        if(argPars.isCheckFile()){
            SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
            List<String> extractedSentences = extractor.getSentences();
            if(argPars.isCorrectionGUI()){    
                    Corrector corrector = new Corrector(SentenceExtractor.of(argPars.getFileName()));
                    corrector.start();
            }else{
                SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
                List<String> extractedSentences = extractor.getSentences();
                for (String sentence : extractedSentences) {
                    System.out.println("Sentence: " + sentence);
                    String tempString = dbInterface.correctTokenInDatabase(sentence.toLowerCase(), graph, 2, true);
@@ -114,7 +116,10 @@ public class Corrector implements GUIListener {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
        }else if(argPars.isCheckSentence()){

            if(argPars.isCorrectionGUI()){  
                Corrector corrector = new Corrector(SentenceExtractor.of(argPars.getSentence()));  
                corrector.start();
            }else{
                System.out.println("Sentence: " + argPars.getSentence());
                stringWriter.appendString(dbInterface.correctTokenInDatabase(argPars.getSentence().toLowerCase(), graph, 2, true));
                try {
@@ -125,6 +130,31 @@ public class Corrector implements GUIListener {
                }
                System.out.println("##########################################################");
            }
        }else if(argPars.isTranslateDutch()){
            DirectTranslator directTranslator = DirectTranslator.make();
            directTranslator.loadWordMapFromFile("SQLite/word_map.txt");
            if(argPars.isCheckFile()){
                SentenceExtractor extractor = SentenceExtractor.of(argPars.getFileName());
                List<String> extractedSentences = extractor.getSentences();
                for (String sentence : extractedSentences) {
                    System.out.println("Sentence: " + sentence);
                    String tempString = directTranslator.replaceWordsInSentence(sentence.toLowerCase());
                    stringWriter.appendString(tempString);
                    System.out.println("##########################################################");
                }
            }else{
                
                System.out.println("Sentence: " + argPars.getSentence());
                stringWriter.appendString(directTranslator.replaceWordsInSentence(argPars.getSentence().toLowerCase()));
                System.out.println("##########################################################");
            }
            try {
                stringWriter.writeToFile();
                System.out.println("Corrected version has been written to the file.");
            } catch (IOException e) {
                System.err.println("An error occurred while writing to the file: " + e.getMessage());
            }
        }
          
    }
}
+7 −0
Original line number Diff line number Diff line
aa a
cc c
dd d
bb e
ee f
gg g
hh h
+58 −0
Original line number Diff line number Diff line
package Translator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DirectTranslator {

    private Map<String, String> wordMap;

    private DirectTranslator() {
        wordMap = new HashMap<>();
    }

    public static DirectTranslator make(){
        return new DirectTranslator();
    }

    public void loadWordMapFromFile(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] words = line.split("\\s+");
                if (words.length == 2) {
                    wordMap.put(words[0], words[1]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String replaceWordsInSentence(String sentence) {
        StringBuilder replacedSentence = new StringBuilder();
        String[] words = sentence.split("(?<=\\p{Punct}|\\s)|(?=\\p{Punct}|\\s)");
        for (String word : words) {
            if (wordMap.containsKey(word)) {
                replacedSentence.append(wordMap.get(word));
            } else {
                replacedSentence.append(word);
            }
        }
        // Remove extra space at the end and ensure no space before punctuation
        return replacedSentence.toString().trim().replaceAll("\\s(?=\\p{Punct})", "");
    }

    public static void main(String[] args) {
        DirectTranslator wordReplacement = new DirectTranslator();
        wordReplacement.loadWordMapFromFile("SQLite/word_map.txt");

        String sentence = "aa, bb! cc dd, ee? ff.";
        String replacedSentence = wordReplacement.replaceWordsInSentence(sentence);
        System.out.println("Original sentence: " + sentence);
        System.out.println("Replaced sentence: " + replacedSentence);
    }
}
+23 −3
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ public class ArgumentParser {
    private boolean correctionGUI;
    private boolean updateHashTable;
    private boolean validateUpdates;
    private boolean translateDutch;

    ArgumentParser(String[] args) {
        checkSentence = false;
@@ -23,6 +24,7 @@ public class ArgumentParser {
        validateUpdates = false;
        checkGUI = false;
        correctionGUI = false;
        translateDutch = false;
        parseArguments(Arrays.asList(args));
    }

@@ -65,15 +67,18 @@ public class ArgumentParser {
                    case "--validateUpdates":
                        validateUpdates = true;
                        break;
                    case "--checkGUI":
                    case "--checkerGUI":
                        checkGUI = true;
                        break;
                    case "--correctionGUI":
                    case "--correctorGUI":
                        correctionGUI = true;
                        break;
                    case "--translateDutch":
                        translateDutch = true;
                        break;
                    // Add cases for other arguments here
                    default:
                        // Handle unknown arguments or simply ignore them
                        System.out.println("Invalis options. Please use --help to see how to use the tool.");
                        break;
                }
            }
@@ -87,6 +92,18 @@ public class ArgumentParser {

    private void printHelp() {
        System.out.println("Help information:");
        System.out.println("    Corrector Options:");
        System.out.println("        --file <filename>: this option should be used if you want to pass your input as file.");
        System.out.println("        --sentence <sentence>: this option should be used if you want to pass your input as a small sentence.");
        System.out.println("        --correctorGUI: this option can be used if you want a GUI for the corrector to select between possible suggestions.");
        System.out.println("        --translateDutch <sentence>: this option should be used if you want to translate from English to Dutch.");
        System.out.println("    Checker Options:");
        System.out.println("        --file <filename>: this option should be used if you want to pass your input as file.");
        System.out.println("        --sentence <sentence>: this option should be used if you want to pass your input as a small sentence.");
        System.out.println("        --checkerGUI: this option can be used if you want a GUI for the checker to see the highlighted sentences.");
        System.out.println("        --updateToken: this option should be used alongside a file as input to update new tokens for the database. This option may take hours based on the size of crawled file.");
        System.out.println("        --updateHashTable: this option should be used alongside a file as input to update n-grams weights for the database. This option may take a few minutes.");
        System.out.println("        --validateUpdates: this option can be used to check the correctness of the database for tokens. This will pops up a window.");
        // Add help information here
    }

@@ -120,4 +137,7 @@ public class ArgumentParser {
    public boolean isCorrectionGUI(){
        return correctionGUI;
    }
    public boolean isTranslateDutch(){
        return translateDutch;
    }
}

WordReplacement.class

0 → 100644
+2.58 KiB

File added.

No diff preview for this file type.

Loading