source: osm/applications/editors/josm/plugins/trustosm/src/tools/NameGenerator.java@ 30742

Last change on this file since 30742 was 30742, checked in by donvip, 11 years ago

[josm_trustosm] fix compilation errors

File size: 15.1 KB
Line 
1package tools;
2
3import java.io.BufferedReader;
4import java.io.FileReader;
5import java.io.IOException;
6import java.util.ArrayList;
7
8/**
9 * This class is released under GNU general public license
10 *
11 * Description: This class generates random names from syllables, and provides programmer a
12 * simple way to set a group of rules for generator to avoid unpronounceable and bizarre names.
13 *
14 * SYLLABLE FILE REQUIREMENTS/FORMAT:
15 * 1) all syllables are separated by line break.
16 * 2) Syllable should not contain or start with whitespace, as this character is ignored and only first part of the syllable is read.
17 * 3) + and - characters are used to set rules, and using them in other way, may result in unpredictable results.
18 * 4) Empty lines are ignored.
19 *
20 * SYLLABLE CLASSIFICATION:
21 * Name is usually composed from 3 different class of syllables, which include prefix, middle part and suffix.
22 * To declare syllable as a prefix in the file, insert "-" as a first character of the line.
23 * To declare syllable as a suffix in the file, insert "+" as a first character of the line.
24 * everything else is read as a middle part.
25 *
26 * NUMBER OF SYLLABLES:
27 * Names may have any positive number of syllables. In case of 2 syllables, name will be composed from prefix and suffix.
28 * In case of 1 syllable, name will be chosen from amongst the prefixes.
29 * In case of 3 and more syllables, name will begin with prefix, is filled with middle parts and ended with suffix.
30 *
31 * ASSIGNING RULES:
32 * I included a way to set 4 kind of rules for every syllable. To add rules to the syllables, write them right after the
33 * syllable and SEPARATE WITH WHITESPACE. (example: "aad +v -c"). The order of rules is not important.
34 *
35 * RULES:
36 * 1) +v means that next syllable must definitely start with a vocal.
37 * 2) +c means that next syllable must definitely start with a consonant.
38 * 3) -v means that this syllable can only be added to another syllable, that ends with a vocal.
39 * 4) -c means that this syllable can only be added to another syllable, that ends with a consonant.
40 * So, our example: "aad +v -c" means that "aad" can only be after consonant and next syllable must start with vocal.
41 * Beware of creating logical mistakes, like providing only syllables ending with consonants, but expecting only vocals, which will be detected
42 * and RuntimeException will be thrown.
43 *
44 * TO START:
45 * Create a new NameGenerator object, provide the syllable file, and create names using compose() method.
46 *
47 * @author Joonas Vali, August 2009.
48 *
49 */
50public class NameGenerator {
51 ArrayList<String> pre = new ArrayList<>();
52 ArrayList<String> mid = new ArrayList<>();
53 ArrayList<String> sur = new ArrayList<>();
54
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'};
57
58 private String fileName;
59
60 /**
61 * Create new random name generator object. refresh() is automatically called.
62 * @param fileName insert file name, where syllables are located
63 * @throws IOException
64 */
65 public NameGenerator(String fileName) throws IOException{
66 this.fileName = fileName;
67 refresh();
68 }
69
70 /**
71 * Change the file. refresh() is automatically called during the process.
72 * @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");
77 this.fileName = fileName;
78 refresh();
79 }
80
81 /**
82 * 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 * is called every time file name is changed or new NameGenerator object created.
84 * @throws IOException
85 */
86 public void refresh() throws IOException{
87 try (
88 FileReader input = new FileReader(fileName);
89 BufferedReader bufRead = new BufferedReader(input);
90 ) {
91 String line="";
92
93 while (line != null) {
94 line = bufRead.readLine();
95 if (line != null && !line.equals("")) {
96 if (line.charAt(0) == '-') {
97 pre.add(line.substring(1).toLowerCase());
98 } else if (line.charAt(0) == '+') {
99 sur.add(line.substring(1).toLowerCase());
100 } else{
101 mid.add(line.toLowerCase());
102 }
103 }
104 }
105 }
106 }
107
108 private String upper(String s) {
109 return s.substring(0,1).toUpperCase().concat(s.substring(1));
110 }
111
112 private boolean containsConsFirst(ArrayList<String> array) {
113 for (String s: array) {
114 if (consonantFirst(s)) return true;
115 }
116 return false;
117 }
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){
158 s = s.trim();
159 if(s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1);
160 return s.split(" ")[0];
161 }
162
163 private boolean vocalFirst(String s){
164 return (String.copyValueOf(vocals).contains(String.valueOf(s.charAt(0)).toLowerCase()));
165 }
166
167 private boolean consonantFirst(String s){
168 return (String.copyValueOf(consonants).contains(String.valueOf(s.charAt(0)).toLowerCase()));
169 }
170
171 private boolean vocalLast(String s){
172 return (String.copyValueOf(vocals).contains(String.valueOf(s.charAt(s.length()-1)).toLowerCase()));
173 }
174
175 private boolean consonantLast(String s){
176 return (String.copyValueOf(consonants).contains(String.valueOf(s.charAt(s.length()-1)).toLowerCase()));
177 }
178
179
180 /**
181 * Compose a new name.
182 * @param syls The number of syllables used in name.
183 * @return Returns composed name as a String
184 * @throws RuntimeException when logical mistakes are detected inside chosen file, and program is unable to complete the name.
185 */
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");
192 int expecting = 0; // 1 for vocal, 2 for consonant
193 int last = 0; // 1 for vocal, 2 for consonant
194 String name;
195 int a = (int)(Math.random() * pre.size());
196
197 if(vocalLast(pureSyl(pre.get(a)))) last = 1;
198 else last = 2;
199
200 if(syls > 2){
201 if(expectsVocal(pre.get(a))){
202 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))){
207 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))){
214 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))){
219 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, " +
225 "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, " +
229 "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());
237 //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
240 || last == 1 && hatesPreviousVocals(mid.get(b[i])) || last == 2 && hatesPreviousConsonants(mid.get(b[i])));
241
242 expecting = 0;
243 if(expectsVocal(mid.get(b[i]))){
244 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]))){
251 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, " +
258 "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, " +
262 "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, " +
266 "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, " +
270 "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;
274 else last = 2;
275 }
276
277 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
282 || last == 1 && hatesPreviousVocals(sur.get(c)) || last == 2 && hatesPreviousConsonants(sur.get(c)));
283
284 name = upper(pureSyl(pre.get(a).toLowerCase()));
285 for(int i = 0; i<b.length-2; i++){
286 name = name.concat(pureSyl(mid.get(b[i]).toLowerCase()));
287 }
288 if(syls > 1)
289 name = name.concat(pureSyl(sur.get(c).toLowerCase()));
290 return name;
291 }
292}
Note: See TracBrowser for help on using the repository browser.