-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammarChecker.java
123 lines (104 loc) · 3.67 KB
/
GrammarChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package proj5;
/**
* GRAMMARCHECKER CLASS
* Uses a thesaurus and word frequencies to replace overused words in a text document with random synonyms.
*
* @author Jordan An
* @version 6/5/2020
*/
public class GrammarChecker {
private Thesaurus thesaurus;
private WordCounter wC;
private LineReader lR;
private int limit;
/**
* Non-default constructor. Builds a thesaurus out of the given comma-separated file and sets the threshold for overused words
* @param thesaurusFile path to comma-separated file used to build a thesaurus
* @param threshold a word is considered "overused" if it appears more than (but not equal to) this many times in a text document
*/
public GrammarChecker(String thesaurusFile, int threshold){
thesaurus = new Thesaurus(thesaurusFile);
limit = threshold;
wC = new WordCounter();
lR = null;
}
/**
* Given a text file, replaces overused words with synonyms. Finished text is printed to the console.
* @param textfile file with original text
*/
public void improveGrammar(String textfile){
lR = new LineReader(textfile, " ");
wC.findFrequencies(textfile);
String[] line = lR.getNextLine();
while (line != null){
for (String word : line){
String stripWord = onlyAlphabet(word);
stripWord = stripWord.toLowerCase();
if (wC.getFrequency(stripWord) > limit) {
if (thesaurus.getSynonymFor(stripWord) == "") {
System.out.print(word + " ");
}
else {
String synonym = thesaurus.getSynonymFor(stripWord);
synonym = correctFormat(word, synonym);
System.out.print(synonym + " ");
}
}
else{
System.out.print(word + " ");
}
}
System.out.println();
line = lR.getNextLine();
}
}
/**
* Return the synonym in the correct format with punctuation
* @param word the original word
* @param newWord the synonym
* @return the synonym after being formatted
*/
private String correctFormat(String word, String newWord){
String ans = "";
int i =0;
Character c = word.charAt(i);
if (c.isLetter(c) && c.isUpperCase(c)){
ans += newWord.substring(0,1).toUpperCase() + newWord.substring(1);
}
else if (c.isLetter(c)){
ans += newWord;
}
// beginning punctuation
while (!c.isLetter(c)){
ans += c;
i++;
c = word.charAt(i);
if (c.isUpperCase(c)){
ans += newWord.substring(0,1).toUpperCase() + newWord.substring(1);
}
}
//ending punctuation
i = word.length() - 1;
c = word.charAt(i);
String endingPunc = "";
while(!c.isLetter(c)){
endingPunc += c;
i--;
c = word.charAt(i);
}
//reverse the ending punctuation we collected
for (int j = endingPunc.length() - 1; j >= 0; j--){
c = endingPunc.charAt(j);
ans += c;
}
return ans;
}
/**
* Get rid of non alphabetic charater in a string
* @param s the string
* @return the string with only alphabetic characters
*/
private String onlyAlphabet(String s){
return s.replaceAll("[^A-Za-z]", "");
}
}