Loading src/main/java/edu/bu/LanguageCorrection/Corrector.java +54 −9 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.util.List; import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Collections; public class Corrector { Loading Loading @@ -51,24 +53,64 @@ public class Corrector { } } public String correct(String inputSentence) { public String[] correct(String inputSentence) { // Divide sentence into words String[] words = inputSentence.split(" "); List<String> sentences = generateSentences(words); float bestScore = Float.MAX_VALUE; String result = ""; // Use a priority queue to store sentences along with their scores PriorityQueue<SentenceScorePair> pq = new PriorityQueue<>( Collections.reverseOrder()); for (String sentence : sentences) { float score = detector.perplexity(sentence); // System.out.println(sentence + " | Score: " + score); if (score < bestScore) { bestScore = score; result = sentence; pq.offer(new SentenceScorePair(sentence, score)); // Ensure only the top 5 sentences are kept, remove the worst if more than 5 if (pq.size() > 5) { pq.poll(); } } // Print the top sentences with their scores SentenceScorePair[] topPairs = new SentenceScorePair[pq.size()]; int index = pq.size() - 1; while (!pq.isEmpty()) { topPairs[index] = pq.poll(); index--; } // Print sentences in the right order // for (SentenceScorePair pair : topPairs) { // System.out.println(pair.sentence + " | Score: " + pair.score); // } // Convert to array of just sentences String[] topSentences = new String[topPairs.length]; for (int i = 0; i < topPairs.length; i++) { topSentences[i] = topPairs[i].sentence; } return result; return topSentences; } // Helper class to manage sentences and their scores class SentenceScorePair implements Comparable<SentenceScorePair> { String sentence; float score; public SentenceScorePair(String sentence, float score) { this.sentence = sentence; this.score = score; } @Override public int compareTo(SentenceScorePair other) { return Float.compare(this.score, other.score); } } public static List<String> generateSentences(String[] words) { Loading Loading @@ -104,8 +146,11 @@ public class Corrector { String[] sentences = TextProcessor.extractSentences(content).toArray(new String[0]); for (String sentence : sentences) { sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", ""); String corrected = corrector.correct(sentence); System.out.println(corrected); String[] corrected = corrector.correct(sentence); System.out.println(sentence + " | Corrected Sentence Suggestions:"); for (String correctedSentence : corrected) { System.out.println(" " + correctedSentence); } } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); Loading Loading
src/main/java/edu/bu/LanguageCorrection/Corrector.java +54 −9 Original line number Diff line number Diff line Loading @@ -8,6 +8,8 @@ import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.util.List; import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Collections; public class Corrector { Loading Loading @@ -51,24 +53,64 @@ public class Corrector { } } public String correct(String inputSentence) { public String[] correct(String inputSentence) { // Divide sentence into words String[] words = inputSentence.split(" "); List<String> sentences = generateSentences(words); float bestScore = Float.MAX_VALUE; String result = ""; // Use a priority queue to store sentences along with their scores PriorityQueue<SentenceScorePair> pq = new PriorityQueue<>( Collections.reverseOrder()); for (String sentence : sentences) { float score = detector.perplexity(sentence); // System.out.println(sentence + " | Score: " + score); if (score < bestScore) { bestScore = score; result = sentence; pq.offer(new SentenceScorePair(sentence, score)); // Ensure only the top 5 sentences are kept, remove the worst if more than 5 if (pq.size() > 5) { pq.poll(); } } // Print the top sentences with their scores SentenceScorePair[] topPairs = new SentenceScorePair[pq.size()]; int index = pq.size() - 1; while (!pq.isEmpty()) { topPairs[index] = pq.poll(); index--; } // Print sentences in the right order // for (SentenceScorePair pair : topPairs) { // System.out.println(pair.sentence + " | Score: " + pair.score); // } // Convert to array of just sentences String[] topSentences = new String[topPairs.length]; for (int i = 0; i < topPairs.length; i++) { topSentences[i] = topPairs[i].sentence; } return result; return topSentences; } // Helper class to manage sentences and their scores class SentenceScorePair implements Comparable<SentenceScorePair> { String sentence; float score; public SentenceScorePair(String sentence, float score) { this.sentence = sentence; this.score = score; } @Override public int compareTo(SentenceScorePair other) { return Float.compare(this.score, other.score); } } public static List<String> generateSentences(String[] words) { Loading Loading @@ -104,8 +146,11 @@ public class Corrector { String[] sentences = TextProcessor.extractSentences(content).toArray(new String[0]); for (String sentence : sentences) { sentence = sentence.replaceAll("[^a-zA-Z0-9\\s]", ""); String corrected = corrector.correct(sentence); System.out.println(corrected); String[] corrected = corrector.correct(sentence); System.out.println(sentence + " | Corrected Sentence Suggestions:"); for (String correctedSentence : corrected) { System.out.println(" " + correctedSentence); } } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); Loading