Commit 67d038d8 by Hut

Moved propability calculation to finish step.

parent 218bdd37
...@@ -4,11 +4,17 @@ public class Decission { ...@@ -4,11 +4,17 @@ public class Decission {
private final Token token; private final Token token;
private final int id; private final int id;
private final Lookup lookup; private final Lookup lookup;
private final double propability;
public Decission(Token token, int id, Lookup lookup) { public Decission(Token token, int id, Lookup lookup, double propability) {
this.token = token; this.token = token;
this.id = id; this.id = id;
this.lookup = lookup; this.lookup = lookup;
this.propability = propability;
}
public double getPropability() {
return propability;
} }
// public Decission(Token key, int id) { // public Decission(Token key, int id) {
...@@ -16,7 +22,6 @@ public class Decission { ...@@ -16,7 +22,6 @@ public class Decission {
// this.id = id; // this.id = id;
// } // }
public Lookup getLookup() { public Lookup getLookup() {
return lookup; return lookup;
} }
......
...@@ -18,17 +18,16 @@ public class Lookup implements Serializable { ...@@ -18,17 +18,16 @@ public class Lookup implements Serializable {
private int totalCounts = 0; private int totalCounts = 0;
int getTotalCounts() {
return totalCounts;
}
int getDistinctTokens() { int getDistinctTokens() {
return tokens.size();
finishGuard();
return finalData.length;
} }
int getAmount(Token t) { private void finishGuard() {
return tokens.getOrDefault(t, 0); if (!isFinishedCollecting) {
throw new IllegalStateException("not jet finished");
}
} }
public void add(Token t) { public void add(Token t) {
...@@ -41,9 +40,7 @@ public class Lookup implements Serializable { ...@@ -41,9 +40,7 @@ public class Lookup implements Serializable {
} }
public Decission forRandom(double random) { public Decission forRandom(double random) {
if (!isFinishedCollecting) { finishGuard();
throw new IllegalStateException("not jet finished");
}
if (random < 0 || random >= 1) { if (random < 0 || random >= 1) {
throw new IllegalArgumentException("expected double [0; 1)"); throw new IllegalArgumentException("expected double [0; 1)");
} }
...@@ -54,9 +51,7 @@ public class Lookup implements Serializable { ...@@ -54,9 +51,7 @@ public class Lookup implements Serializable {
} }
public Decission forId(int id) { public Decission forId(int id) {
if (!isFinishedCollecting) { finishGuard();
throw new IllegalStateException("not jet finished");
}
if (id < 0 || id > finishedSums.length) { if (id < 0 || id > finishedSums.length) {
throw new IllegalArgumentException(String.format("got %d but expected id [0; %d)", throw new IllegalArgumentException(String.format("got %d but expected id [0; %d)",
id, id,
...@@ -66,9 +61,7 @@ public class Lookup implements Serializable { ...@@ -66,9 +61,7 @@ public class Lookup implements Serializable {
} }
public Decission average() { public Decission average() {
if (!isFinishedCollecting) { finishGuard();
throw new IllegalStateException("not jet finished");
}
return finalData[this.finishedSums.length - 1]; return finalData[this.finishedSums.length - 1];
} }
...@@ -91,13 +84,20 @@ public class Lookup implements Serializable { ...@@ -91,13 +84,20 @@ public class Lookup implements Serializable {
Entry<Token, Integer> entry = orderedEntries.get(i); Entry<Token, Integer> entry = orderedEntries.get(i);
sum += entry.getValue(); sum += entry.getValue();
finishedSums[i] = sum; finishedSums[i] = sum;
finalData[i] = new Decission(entry.getKey(), i, this); finalData[i] = new Decission(entry.getKey(),
i,
this,
entry.getValue() / getTotalCounts());
} }
this.tokens.clear(); this.tokens.clear();
isFinishedCollecting = true; isFinishedCollecting = true;
} }
} }
int getTotalCounts() {
return totalCounts;
}
@Override @Override
public String toString() { public String toString() {
return "Lookup [tokens= " + tokens.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2 return "Lookup [tokens= " + tokens.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2
......
...@@ -22,15 +22,14 @@ public class Sentence implements Iterable<Decission>, Serializable { ...@@ -22,15 +22,14 @@ public class Sentence implements Iterable<Decission>, Serializable {
this.decissions.add(decission); this.decissions.add(decission);
} }
public Stream<Decission> asStream() { public double propability() {
return decissions.stream(); return asStream().mapToDouble(Decission::getPropability)
.reduce((d1, d2) -> d1 * d2)
.getAsDouble();
} }
public double propability() { public Stream<Decission> asStream() {
return asStream() return decissions.stream();
.mapToDouble(d -> (double) d.getLookup().getAmount(d.getToken())
/ (double) d.getLookup().getTotalCounts())
.reduce((d1, d2) -> d1 * d2).getAsDouble();
} }
@Override @Override
......
...@@ -86,44 +86,34 @@ public class LookupTests { ...@@ -86,44 +86,34 @@ public class LookupTests {
@Test @Test
public void allPossible() { public void allPossible() {
assertIterableEquals(lookup.allPossible(), Arrays.asList(new Token("a"))); assertIterableEquals(Arrays.asList(new Token("a")), lookup.allPossible());
lookup.add(new Token("b")); lookup.add(new Token("b"));
assertIterableEquals(lookup.allPossible(), Arrays.asList(new Token("a"), new Token("b"))); assertIterableEquals(Arrays.asList(new Token("a"), new Token("b")), lookup.allPossible());
lookup.add(new Token("c")); lookup.add(new Token("c"));
assertIterableEquals( assertIterableEquals(
lookup.allPossible(), Arrays.asList(new Token("a"), new Token("b"), new Token("c")),
Arrays.asList(new Token("a"), new Token("b"), new Token("c"))); lookup.allPossible());
lookup.add(new Token("a")); lookup.add(new Token("a"));
assertIterableEquals( assertIterableEquals(
lookup.allPossible(), Arrays.asList(new Token("a"), new Token("b"), new Token("c")),
Arrays.asList(new Token("a"), new Token("b"), new Token("c"))); lookup.allPossible());
} }
@Test @Test
public void getTotalCounts() { public void getTotalCounts() {
assertEquals(lookup.getTotalCounts(), 1); assertEquals(1, lookup.getTotalCounts());
lookup.add(new Token("b")); lookup.add(new Token("b"));
assertEquals(lookup.getTotalCounts(), 2); assertEquals(2, lookup.getTotalCounts());
lookup.add(new Token("b")); lookup.add(new Token("b"));
assertEquals(lookup.getTotalCounts(), 3); assertEquals(3, lookup.getTotalCounts());
} }
@Test @Test
public void getDistinctTokens() { public void getDistinctTokens() {
assertEquals(lookup.getDistinctTokens(), 1); assertEquals(1, lookup.getDistinctTokens());
lookup.add(new Token("b")); lookup.add(new Token("b"));
assertEquals(lookup.getDistinctTokens(), 2); assertEquals(2, lookup.getDistinctTokens());
lookup.add(new Token("b"));
assertEquals(lookup.getDistinctTokens(), 2);
}
@Test
public void getAmount() {
assertEquals(1, lookup.getAmount(new Token("a")));
lookup.add(new Token("a"));
assertEquals(2, lookup.getAmount(new Token("a")));
lookup.add(new Token("a"));
assertEquals(3, lookup.getAmount(new Token("a")));
lookup.add(new Token("b")); lookup.add(new Token("b"));
assertEquals(2, lookup.getDistinctTokens());
} }
} }
\ No newline at end of file
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