-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharGenerator.java
More file actions
99 lines (89 loc) · 2.67 KB
/
Copy pathCharGenerator.java
File metadata and controls
99 lines (89 loc) · 2.67 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
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class CharGenerator {
String text;
int k;
int myRandomSeed;
Random r;
ArrayList<Character> charSeed = new ArrayList<Character>();
ArrayList<Character> chars = new ArrayList<Character>();
public CharGenerator(String text, int k){
this(text, k, -1);
}
public CharGenerator(String text, int k, int myRandomSeed){
this.text = text;
this.k = k;
this.myRandomSeed = myRandomSeed;
if(myRandomSeed!=-1) {
this.r = new Random(myRandomSeed);
}
else {
this.r = new Random();
}
buildChars();
buildSeed();
}
public char getNextChar(ArrayList<Character> nextsArray){
//TreeSet<Character> nextsSet = new TreeSet<Character>(nextsArray);
int selector = r.nextInt(nextsArray.size());
return nextsArray.get(selector);
}
public ArrayList<Character> getProbableNexts(){
String charString = arrayListToString(charSeed);
int matchIndex = text.indexOf(charString);
int firstMatchIndex = matchIndex;
ArrayList<Character> returnArray = new ArrayList<Character>();
do{ //look for next match
returnArray.add(chars.get(matchIndex+k));
//System.out.println(chars.get(matchIndex+k));
//System.out.println("getnextsloop");
matchIndex = text.indexOf(arrayListToString(charSeed), matchIndex+1);
}while(matchIndex != firstMatchIndex); //leave the loop when the first match is reached again
returnArray.remove(returnArray.size()-1);//'1' kept showing up as the last item in the array--why?
return returnArray;
}
public static Hashtable getFreqs(TreeSet<Character> defs, ArrayList<Character> data) {
Iterator iterator = defs.iterator();
Hashtable<Character, Float> frequencies = new Hashtable<Character, Float>();
while(iterator.hasNext()) {
char currentDef = (char) iterator.next();
float currentDefCount = 0;
for(int i = 0; i < data.size(); i++) {
if(data.get(i)==currentDef) {
currentDefCount++;
}
}
frequencies.put(currentDef, currentDefCount/data.size());
}
return frequencies;
}
// constructor methods
public void buildSeed(){
int seedStart = getSeedStart();
for(int i = seedStart; i < k+seedStart; i++){ //build seed
charSeed.add(chars.get(i));
}
}
public void buildChars(){
for(char c : text.toCharArray()){
chars.add(c);
}
}
public int getSeedStart(){
Random r = new Random();
if(myRandomSeed!=-1){
r = new Random(myRandomSeed);
}
return r.nextInt(chars.size());
}
public String arrayListToString(ArrayList<Character> chars){
String output = "";
for(int i = 0; i < chars.size(); i++){
output = output + chars.get(i);
}
return output;
}
}