Commit e03e6f08 by Hut

mehr analyse, refactoringcram

parent 3e2e0d64
...@@ -6,6 +6,7 @@ import java.util.LinkedList; ...@@ -6,6 +6,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
public class Builder { public class Builder {
...@@ -19,21 +20,37 @@ public class Builder { ...@@ -19,21 +20,37 @@ public class Builder {
} }
public Collection<Map.Entry<Prefix, Token>> random() { public Collection<Map.Entry<Prefix, Token>> random() {
List<Map.Entry<Prefix, Token>> result = new LinkedList<>(); return produce(p -> nextRandom(p));
Token[] initToken = new Token[prefix_length];
for (int i = 0; i < prefix_length; i++) {
initToken[i] = Token.START;
} }
Prefix p = new Prefix(initToken);
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<>();
Prefix p = initPrefix();
Token t = Token.START; Token t = Token.START;
while (t != Token.END) { for (int i = 0; i < 200; i++) {
t = nextRandom(p); if (t == Token.END)
break;
t = producer.apply(p);
result.add(new AbstractMap.SimpleEntry<Prefix, Token>(p, t)); result.add(new AbstractMap.SimpleEntry<Prefix, Token>(p, t));
p = p.slide(t); p = p.slide(t);
} }
return result; 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) { private Token nextRandom(Prefix prefix) {
return data.fetch(prefix).forRandom(nextRandomNumber()); return data.fetch(prefix).forRandom(nextRandomNumber());
} }
......
package directory.passive.markov; package directory.passive.markov;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -19,12 +20,20 @@ public class Data { ...@@ -19,12 +20,20 @@ public class Data {
return data.get(p); 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 @Override
public String toString() { public String toString() {
return "Data [data=" + data + "]"; return "Data [data=" + data + "]";
} }
public String dumpStats() { public String dumpStats() {
return "Total hits: " return "Total hits: "
+ data.entrySet().stream() + data.entrySet().stream()
......
...@@ -2,6 +2,7 @@ package directory.passive.markov; ...@@ -2,6 +2,7 @@ package directory.passive.markov;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.stream.Collectors;
// FIXME rename // FIXME rename
public class Lookup { public class Lookup {
...@@ -25,15 +26,15 @@ public class Lookup { ...@@ -25,15 +26,15 @@ public class Lookup {
return tokens.size(); return tokens.size();
} }
int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
public void add(Token t) { public void add(Token t) {
Integer i = tokens.getOrDefault(t, 0); Integer i = tokens.getOrDefault(t, 0);
tokens.put(t, i + 1); tokens.put(t, i + 1);
} }
public int getAmount(Token t) {
return tokens.getOrDefault(t, 0);
}
public Token forRandom(double random) { public Token forRandom(double random) {
int id = (int) Math.floor(random * getTotalCounts()); int id = (int) Math.floor(random * getTotalCounts());
int i = 0; int i = 0;
...@@ -49,9 +50,23 @@ public class Lookup { ...@@ -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 @Override
public String toString() { 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; ...@@ -3,14 +3,19 @@ package directory.passive.markov;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
int prefixLength = 1; int prefixLength = 2;
Data data = new Parser(prefixLength).parse(input); Data data = new Parser(prefixLength).parse(input);
Builder b = new Builder(prefixLength, data); 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++) { for (int i = 0; i < 25; i++) {
System.out.println(r.render(b.random())); 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" 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; ...@@ -4,14 +4,38 @@ import java.util.Collection;
import java.util.Map.Entry; import java.util.Map.Entry;
public class Renderer { 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 Data data;
private final Options options;
public Renderer(Data data) { public Renderer(Data data) {
super(); super();
this.data = data; 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) { public String render(Collection<Entry<Prefix, Token>> collection) {
double p = 1; double p = 1;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("KAI-uwe empfiehlt"); sb.append("KAI-uwe empfiehlt");
...@@ -20,6 +44,7 @@ public class Renderer { ...@@ -20,6 +44,7 @@ public class Renderer {
int possibilities = lookup.getDistinctTokens(); int possibilities = lookup.getDistinctTokens();
p *= (double) lookup.getAmount(t.getValue()) p *= (double) lookup.getAmount(t.getValue())
/ (double) lookup.getTotalCounts(); / (double) lookup.getTotalCounts();
if (options.possibilities) {
if (possibilities <= 1) { if (possibilities <= 1) {
sb.append("-"); sb.append("-");
...@@ -42,9 +67,12 @@ public class Renderer { ...@@ -42,9 +67,12 @@ public class Renderer {
} else { } else {
sb.append(" "); sb.append(" ");
} }
sb.append(t.getValue().render(""));
} }
sb.append(t.getValue().render(" "));
}
if (options.propability) {
sb.append(" - " + p); sb.append(" - " + p);
}
return sb.toString(); 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