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
e03e6f08
Commit
e03e6f08
authored
Feb 12, 2018
by
Hut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mehr analyse, refactoringcram
parent
3e2e0d64
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
17 deletions
+91
-17
Builder.java
Markov/src/directory/passive/markov/Builder.java
+24
-7
Data.java
Markov/src/directory/passive/markov/Data.java
+10
-1
Lookup.java
Markov/src/directory/passive/markov/Lookup.java
+20
-5
Main.java
Markov/src/directory/passive/markov/Main.java
+8
-3
Renderer.java
Markov/src/directory/passive/markov/Renderer.java
+29
-1
No files found.
Markov/src/directory/passive/markov/Builder.java
View file @
e03e6f08
...
@@ -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
());
}
}
...
...
Markov/src/directory/passive/markov/Data.java
View file @
e03e6f08
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
()
...
...
Markov/src/directory/passive/markov/Lookup.java
View file @
e03e6f08
...
@@ -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
(
", "
))
+
"]"
;
}
}
}
}
Markov/src/directory/passive/markov/Main.java
View file @
e03e6f08
...
@@ -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"
...
...
Markov/src/directory/passive/markov/Renderer.java
View file @
e03e6f08
...
@@ -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
();
}
}
...
...
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