1
我已经实现了使用HashMaps的一阶,二阶和三阶隐马尔可夫模型,而不是转换矩阵。根据顺序,我使用这些HMM计算1音符/ 2音符/ 3音符后音符的出现次数(建模为0-128整数)。分层HashMap N阶HMM实现
例如,对于二阶的实现是:
public void updateWeigths(ArrayList<Note> notes, HashMap<Integer, HashMap<Integer, HashMap<Integer, Double>>> hm) {
for (int i=0; i<notes.size()-2; i++) {
int prevPitch1 = notes.get(i).getPitch();
int prevPitch2 = notes.get(i+1).getPitch();
int nextPitch = notes.get(i+2).getPitch();
if (prevPitch1 > 0 && prevPitch2 > 0 && nextPitch > 0) {
if (hm.containsKey(prevPitch1)) {
HashMap<Integer, HashMap<Integer, Double>> nextMapping1 = hm.get(prevPitch1);
if (nextMapping1.containsKey(prevPitch2)){
HashMap<Integer, Double> nextMapping2 = nextMapping1.get(prevPitch2);
if (nextMapping2.containsKey(nextPitch)) {
double prob = nextMapping2.get(nextPitch);
nextMapping2.put(nextPitch, prob+1);
}
else {
nextMapping2.put(nextPitch, 1.0);
}
}
else {
nextMapping1.put(prevPitch2, new HashMap<Integer, Double>());
}
}
else {
hm.put(prevPitch1, new HashMap<Integer,HashMap<Integer,Double>>());
}
}
}
}
我想要实现使用相同的模式任意顺序HMM。我尝试使用多态,但每次都得到ClassCastException。不完全确定如何在此上使用泛型。我猜的诀窍是知道你什么时候在最后一个HashMap上,这样你就可以更新计数Double值。
任何建议将是伟大的!