-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
165 lines (123 loc) · 4.78 KB
/
Main.java
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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import netscape.javascript.JSObject;
public class Main {
public static void main(String[] args) {
// structure: pronoun + verb + word + pronoun + word + verb + word + verb * 40
// for normal visitor: 30 content
// pronoun 2-5
// verbs 3-8
// word 3-6
int proLimit = 8, verbLimit = 8, wordLimit = 6;
ArrayList<String> proList = getList(2, proLimit, "pronouns_");
ArrayList<String> verbList = getList(3, verbLimit, "verbs");
ArrayList<String> wordList = getList(3, wordLimit, "words_");
print("pronounce: " + proList.size());
print("verb: " + verbList.size());
print("word: " + wordList.size());
int i = 0;
while (i < 500) { // testing with 3
i++;
String content = buildParagraph(proList, verbList, wordList);
String source = "{\"value\":\"" + content + "\"}";
File fp = new File("contents/guests");
File file = new File(fp, i+".json");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter fw;
try {
fw = new FileWriter(file);
Scanner sc = new Scanner(source);
while (sc.hasNextLine()) {
String s = sc.nextLine();
fw.append(s);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String buildParagraph(ArrayList<String> pronouns, ArrayList<String> verbs,
ArrayList<String> words) {
StringBuilder paragraph = new StringBuilder();
// structure: pronoun + verb + word + pronoun + word + verb + word + verb * 40
for (int i = 0; i < 100; i++) {
paragraph.append(getRandomStringFromList(pronouns));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(verbs));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(words));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(pronouns));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(words));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(verbs));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(words));
paragraph.append(" ");
paragraph.append(getRandomStringFromList(verbs));
paragraph.append(" ");
}
return paragraph.toString();
}
private static String getRandomStringFromList(ArrayList<String> list) {
String s = "";
Random random = new Random();
int randomNumber = random.nextInt(list.size() - 1);
s = list.get(randomNumber);
if (randomNumber % 3 == 0 && randomNumber %5==0) {
return s;
}
return s.toLowerCase();
}
public static void print(Object x) {
System.out.println(x);
}
public static ArrayList<String> getList(int min, int limit, String file) {
ArrayList<String> list = new ArrayList<>();
// create pronouns file list
ArrayList<File> proFiles = new ArrayList<>();
for (int i = min; i <= limit; i++) {
String parent = file.replace("_", "");
proFiles.add(new File(parent, file + i + ".json"));
}
for (File f : proFiles) {
try (Scanner sc = new Scanner(f)) {
String fileContent = "";
while (sc.hasNextLine()) {
fileContent += sc.nextLine();
}
try {
JSONObject jo = (JSONObject) new JSONParser().parse(fileContent);
JSONArray ja = (JSONArray) jo.get("array");
for (int i = 0; i < ja.size(); i++) {
JSONObject job = (JSONObject) ja.get(i);
list.add((String) job.get("value"));
}
} catch (ParseException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return list;
}
}