Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
markov
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Patrick Friedel
markov
Commits
67d038d8
Commit
67d038d8
authored
Mar 25, 2018
by
Hut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved propability calculation to finish step.
parent
218bdd37
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
49 deletions
+44
-49
Decission.java
src/main/java/markov/Decission.java
+7
-2
Lookup.java
src/main/java/markov/Lookup.java
+18
-18
Sentence.java
src/main/java/markov/Sentence.java
+6
-7
LookupTests.java
src/test/java/markov/LookupTests.java
+13
-22
No files found.
src/main/java/markov/Decission.java
View file @
67d038d8
...
...
@@ -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
;
}
...
...
src/main/java/markov/Lookup.java
View file @
67d038d8
...
...
@@ -18,17 +18,16 @@ public class Lookup implements Serializable {
private
int
totalCounts
=
0
;
int
getTotalCounts
()
{
return
totalCounts
;
}
int
getDistinctTokens
()
{
return
tokens
.
size
();
finishGuard
();
return
finalData
.
length
;
}
int
getAmount
(
Token
t
)
{
return
tokens
.
getOrDefault
(
t
,
0
);
private
void
finishGuard
()
{
if
(!
isFinishedCollecting
)
{
throw
new
IllegalStateException
(
"not jet finished"
);
}
}
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
...
...
src/main/java/markov/Sentence.java
View file @
67d038d8
...
...
@@ -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
...
...
src/test/java/markov/LookupTests.java
View file @
67d038d8
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment