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
273a6bcf
Commit
273a6bcf
authored
Feb 12, 2018
by
Hut
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first working version
parents
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
194 additions
and
0 deletions
+194
-0
Data.java
Markov/src/directory/passive/markov/Data.java
+26
-0
Lookup.java
Markov/src/directory/passive/markov/Lookup.java
+49
-0
Main.java
Markov/src/directory/passive/markov/Main.java
+0
-0
Prefix.java
Markov/src/directory/passive/markov/Prefix.java
+41
-0
Token.java
Markov/src/directory/passive/markov/Token.java
+78
-0
No files found.
Markov/src/directory/passive/markov/Data.java
0 → 100644
View file @
273a6bcf
package
directory
.
passive
.
markov
;
import
java.util.HashMap
;
import
java.util.Map
;
// FIXME rename
public
class
Data
{
private
final
Map
<
Prefix
,
Lookup
>
data
=
new
HashMap
<>();
public
void
add
(
Prefix
p
,
Token
t
)
{
Lookup
l
=
data
.
getOrDefault
(
p
,
new
Lookup
());
l
.
add
(
t
);
data
.
put
(
p
,
l
);
}
public
Lookup
fetch
(
Prefix
p
)
{
return
data
.
getOrDefault
(
p
,
Lookup
.
empty
);
}
@Override
public
String
toString
()
{
return
"Data [data="
+
data
+
"]"
;
}
}
Markov/src/directory/passive/markov/Lookup.java
0 → 100644
View file @
273a6bcf
package
directory
.
passive
.
markov
;
import
java.util.LinkedHashMap
;
import
java.util.Map.Entry
;
// FIXME rename
public
class
Lookup
{
public
static
final
Lookup
empty
=
new
Lookup
()
{
public
Token
forRandom
(
double
random
)
{
return
Token
.
EMPTY
;
}
};
public
Lookup
()
{
}
private
final
LinkedHashMap
<
Token
,
Integer
>
tokens
=
new
LinkedHashMap
<
Token
,
Integer
>();
private
int
getTotalCounts
()
{
return
tokens
.
values
().
stream
().
mapToInt
(
i
->
i
.
intValue
()).
sum
();
}
public
void
add
(
Token
t
)
{
Integer
i
=
tokens
.
getOrDefault
(
t
,
0
);
tokens
.
put
(
t
,
i
+
1
);
}
public
Token
forRandom
(
double
random
)
{
int
id
=
(
int
)
Math
.
floor
(
random
*
getTotalCounts
());
int
i
=
0
;
for
(
Entry
<
Token
,
Integer
>
entry
:
tokens
.
entrySet
())
{
i
+=
entry
.
getValue
();
if
(
id
<
i
)
{
return
entry
.
getKey
();
}
}
throw
new
IllegalStateException
(
String
.
format
(
"failed to find a random token with seed %d within %s"
,
id
,
this
));
}
@Override
public
String
toString
()
{
return
"Lookup [tokens="
+
tokens
+
"]"
;
}
}
Markov/src/directory/passive/markov/Main.java
0 → 100644
View file @
273a6bcf
This diff is collapsed.
Click to expand it.
Markov/src/directory/passive/markov/Prefix.java
0 → 100644
View file @
273a6bcf
package
directory
.
passive
.
markov
;
import
java.util.Arrays
;
// FIXME rename
public
class
Prefix
{
private
final
Token
[]
tokens
;
public
Prefix
(
Token
[]
tokens
)
{
this
.
tokens
=
tokens
;
}
@Override
public
String
toString
()
{
return
"Prefix [tokens="
+
Arrays
.
toString
(
tokens
)
+
"]"
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
Arrays
.
hashCode
(
tokens
);
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
Prefix
other
=
(
Prefix
)
obj
;
if
(!
Arrays
.
equals
(
tokens
,
other
.
tokens
))
return
false
;
return
true
;
}
}
Markov/src/directory/passive/markov/Token.java
0 → 100644
View file @
273a6bcf
package
directory
.
passive
.
markov
;
public
class
Token
{
public
static
final
Token
START
=
new
SpecialToken
();
public
static
final
Token
END
=
new
SpecialToken
();
protected
static
final
Token
EMPTY
=
new
SpecialToken
();
public
Token
(
String
content
)
{
super
();
this
.
content
=
content
;
}
String
content
;
@Override
public
String
toString
()
{
if
(
this
==
END
)
{
return
"Token <END>"
;
}
if
(
this
==
START
)
{
return
"TOKEN <START>"
;
}
if
(
this
==
EMPTY
)
return
"TOKEN <empty>"
;
return
"Token [content="
+
content
+
"]"
;
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
((
content
==
null
)
?
0
:
content
.
hashCode
());
return
result
;
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
{
return
true
;
}
if
(
obj
==
null
)
return
false
;
if
(
this
==
START
)
return
false
;
if
(
this
==
END
)
return
false
;
if
(
this
==
EMPTY
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
Token
other
=
(
Token
)
obj
;
if
(
content
==
null
)
{
if
(
other
.
content
!=
null
)
return
false
;
}
else
if
(!
content
.
equals
(
other
.
content
))
return
false
;
return
true
;
}
public
String
render
(
String
prefix
)
{
return
prefix
+
content
;
}
private
static
class
SpecialToken
extends
Token
{
public
SpecialToken
()
{
super
(
""
);
}
@Override
public
String
render
(
String
prefix
)
{
return
""
;
}
}
}
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