-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEfficientDocument.java
More file actions
162 lines (143 loc) · 5.9 KB
/
Copy pathEfficientDocument.java
File metadata and controls
162 lines (143 loc) · 5.9 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package document;
import java.util.List;
/**
* A class that represents a text document
* It does one pass through the document to count the number of syllables, words,
* and sentences and then stores those values.
*
* @author UC San Diego Intermediate Programming MOOC team
*/
public class EfficientDocument extends Document {
private int numWords; // The number of words in the document
private int numSentences; // The number of sentences in the document
private int numSyllables; // The number of syllables in the document
public EfficientDocument(String text)
{
super(text);
processText();
}
/**
* Take a string that either contains only alphabetic characters,
* or only sentence-ending punctuation. Return true if the string
* contains only alphabetic characters, and false if it contains
* end of sentence punctuation.
*
* @param tok The string to check
* @return true if tok is a word, false if it is punctuation.
*/
private boolean isWord(String tok)
{
// Note: This is a fast way of checking whether a string is a word
// You probably don't want to change it.
return !(tok.indexOf("!") >=0 || tok.indexOf(".") >=0 || tok.indexOf("?")>=0);
}
/** Passes through the text one time to count the number of words, syllables
* and sentences, and set the member variables appropriately.
* Words, sentences and syllables are defined as described below.
*/
private void processText()
{
// Call getTokens on the text to preserve separate strings that are
// either words or sentence-ending punctuation. Ignore everything
// That is not a word or a sentence-ending puctuation.
// MAKE SURE YOU UNDERSTAND THIS LINE BEFORE YOU CODE THE REST
// OF THIS METHOD.
List<String> tokens = getTokens("[!?.]+|[a-zA-Z]+");
// Finish this method. Remember the countSyllables method from
// Document. That will come in handy here. isWord defined above will also help.
numWords = 0;
numSentences = 0;
numSyllables = 0;
for (String str : tokens) {
if (!isWord(str)) {
numSentences++;
} else {
numWords ++;
numSyllables += countSyllables(str);
}
}
if (tokens.size() == 1) {
numSentences = isWord(tokens.get(0)) ? 1 : 0;
} else if (tokens.size() > 1) {
numSentences += isWord(tokens.get(tokens.size()-1)) ? 1 : 0;
}
}
/**
* Get the number of sentences in the document.
* Sentences are defined as contiguous strings of characters ending in an
* end of sentence punctuation (. ! or ?) or the last contiguous set of
* characters in the document, even if they don't end with a punctuation mark.
*
* Check the examples in the main method below for more information.
*
* This method does NOT process the whole text each time it is called.
* It returns information already stored in the EfficientDocument object.
*
* @return The number of sentences in the document.
*/
@Override
public int getNumSentences() {
// write this method. Hint: It's simple
return numSentences;
}
/**
* Get the number of words in the document.
* A "word" is defined as a contiguous string of alphabetic characters
* i.e. any upper or lower case characters a-z or A-Z. This method completely
* ignores numbers when you count words, and assumes that the document does not have
* any strings that combine numbers and letters.
*
* Check the examples in the main method below for more information.
*
* This method does NOT process the whole text each time it is called.
* It returns information already stored in the EfficientDocument object.
*
* @return The number of words in the document.
*/
@Override
public int getNumWords() {
//TODO: write this method. Hint: It's simple
return numWords;
}
/**
* Get the total number of syllables in the document (the stored text).
* To calculate the the number of syllables in a word, it uses the following rules:
* Each contiguous sequence of one or more vowels is a syllable,
* with the following exception: a lone "e" at the end of a word
* is not considered a syllable unless the word has no other syllables.
* You should consider y a vowel.
*
* Check the examples in the main method below for more information.
*
* This method does NOT process the whole text each time it is called.
* It returns information already stored in the EfficientDocument object.
*
* @return The number of syllables in the document.
*/
@Override
public int getNumSyllables() {
//TODO: write this method. Hint: It's simple
return numSyllables;
}
// Can be used for testing
// We encourage you to add your own tests here.
public static void main(String[] args)
{
testCase(new EfficientDocument("This is a test. How many??? "
+ "Senteeeeeeeeeences are here... there should be 5! Right?"),
16, 13, 5);
testCase(new EfficientDocument(""), 0, 0, 0);
testCase(new EfficientDocument("sentence, with, lots, of, commas.! "
+ "(And some poaren)). The output is: 7.5."), 15, 11, 4);
testCase(new EfficientDocument("many??? Senteeeeeeeeeences are"), 6, 3, 2);
testCase(new EfficientDocument("Here is a series of test sentences. Your program should "
+ "find 3 sentences, 33 words, and 49 syllables. Not every word will have "
+ "the correct amount of syllables (example, for example), "
+ "but most of them will."), 49, 33, 3);
testCase(new EfficientDocument("Segue"), 2, 1, 1);
testCase(new EfficientDocument("Sentence"), 2, 1, 1);
testCase(new EfficientDocument("Sentences?!"), 3, 1, 1);
testCase(new EfficientDocument("Lorem ipsum dolor sit amet, qui ex choro quodsi moderatius, nam dolores explicari forensibus ad."),
32, 15, 1);
}
}