Changeset 32533 in osm for applications/editors/josm/plugins/trustosm/src/tools/NameGenerator.java
- Timestamp:
- 2016-07-02T21:57:09+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/trustosm/src/tools/NameGenerator.java
r30742 r32533 1 // License: GPL. For details, see LICENSE file. 1 2 package tools; 2 3 … … 53 54 ArrayList<String> sur = new ArrayList<>(); 54 55 55 final private static char[] vocals = {'a', 'e', 'i', 'o', 'u', 'ä', 'ö', 'õ', 'ü', 'y'}; 56 final private static char[] consonants = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y'}; 56 private static final char[] vocals = { 57 'a', 'e', 'i', 'o', 'u', 'ä', 'ö', 'õ', 'ü', 'y'}; 58 private static final char[] consonants = { 59 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y'}; 57 60 58 61 private String fileName; … … 61 64 * Create new random name generator object. refresh() is automatically called. 62 65 * @param fileName insert file name, where syllables are located 63 * @throws IOException 64 */ 65 public NameGenerator(String fileName) throws IOException{ 66 */ 67 public NameGenerator(String fileName) throws IOException { 66 68 this.fileName = fileName; 67 69 refresh(); … … 71 73 * Change the file. refresh() is automatically called during the process. 72 74 * @param fileName insert the file name, where syllables are located. 73 * @throws IOException 74 */ 75 public void changeFile(String fileName) throws IOException{ 76 if(fileName == null) throw new IOException("File name cannot be null"); 75 */ 76 public void changeFile(String fileName) throws IOException { 77 if (fileName == null) throw new IOException("File name cannot be null"); 77 78 this.fileName = fileName; 78 79 refresh(); … … 82 83 * Refresh names from file. No need to call that method, if you are not changing the file during the operation of program, as this method 83 84 * is called every time file name is changed or new NameGenerator object created. 84 * @throws IOException 85 */ 86 public void refresh() throws IOException{ 85 */ 86 public void refresh() throws IOException { 87 87 try ( 88 FileReader input = new FileReader(fileName); 89 BufferedReader bufRead = new BufferedReader(input); 90 ) { 91 String line ="";88 FileReader input = new FileReader(fileName); 89 BufferedReader bufRead = new BufferedReader(input); 90 ) { 91 String line = ""; 92 92 93 93 while (line != null) { … … 98 98 } else if (line.charAt(0) == '+') { 99 99 sur.add(line.substring(1).toLowerCase()); 100 } else{ 100 } else { 101 101 mid.add(line.toLowerCase()); 102 102 } … … 107 107 108 108 private String upper(String s) { 109 return s.substring(0,1).toUpperCase().concat(s.substring(1)); 109 return s.substring(0, 1).toUpperCase().concat(s.substring(1)); 110 110 } 111 111 … … 117 117 } 118 118 119 private boolean containsVocFirst(ArrayList<String> array){ 120 for(String s: array){ 121 if(vocalFirst(s)) return true; 122 } 123 return false; 124 } 125 126 private boolean allowCons(ArrayList<String> array){ 127 for(String s: array){ 128 if(hatesPreviousVocals(s) || hatesPreviousConsonants(s) == false) return true; 129 } 130 return false; 131 } 132 133 private boolean allowVocs(ArrayList<String> array){ 134 for(String s: array){ 135 if(hatesPreviousConsonants(s) || hatesPreviousVocals(s) == false) return true; 136 } 137 return false; 138 } 139 140 private boolean expectsVocal(String s){ 141 if(s.substring(1).contains("+v")) return true; 142 else return false; 143 } 144 private boolean expectsConsonant(String s){ 145 if(s.substring(1).contains("+c")) return true; 146 else return false; 147 } 148 private boolean hatesPreviousVocals(String s){ 149 if(s.substring(1).contains("-c")) return true; 150 else return false; 151 } 152 private boolean hatesPreviousConsonants(String s){ 153 if(s.substring(1).contains("-v")) return true; 154 else return false; 155 } 156 157 private String pureSyl(String s){ 119 private boolean containsVocFirst(ArrayList<String> array) { 120 for (String s: array) { 121 if (vocalFirst(s)) return true; 122 } 123 return false; 124 } 125 126 private boolean allowCons(ArrayList<String> array) { 127 for (String s: array) { 128 if (hatesPreviousVocals(s) || hatesPreviousConsonants(s) == false) return true; 129 } 130 return false; 131 } 132 133 private boolean allowVocs(ArrayList<String> array) { 134 for (String s: array) { 135 if (hatesPreviousConsonants(s) || hatesPreviousVocals(s) == false) return true; 136 } 137 return false; 138 } 139 140 private boolean expectsVocal(String s) { 141 if (s.substring(1).contains("+v")) return true; 142 else return false; 143 } 144 145 private boolean expectsConsonant(String s) { 146 if (s.substring(1).contains("+c")) return true; 147 else return false; 148 } 149 150 private boolean hatesPreviousVocals(String s) { 151 if (s.substring(1).contains("-c")) return true; 152 else return false; 153 } 154 155 private boolean hatesPreviousConsonants(String s) { 156 if (s.substring(1).contains("-v")) return true; 157 else return false; 158 } 159 160 private String pureSyl(String s) { 158 161 s = s.trim(); 159 if(s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1); 162 if (s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1); 160 163 return s.split(" ")[0]; 161 164 } 162 165 163 private boolean vocalFirst(String s){ 166 private boolean vocalFirst(String s) { 164 167 return (String.copyValueOf(vocals).contains(String.valueOf(s.charAt(0)).toLowerCase())); 165 168 } 166 169 167 private boolean consonantFirst(String s){ 170 private boolean consonantFirst(String s) { 168 171 return (String.copyValueOf(consonants).contains(String.valueOf(s.charAt(0)).toLowerCase())); 169 172 } 170 173 171 private boolean vocalLast(String s){ 174 private boolean vocalLast(String s) { 172 175 return (String.copyValueOf(vocals).contains(String.valueOf(s.charAt(s.length()-1)).toLowerCase())); 173 176 } 174 177 175 private boolean consonantLast(String s){ 178 private boolean consonantLast(String s) { 176 179 return (String.copyValueOf(consonants).contains(String.valueOf(s.charAt(s.length()-1)).toLowerCase())); 177 180 } 178 181 182 // CHECKSTYLE.OFF: LineLength 179 183 180 184 /** … … 184 188 * @throws RuntimeException when logical mistakes are detected inside chosen file, and program is unable to complete the name. 185 189 */ 186 public String compose(int syls){ 187 if(syls > 2 && mid.size() == 0) throw new RuntimeException("You are trying to create a name with more than 3 parts, which requires middle parts, " + 188 "which you have none in the file "+fileName+". You should add some. Every word, which doesn't have + or - for a prefix is counted as a middle part."); 189 if(pre.size() == 0) throw new RuntimeException("You have no prefixes to start creating a name. add some and use \"-\" prefix, to identify it as a prefix for a name. (example: -asd)"); 190 if(sur.size() == 0) throw new RuntimeException("You have no suffixes to end a name. add some and use \"+\" prefix, to identify it as a suffix for a name. (example: +asd)"); 191 if(syls < 1) throw new RuntimeException("compose(int syls) can't have less than 1 syllable"); 190 public String compose(int syls) { 191 if (syls > 2 && mid.size() == 0) 192 throw new RuntimeException("You are trying to create a name with more than 3 parts, which requires middle parts, " + 193 "which you have none in the file "+fileName+". You should add some. Every word, which doesn't have + or - for a prefix is counted as a middle part."); 194 if (pre.size() == 0) 195 throw new RuntimeException("You have no prefixes to start creating a name. add some and use \"-\" prefix, to identify it as a prefix for a name. (example: -asd)"); 196 if (sur.size() == 0) 197 throw new RuntimeException("You have no suffixes to end a name. add some and use \"+\" prefix, to identify it as a suffix for a name. (example: +asd)"); 198 if (syls < 1) throw new RuntimeException("compose(int syls) can't have less than 1 syllable"); 192 199 int expecting = 0; // 1 for vocal, 2 for consonant 193 200 int last = 0; // 1 for vocal, 2 for consonant 194 201 String name; 195 int a = (int)(Math.random() * pre.size()); 196 197 if(vocalLast(pureSyl(pre.get(a)))) last = 1; 202 int a = (int) (Math.random() * pre.size()); 203 204 if (vocalLast(pureSyl(pre.get(a)))) last = 1; 198 205 else last = 2; 199 206 200 if(syls > 2){ 201 if(expectsVocal(pre.get(a))){ 207 if (syls > 2) { 208 if (expectsVocal(pre.get(a))) { 202 209 expecting = 1; 203 if(containsVocFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with vocal, " + 204 "but there is none. You should add one, or remove requirement for one.. "); 205 } 206 if(expectsConsonant(pre.get(a))){ 210 if (containsVocFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with vocal, " + 211 "but there is none. You should add one, or remove requirement for one.. "); 212 } 213 if (expectsConsonant(pre.get(a))) { 207 214 expecting = 2; 208 if(containsConsFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with consonant, " + 209 "but there is none. You should add one, or remove requirement for one.. "); 210 } 211 } 212 else{ 213 if(expectsVocal(pre.get(a))){ 215 if (containsConsFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with consonant, " + 216 "but there is none. You should add one, or remove requirement for one.. "); 217 } 218 } else { 219 if (expectsVocal(pre.get(a))) { 214 220 expecting = 1; 215 if(containsVocFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with vocal, " + 216 "but there is none. You should add one, or remove requirement for one.. "); 217 } 218 if(expectsConsonant(pre.get(a))){ 221 if (containsVocFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with vocal, " + 222 "but there is none. You should add one, or remove requirement for one.. "); 223 } 224 if (expectsConsonant(pre.get(a))) { 219 225 expecting = 2; 220 if(containsConsFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with consonant, " + 221 "but there is none. You should add one, or remove requirement for one.. "); 222 } 223 } 224 if(vocalLast(pureSyl(pre.get(a))) && allowVocs(mid) == false) throw new RuntimeException("Expecting \"middle\" part that allows last character of prefix to be a vocal, " + 226 if (containsConsFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with consonant, " + 227 "but there is none. You should add one, or remove requirement for one.. "); 228 } 229 } 230 if (vocalLast(pureSyl(pre.get(a))) && allowVocs(mid) == false) throw new RuntimeException("Expecting \"middle\" part that allows last character of prefix to be a vocal, " + 225 231 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the prefix used, was : \""+pre.get(a)+"\", which" + 226 "means there should be a part available, that has \"-v\" requirement or no requirements for previous syllables at all."); 227 228 if(consonantLast(pureSyl(pre.get(a))) && allowCons(mid) == false) throw new RuntimeException("Expecting \"middle\" part that allows last character of prefix to be a consonant, " + 232 "means there should be a part available, that has \"-v\" requirement or no requirements for previous syllables at all."); 233 234 if (consonantLast(pureSyl(pre.get(a))) && allowCons(mid) == false) throw new RuntimeException("Expecting \"middle\" part that allows last character of prefix to be a consonant, " + 229 235 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the prefix used, was : \""+pre.get(a)+"\", which" + 230 "means there should be a part available, that has \"-c\" requirement or no requirements for previous syllables at all."); 231 232 int b[]= new int[syls];233 for(int i = 0; i <b.length-2; i++){234 235 do{ 236 b[i] = (int)(Math.random() * mid.size()); 236 "means there should be a part available, that has \"-c\" requirement or no requirements for previous syllables at all."); 237 238 int[] b = new int[syls]; 239 for (int i = 0; i < b.length-2; i++) { 240 241 do { 242 b[i] = (int) (Math.random() * mid.size()); 237 243 //System.out.println("exp " +expecting+" vocalF:"+vocalFirst(mid.get(b[i]))+" syl: "+mid.get(b[i])); 238 } 239 while(expecting == 1 && vocalFirst(pureSyl(mid.get(b[i]))) == false || expecting == 2 && consonantFirst(pureSyl(mid.get(b[i]))) == false 244 } while (expecting == 1 && vocalFirst(pureSyl(mid.get(b[i]))) == false || expecting == 2 && consonantFirst(pureSyl(mid.get(b[i]))) == false 240 245 || last == 1 && hatesPreviousVocals(mid.get(b[i])) || last == 2 && hatesPreviousConsonants(mid.get(b[i]))); 241 246 242 247 expecting = 0; 243 if(expectsVocal(mid.get(b[i]))){ 248 if (expectsVocal(mid.get(b[i]))) { 244 249 expecting = 1; 245 if(i < b.length-3 && containsVocFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with vocal, " + 246 "but there is none. You should add one, or remove requirement for one.. "); 247 if(i == b.length-3 && containsVocFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with vocal, " + 248 "but there is none. You should add one, or remove requirement for one.. "); 249 } 250 if(expectsConsonant(mid.get(b[i]))){ 250 if (i < b.length-3 && containsVocFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with vocal, " + 251 "but there is none. You should add one, or remove requirement for one.. "); 252 if (i == b.length-3 && containsVocFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with vocal, " + 253 "but there is none. You should add one, or remove requirement for one.. "); 254 } 255 if (expectsConsonant(mid.get(b[i]))) { 251 256 expecting = 2; 252 if(i < b.length-3 && containsConsFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with consonant, " + 253 "but there is none. You should add one, or remove requirement for one.. "); 254 if(i == b.length-3 && containsConsFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with consonant, " + 255 "but there is none. You should add one, or remove requirement for one.. "); 256 } 257 if(vocalLast(pureSyl(mid.get(b[i]))) && allowVocs(mid) == false && syls > 3) throw new RuntimeException("Expecting \"middle\" part that allows last character of last syllable to be a vocal, " + 257 if (i < b.length-3 && containsConsFirst(mid) == false) throw new RuntimeException("Expecting \"middle\" part starting with consonant, " + 258 "but there is none. You should add one, or remove requirement for one.. "); 259 if (i == b.length-3 && containsConsFirst(sur) == false) throw new RuntimeException("Expecting \"suffix\" part starting with consonant, " + 260 "but there is none. You should add one, or remove requirement for one.. "); 261 } 262 if (vocalLast(pureSyl(mid.get(b[i]))) && allowVocs(mid) == false && syls > 3) throw new RuntimeException("Expecting \"middle\" part that allows last character of last syllable to be a vocal, " + 258 263 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the part used, was : \""+mid.get(b[i])+"\", which " + 259 "means there should be a part available, that has \"-v\" requirement or no requirements for previous syllables at all."); 260 261 if(consonantLast(pureSyl(mid.get(b[i]))) && allowCons(mid) == false && syls > 3) throw new RuntimeException("Expecting \"middle\" part that allows last character of last syllable to be a consonant, " + 264 "means there should be a part available, that has \"-v\" requirement or no requirements for previous syllables at all."); 265 266 if (consonantLast(pureSyl(mid.get(b[i]))) && allowCons(mid) == false && syls > 3) throw new RuntimeException("Expecting \"middle\" part that allows last character of last syllable to be a consonant, " + 262 267 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the part used, was : \""+mid.get(b[i])+"\", which " + 263 "means there should be a part available, that has \"-c\" requirement or no requirements for previous syllables at all."); 264 if(i == b.length-3){ 265 if(vocalLast(pureSyl(mid.get(b[i]))) && allowVocs(sur) == false) throw new RuntimeException("Expecting \"suffix\" part that allows last character of last syllable to be a vocal, " + 268 "means there should be a part available, that has \"-c\" requirement or no requirements for previous syllables at all."); 269 if (i == b.length-3) { 270 if (vocalLast(pureSyl(mid.get(b[i]))) && allowVocs(sur) == false) throw new RuntimeException("Expecting \"suffix\" part that allows last character of last syllable to be a vocal, " + 266 271 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the part used, was : \""+mid.get(b[i])+"\", which " + 267 "means there should be a suffix available, that has \"-v\" requirement or no requirements for previous syllables at all."); 268 269 if(consonantLast(pureSyl(mid.get(b[i]))) && allowCons(sur) == false) throw new RuntimeException("Expecting \"suffix\" part that allows last character of last syllable to be a consonant, " + 272 "means there should be a suffix available, that has \"-v\" requirement or no requirements for previous syllables at all."); 273 274 if (consonantLast(pureSyl(mid.get(b[i]))) && allowCons(sur) == false) throw new RuntimeException("Expecting \"suffix\" part that allows last character of last syllable to be a consonant, " + 270 275 "but there is none. You should add one, or remove requirements that cannot be fulfilled.. the part used, was : \""+mid.get(b[i])+"\", which " + 271 "means there should be a suffix available, that has \"-c\" requirement or no requirements for previous syllables at all."); 272 } 273 if(vocalLast(pureSyl(mid.get(b[i])))) last = 1; 276 "means there should be a suffix available, that has \"-c\" requirement or no requirements for previous syllables at all."); 277 } 278 if (vocalLast(pureSyl(mid.get(b[i])))) last = 1; 274 279 else last = 2; 275 280 } 276 281 277 282 int c; 278 do{ 279 c = (int)(Math.random() * sur.size()); 280 } 281 while(expecting == 1 && vocalFirst(pureSyl(sur.get(c))) == false || expecting == 2 && consonantFirst(pureSyl(sur.get(c))) == false 283 do { 284 c = (int) (Math.random() * sur.size()); 285 } while (expecting == 1 && vocalFirst(pureSyl(sur.get(c))) == false || expecting == 2 && consonantFirst(pureSyl(sur.get(c))) == false 282 286 || last == 1 && hatesPreviousVocals(sur.get(c)) || last == 2 && hatesPreviousConsonants(sur.get(c))); 283 287 284 288 name = upper(pureSyl(pre.get(a).toLowerCase())); 285 for(int i = 0; i <b.length-2; i++){289 for (int i = 0; i < b.length-2; i++) { 286 290 name = name.concat(pureSyl(mid.get(b[i]).toLowerCase())); 287 291 } 288 if(syls > 1) 292 if (syls > 1) 289 293 name = name.concat(pureSyl(sur.get(c).toLowerCase())); 290 294 return name; 291 295 } 296 // CHECKSTYLE.ON: LineLength 292 297 }
Note:
See TracChangeset
for help on using the changeset viewer.