-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicDocument.java
More file actions
121 lines (112 loc) · 4.59 KB
/
Copy pathBasicDocument.java
File metadata and controls
121 lines (112 loc) · 4.59 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
package document;
import java.util.List;
/**
* A naive implementation of the Document abstract class.
* @author UC San Diego Intermediate Programming MOOC team
*/
public class BasicDocument extends Document
{
/** Create a new BasicDocument object
*
* @param text The full text of the Document.
*/
public BasicDocument(String text)
{
super(text);
}
/**
* 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 should process the entire text string each time it is called.
*
* @return The number of words in the document.
*/
@Override
public int getNumWords()
{
// Implement this method in week 1 according to the comments above.
// See the Module 1 support videos if you need help.
return getTokens("[a-zA-Z]+").size();
}
/**
* 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 should process the entire text string each time it is called.
*
* @return The number of sentences in the document.
*/
@Override
public int getNumSentences()
{
//Implement this method. See the Module 1 support videos
// if you need help.
return getTokens("[^.!?]+").size();
}
/**
* Get the total number of syllables in the document (the stored text).
* To count 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 should process the entire text string each time it is called.
*
* @return The number of syllables in the document.
*/
@Override
public int getNumSyllables()
{
// Implement this method in week 1. See the Module 1 support videos
// if you need help. And note that there is no need to use a regular
// expression for the syllable counting. We recommend you implement
// the helper function countSyllables in Document.java using a loop,
// and then call it here on each word.
int syllables = 0;
List<String> words = getTokens("[a-zA-Z]+");
for (String word : words) {
syllables += countSyllables(word);
}
return syllables;
}
/* The main method for testing this class.
* You are encouraged to add your own tests. */
public static void main(String[] args)
{
/* Each of the test cases below uses the method testCase. The first
* argument to testCase is a Document object, created with the string shown.
* The next three arguments are the number of syllables, words and sentences
* in the string, respectively. You can use these examples to help clarify
* your understanding of how to count syllables, words, and sentences.
*/
testCase(new BasicDocument("This is a test. How many??? "
+ "Senteeeeeeeeeences are here... there should be 5! Right?"),
16, 13, 5);
testCase(new BasicDocument(""), 0, 0, 0);
testCase(new BasicDocument("sentence, with, lots, of, commas.! "
+ "(And some poaren)). The output is: 7.5."), 15, 11, 4);
testCase(new BasicDocument("many??? Senteeeeeeeeeences are"), 6, 3, 2);
testCase(new BasicDocument("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 BasicDocument("Segue"), 2, 1, 1);
testCase(new BasicDocument("Sentence"), 2, 1, 1);
testCase(new BasicDocument("Sentences?!"), 3, 1, 1);
testCase(new BasicDocument("Lorem ipsum dolor sit amet, qui ex choro quodsi moderatius, nam dolores explicari forensibus ad."),
32, 15, 1);
}
}