Commit 67d038d8 by Hut

Moved propability calculation to finish step.

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