Commit e03e6f08 by Hut

mehr analyse, refactoringcram

parent 3e2e0d64
......@@ -6,9 +6,10 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
public class Builder {
private final int prefix_length;
private final Data data;
......@@ -19,21 +20,37 @@ public class Builder {
}
public Collection<Map.Entry<Prefix, Token>> random() {
return produce(p -> nextRandom(p));
}
public Collection<Map.Entry<Prefix, Token>> average() {
return produce(p -> data.fetch(p).average());
}
private Collection<Map.Entry<Prefix, Token>> produce(
Function<Prefix, Token> producer) {
List<Map.Entry<Prefix, Token>> result = new LinkedList<>();
Token[] initToken = new Token[prefix_length];
for (int i = 0; i < prefix_length; i++) {
initToken[i] = Token.START;
}
Prefix p = new Prefix(initToken);
Prefix p = initPrefix();
Token t = Token.START;
while (t != Token.END) {
t = nextRandom(p);
for (int i = 0; i < 200; i++) {
if (t == Token.END)
break;
t = producer.apply(p);
result.add(new AbstractMap.SimpleEntry<Prefix, Token>(p, t));
p = p.slide(t);
}
return result;
}
private Prefix initPrefix() {
Token[] initToken = new Token[prefix_length];
for (int i = 0; i < prefix_length; i++) {
initToken[i] = Token.START;
}
Prefix p = new Prefix(initToken);
return p;
}
private Token nextRandom(Prefix prefix) {
return data.fetch(prefix).forRandom(nextRandomNumber());
}
......
package directory.passive.markov;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -19,12 +20,20 @@ public class Data {
return data.get(p);
}
public Lookup info(Token... t) {
return data.get(new Prefix(t));
}
public Lookup info(String... s) {
return data.get(new Prefix(Arrays.stream(s).map(t -> new Token(t))
.collect(Collectors.toList())));
}
@Override
public String toString() {
return "Data [data=" + data + "]";
}
public String dumpStats() {
return "Total hits: "
+ data.entrySet().stream()
......
......@@ -2,6 +2,7 @@ package directory.passive.markov;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.stream.Collectors;
// FIXME rename
public class Lookup {
......@@ -25,15 +26,15 @@ public class Lookup {
return tokens.size();
}
int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1);
}
public int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
public Token forRandom(double random) {
int id = (int) Math.floor(random * getTotalCounts());
int i = 0;
......@@ -49,9 +50,23 @@ public class Lookup {
}
public Token average() {
return tokens
.entrySet().stream().sorted((e1, e2) -> Integer
.compare(e2.getValue(), e1.getValue()))
.findFirst().get().getKey();
}
@Override
public String toString() {
return "Lookup [tokens=" + tokens + "]";
return "Lookup [tokens= "
+ tokens.entrySet().stream()
.sorted((e1, e2) -> Integer.compare(e2.getValue(),
e1.getValue()))
.map(e -> String.format("%d*%s", e.getValue(),
e.getKey()))
.collect(Collectors.joining(", "))
+ "]";
}
}
......@@ -3,14 +3,19 @@ package directory.passive.markov;
public class Main {
public static void main(String[] args) {
int prefixLength = 1;
int prefixLength = 2;
Data data = new Parser(prefixLength).parse(input);
Builder b = new Builder(prefixLength, data);
Renderer r = new Renderer(data);
Renderer r = new Renderer(data, Renderer.Options.FULL);
for (int i = 0; i < 25; i++) {
System.out.println(r.render(b.random()));
}
System.out.println(data.dumpStats());
// Collection<Entry<Prefix, Token>> a = b.average();
// System.out.println("average: " + r.render(a));
// for (Entry<Prefix, Token> e : a) {
// System.out.println(e.getKey() + " -> " + data.fetch(e.getKey()));
// }
// System.out.println(data.dumpStats());
}
private static final String input = "Kai☺UWE empfiehlt \"langsam ist Präzise und Präzise ist schnell\" zu verinnerlichen.\n"
......
......@@ -4,14 +4,38 @@ import java.util.Collection;
import java.util.Map.Entry;
public class Renderer {
public static class Options {
private final boolean propability;
private final boolean possibilities;
public Options(boolean propability, boolean possibilities) {
super();
this.propability = propability;
this.possibilities = possibilities;
}
public static final Options NONE = new Options(false, false);
public static final Options FULL = new Options(true, true);
}
private final Data data;
private final Options options;
public Renderer(Data data) {
super();
this.data = data;
this.options = Options.NONE;
}
public Renderer(Data data, Options options) {
super();
this.data = data;
this.options = options;
}
public String render(Collection<Entry<Prefix, Token>> collection) {
double p = 1;
StringBuilder sb = new StringBuilder();
sb.append("KAI-uwe empfiehlt");
......@@ -20,31 +44,35 @@ public class Renderer {
int possibilities = lookup.getDistinctTokens();
p *= (double) lookup.getAmount(t.getValue())
/ (double) lookup.getTotalCounts();
if (options.possibilities) {
if (possibilities <= 1) {
sb.append("-");
} else if (possibilities <= 2) {
sb.append("--");
} else if (possibilities <= 3) {
sb.append("---");
} else if (possibilities <= 5) {
sb.append("_");
} else if (possibilities <= 10) {
sb.append("__");
} else if (possibilities <= 20) {
sb.append("___");
} else if (possibilities <= 50) {
sb.append("*");
} else if (possibilities <= 100) {
sb.append("**");
} else if (possibilities <= 200) {
sb.append("***");
} else {
sb.append(" ");
if (possibilities <= 1) {
sb.append("-");
} else if (possibilities <= 2) {
sb.append("--");
} else if (possibilities <= 3) {
sb.append("---");
} else if (possibilities <= 5) {
sb.append("_");
} else if (possibilities <= 10) {
sb.append("__");
} else if (possibilities <= 20) {
sb.append("___");
} else if (possibilities <= 50) {
sb.append("*");
} else if (possibilities <= 100) {
sb.append("**");
} else if (possibilities <= 200) {
sb.append("***");
} else {
sb.append(" ");
}
}
sb.append(t.getValue().render(""));
sb.append(t.getValue().render(" "));
}
if (options.propability) {
sb.append(" - " + p);
}
sb.append(" - " + p);
return sb.toString();
}
......
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