Commit 273a6bcf by Hut

first working version

parents
package directory.passive.markov;
import java.util.HashMap;
import java.util.Map;
// FIXME rename
public class Data {
private final Map<Prefix, Lookup> data = new HashMap<>();
public void add(Prefix p, Token t) {
Lookup l = data.getOrDefault(p, new Lookup());
l.add(t);
data.put(p, l);
}
public Lookup fetch(Prefix p) {
return data.getOrDefault(p, Lookup.empty);
}
@Override
public String toString() {
return "Data [data=" + data + "]";
}
}
package directory.passive.markov;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
// FIXME rename
public class Lookup {
public static final Lookup empty = new Lookup() {
public Token forRandom(double random) {
return Token.EMPTY;
}
};
public Lookup() {
}
private final LinkedHashMap<Token, Integer> tokens = new LinkedHashMap<Token, Integer>();
private int getTotalCounts() {
return tokens.values().stream().mapToInt(i -> i.intValue()).sum();
}
public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1);
}
public Token forRandom(double random) {
int id = (int) Math.floor(random * getTotalCounts());
int i = 0;
for (Entry<Token, Integer> entry : tokens.entrySet()) {
i += entry.getValue();
if (id < i) {
return entry.getKey();
}
}
throw new IllegalStateException(String.format(
"failed to find a random token with seed %d within %s", id,
this));
}
@Override
public String toString() {
return "Lookup [tokens=" + tokens + "]";
}
}
package directory.passive.markov;
import java.util.Arrays;
// FIXME rename
public class Prefix {
private final Token[] tokens;
public Prefix(Token[] tokens) {
this.tokens = tokens;
}
@Override
public String toString() {
return "Prefix [tokens=" + Arrays.toString(tokens) + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(tokens);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Prefix other = (Prefix) obj;
if (!Arrays.equals(tokens, other.tokens))
return false;
return true;
}
}
package directory.passive.markov;
public class Token {
public static final Token START = new SpecialToken();
public static final Token END = new SpecialToken();
protected static final Token EMPTY = new SpecialToken();
public Token(String content) {
super();
this.content = content;
}
String content;
@Override
public String toString() {
if (this == END) {
return "Token <END>";
}
if (this == START) {
return "TOKEN <START>";
}
if (this == EMPTY)
return "TOKEN <empty>";
return "Token [content=" + content + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((content == null) ? 0 : content.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null)
return false;
if (this == START)
return false;
if (this == END)
return false;
if (this == EMPTY)
return false;
if (getClass() != obj.getClass())
return false;
Token other = (Token) obj;
if (content == null) {
if (other.content != null)
return false;
} else if (!content.equals(other.content))
return false;
return true;
}
public String render(String prefix) {
return prefix + content;
}
private static class SpecialToken extends Token {
public SpecialToken() {
super("");
}
@Override
public String render(String prefix) {
return "";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment