-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkovModel.java
More file actions
93 lines (78 loc) · 2.71 KB
/
Copy pathMarkovModel.java
File metadata and controls
93 lines (78 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.Set;
/**
* Construct a Markov model of order /k/ based on an input string.
*
* @Jesse Carter 22277029 & Nick Walters 22243339
* @Version 1 25th May '17
*/
public class MarkovModel
{
/** Markov model order parameter */
int k;
/** ngram model of order k */
NgramAnalyser ngram;
/** ngram model of order k+1 */
NgramAnalyser n1gram;
/**
* Construct an order-k Markov model from string s
* @param k int order of the Markov model
* @param s String input to be modelled
*/
public MarkovModel(int k, String s)
{
this.k = k;
ngram = new NgramAnalyser(k, s);
n1gram = new NgramAnalyser(k+1, s);
}
public MarkovModel()
{
this(2, "aabcabaacaac");
}
/**
* @return order of this Markov model
*/
public int getK()
{
return k;
}
/** Estimate the probability of a sequence appearing in the text
* using simple estimate of freq seq / frequency front(seq).
* @param sequence String of length k+1
* @return double probability of the last letter occuring in the
* context of the first ones or 0 if front(seq) does not occur.
*/
public double simpleEstimate(String sequence)
{
if ((sequence.length() != k+1) || (sequence.equals("")) || sequence == null) {
throw new IllegalArgumentException(
"Invalid Input Parameters, Please enter a sequence of greater length");
} else {
double probability = 0.0;
// find frequency of specific sequence and separately the last character's preceding characters
int sequenceFreq = n1gram.getNgramFrequency(sequence);
int preFreq = ngram.getNgramFrequency(sequence.substring(0,sequence.length()-1));
probability = (double) sequenceFreq / (double) preFreq;
return probability;
}
}
/**
* Calculate the Laplacian probability of string obs given this Markov model
* @input sequence String of length k+1
*/
public double laplaceEstimate(String sequence)
{
double probability = 0.0;
int sequenceFreq = n1gram.getNgramFrequency(sequence);
int preFreq = ngram.getNgramFrequency(sequence.substring(0,sequence.length()-1));
probability = ((double) sequenceFreq + 1) / ((double) preFreq + ngram.getAlphabetSize());
return probability;
}
/**
* @return String representing this Markov model
*/
public String toString()
{
String toStr = k + "\n" + ngram.getAlphabetSize() + "\n" + ngram.toString() + "\n" + n1gram.toString();
return toStr;
}
}