-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoem.java
More file actions
186 lines (156 loc) · 7.91 KB
/
Copy pathPoem.java
File metadata and controls
186 lines (156 loc) · 7.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.ArrayList;
import java.util.Random;
public class Poem {
ArrayList<String> poem; // the poem in a list, one line for each item
String rhymepattern = "ABABCDCDEFEFGG"; // The rhyme pattern
String stresspattern = "1010101010"; // The stress pattern
ArrayList<Integer> usablelines; // The lines which can be used to check for rhymes
public static void main(String[] args) {
}
public Poem() {
poem = new ArrayList<String>();
}
public Poem(String wholepoem) { // Constructs a Poem object from a poem string
String[] poemlines = wholepoem.split("\n"); // Makes array seperated by line
poem = new ArrayList<String>();
for (int i = 0; i < poemlines.length; i++) {
poem.add(poemlines[i]);
}
}
public Poem takeOutLine(int index) { // Takes a random line out of the poem arraylist and replaces it with " "
Poem newpoem = new Poem();
for (int i = 0; i < this.poem.size(); i++) {
if (i == index) {
newpoem.poem.add(" ");
} else {
newpoem.poem.add(this.poem.get(i));
}
}
return newpoem;
}
public void printPoem() {
for (int i = 0; i < this.poem.size(); i++) {
System.out.println(this.poem.get(i));
}
}
public Boolean lineFits(String inputline, int linenumber) {
String inputstresspattern = "";
String[] stringsOnLineShakespeare = this.poem.get(linenumber).replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "")
.split(" ");
String[] stringsOnInputLine = inputline.toLowerCase().replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "").split(" ");
ArrayList<Word> wordsOnInputLine = new ArrayList<Word>();
for (int i = 0; i < stringsOnInputLine.length; i++) { // Make the inputline a list of words with cmupron info
Word currentword = Game.cmupron.get(stringsOnInputLine[i]);
if (currentword == null) {
System.out.println(stringsOnInputLine[i] + " Did not work as a real word!");
return false;
} else {
wordsOnInputLine.add(currentword);
}
}
for (int j = 0; j < wordsOnInputLine.size(); j++) { // find the Stresspattern of the inputline
for (int k = 0; k < wordsOnInputLine.get(j).stresses.size(); k++) {
inputstresspattern += wordsOnInputLine.get(j).stresses.get(k).toString();
}
}
Word lastWordShakespeare = Game.cmupron.get(stringsOnLineShakespeare[stringsOnLineShakespeare.length - 1]);
Word lastWordInputLine = wordsOnInputLine.get(wordsOnInputLine.size() - 1);
if (areCloseEnough(this.stresspattern, inputstresspattern)) {
System.out.println("The stress pattern was correct!");
if (this.checkOtherRhyme(linenumber, inputline) || lastWordInputLine.endsWith(lastWordShakespeare.getRhymeNeeds())) {
System.out.println("That line sure did fit in the rhyme scheme!");
return true;
} else {
System.out.println("That line did not rhyme!");
return false;
}
} else {
System.out.println(this.stresspattern + " did not work with " + inputstresspattern);
return false;
}
}
public Boolean lineRhymes(String inputline, int linenumber) { // Check if a line rhymes with the rest of the poem.
Word lastWordInputLine = Game.cmupron.get(getLastWord(inputline.toLowerCase().replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "")));
Word lastWordShakespeare = Game.cmupron.get(getLastWord(this.poem.get(linenumber).replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "")));
if (lastWordInputLine == null) {
System.out.println(getLastWord(inputline) + " Did not work as a real word!");
return false;
} else {
if (this.checkOtherRhyme(linenumber, inputline) || lastWordInputLine.endsWith(lastWordShakespeare.getRhymeNeeds())) {
return true;
} else return false;
}
}
public static String getLastWord(String line) {
String[] arrayofstrings = line.trim().toLowerCase().replaceAll("[)(\\[\\]!,.?{}:;\"\\'\\-]", "").split(" ");
return arrayofstrings[arrayofstrings.length - 1];
}
public String toString() {
return this.poem.toString() + "\n" + this.rhymepattern + " " + this.stresspattern + this.usablelines;
}
public Boolean areCloseEnough(String stresspattern1, String stresspattern2) { // The stress pattern is close enough
// if it is the same length and it has
// at least 4 matching stresses
int sumofmatchingsyllables = 0;
if (stresspattern1.length() != stresspattern2.length()) {
return false;
}
for (int i = 0; i < stresspattern1.length(); i++) {
if (stresspattern1.charAt(i) == stresspattern2.charAt(i))
sumofmatchingsyllables += 1;
}
if (sumofmatchingsyllables > 3) {
return true;
} else {
return false;
}
}
public Boolean checkOtherRhyme(int linenumber, String line) { // Check if a certain line fits in with the other
// lines in
// the poem i.e. rhymes with another B or A, etc. rhyme
String lastWord = getLastWord(line);
Word theLastWord = Game.cmupron.get(lastWord);
char rhymeType = this.rhymepattern.charAt(linenumber);
int sameRhymeTypeIndex = 0;
for (int i = 0; i < this.rhymepattern.length(); i++) { // Finds the line number of the other line the inputline
// needs to rhyme with
if (i == linenumber) {
continue;
}
if (rhymeType == rhymepattern.charAt(i)) {
sameRhymeTypeIndex = i;
}
}
if (Game.cmupron.containsKey(getLastWord(this.poem.get(sameRhymeTypeIndex)))) { // Checks if we can check that
// other line
Word otherRhymeWord = Game.cmupron.get(getLastWord(this.poem.get(sameRhymeTypeIndex)));
if (theLastWord.endsWith(otherRhymeWord.getRhymeNeeds())) { // Checks if the input line's last word rhymes
// with the other line's last word
return true;
} else {
return false;
}
} else {
return false; // If the other line is unusable then it will return false
}
}
public Poem replaceLastWordOfLineWith(int linenumber, String word) { // Takes out the last word of the line of a poem at the given linenumber and replaces it with ____ and returns new poem
Poem gamepoem = new Poem(); // Make new poem
String line = this.poem.get(linenumber); // Gets original line
String[] linearray = line.split(" "); // makes an array
String newline = ""; // Instantiate the newline string
for (int i = 0; i < linearray.length - 1; i++) { // Runs for every word except the last one
newline += linearray[i] + " ";
}
newline += word; // adds blank instead of last word
for (int i = 0; i < this.poem.size(); i++) { // adds the orginal lines to the new poem, except for the modified line
if (i == linenumber) {
gamepoem.poem.add(newline);
} else {
gamepoem.poem.add(this.poem.get(i));
}
}
gamepoem.usablelines = this.usablelines;
return gamepoem;
}
}