diff --git a/include/hiddenmarkovmodel.h b/include/hiddenmarkovmodel.h index 13341aa..030f475 100644 --- a/include/hiddenmarkovmodel.h +++ b/include/hiddenmarkovmodel.h @@ -71,20 +71,21 @@ class HiddenMarkovModel { std::map > m_emissionProbabilities; public: + typedef std::vector KeySequence; HiddenMarkovModel( std::vector observations, Key::KeyVector states, std::map initialProbabilities, std::map > transitionProbabilities, std::map > emissionProbabilities); - // HiddenMarkovModel( - // std::vector observations, - // Key::KeyVector states, - // std::map initialProbabilities, - // std::map > transitionProbabilities, - // std::map > emissionProbabilities); + HiddenMarkovModel( + std::vector observations, + Key::KeyVector states, + std::map initialProbabilities, + std::map > transitionProbabilities, + std::map > emissionProbabilities); void printOutput(); - void runViterbi(); + KeySequence runViterbi(); }; diff --git a/src/hiddenmarkovmodel.cc b/src/hiddenmarkovmodel.cc index 558f615..174f991 100644 --- a/src/hiddenmarkovmodel.cc +++ b/src/hiddenmarkovmodel.cc @@ -82,55 +82,55 @@ HiddenMarkovModel::HiddenMarkovModel( } } -// HiddenMarkovModel::HiddenMarkovModel( -// std::vector observations, -// Key::KeyVector states, -// std::map initialProbabilities, -// std::map > transitionProbabilities, -// std::map > emissionProbabilities) { -// // Observations -// for (std::vector::const_iterator itObs = observations.begin(); -// itObs != observations.end(); itObs++) { -// m_observations.push_back(itObs->getInt()); -// } -// // States -// for (std::vector::const_iterator itState = states.begin(); -// itState != states.end(); itState++) { -// m_states.push_back(itState->getString()); -// } -// // Initial probabilities -// for (std::map::const_iterator itInitial = -// initialProbabilities.begin(); itInitial != initialProbabilities.end(); -// itInitial++) { -// std::string key = (itInitial->first).getString(); -// m_initialProbabilities[key] = -// itInitial->second; -// } -// // Transition probabilities -// for (std::map >::const_iterator itTransition = transitionProbabilities.begin(); -// itTransition != transitionProbabilities.end(); itTransition++) { -// std::string fromKey = (itTransition->first).getString(); -// std::map toKeyMap = itTransition->second; -// for (std::map::const_iterator itToKey = toKeyMap.begin(); -// itToKey != toKeyMap.end(); itToKey++ ) { -// std::string toKey = (itToKey->first).getString(); -// m_transitionProbabilities[fromKey][toKey] = itToKey->second; -// } -// } -// // Emission probabilities -// for (std::map >::const_iterator itEmission = emissionProbabilities.begin(); -// itEmission != emissionProbabilities.end(); itEmission++) { -// std::string fromKey = (itEmission->first).getString(); -// std::map emitPcMap = itEmission->second; -// for (std::map::const_iterator itEmitPc = -// emitPcMap.begin(); itEmitPc != emitPcMap.end(); itEmitPc++) { -// int emitPc = (itEmitPc->first).getInt(); -// m_emissionProbabilities[fromKey][emitPc] = itEmitPc->second; -// } -// } -// } +HiddenMarkovModel::HiddenMarkovModel( + std::vector observations, + Key::KeyVector states, + std::map initialProbabilities, + std::map > transitionProbabilities, + std::map > emissionProbabilities) { + // Observations + for (std::vector::const_iterator itObs = observations.begin(); + itObs != observations.end(); itObs++) { + m_observations.push_back(itObs->getString()); + } + // States + for (std::vector::const_iterator itState = states.begin(); + itState != states.end(); itState++) { + m_states.push_back(itState->getString()); + } + // Initial probabilities + for (std::map::const_iterator itInitial = + initialProbabilities.begin(); itInitial != initialProbabilities.end(); + itInitial++) { + std::string key = (itInitial->first).getString(); + m_initialProbabilities[key] = + itInitial->second; + } + // Transition probabilities + for (std::map >::const_iterator itTransition = transitionProbabilities.begin(); + itTransition != transitionProbabilities.end(); itTransition++) { + std::string fromKey = (itTransition->first).getString(); + std::map toKeyMap = itTransition->second; + for (std::map::const_iterator itToKey = toKeyMap.begin(); + itToKey != toKeyMap.end(); itToKey++ ) { + std::string toKey = (itToKey->first).getString(); + m_transitionProbabilities[fromKey][toKey] = itToKey->second; + } + } + // Emission probabilities + for (std::map >::const_iterator itEmission = emissionProbabilities.begin(); + itEmission != emissionProbabilities.end(); itEmission++) { + std::string fromKey = (itEmission->first).getString(); + std::map emitKeyMap = itEmission->second; + for (std::map::const_iterator itEmitKey = + emitKeyMap.begin(); itEmitKey != emitKeyMap.end(); itEmitKey++) { + std::string emitKey = (itEmitKey->first).getString(); + m_emissionProbabilities[fromKey][emitKey] = itEmitKey->second; + } + } +} void HiddenMarkovModel::printOutput() { // print states @@ -178,7 +178,8 @@ void HiddenMarkovModel::printOutput() { } } -void HiddenMarkovModel::runViterbi() { +HiddenMarkovModel::KeySequence HiddenMarkovModel::runViterbi() { + KeySequence keySequence; std::map T; for (std::vector::iterator state=m_states.begin(); @@ -232,6 +233,14 @@ void HiddenMarkovModel::runViterbi() { } } + final_tracker.v_path.pop_back(); + + for (std::vector::const_iterator itNode = + final_tracker.v_path.begin(); itNode != final_tracker.v_path.end(); + itNode++) { + keySequence.push_back(Key(*itNode)); + } + std::cout << "Total probability of the observation sequence: " << final_tracker.prob << std::endl; std::cout << "Probability of the Viterbi path: " @@ -241,6 +250,8 @@ void HiddenMarkovModel::runViterbi() { state != final_tracker.v_path.end(); state++) { std::cout << "VState: " << *state << std::endl; } + + return keySequence; } } // namespace justkeydding diff --git a/test/test_hiddenmarkovmodel.cc b/test/test_hiddenmarkovmodel.cc index 3419414..210d940 100644 --- a/test/test_hiddenmarkovmodel.cc +++ b/test/test_hiddenmarkovmodel.cc @@ -44,35 +44,14 @@ using justkeydding::HiddenMarkovModel; int main(int argc, char *argv[]) { + //////////////////////////// + // First Hidden Markov Model + //////////////////////////// // Observation symbols section std::vector pitchClassSequence; pitchClassSequence.push_back(PitchClass("C")); - pitchClassSequence.push_back(PitchClass("D")); pitchClassSequence.push_back(PitchClass("E")); - pitchClassSequence.push_back(PitchClass("F")); pitchClassSequence.push_back(PitchClass("G")); - pitchClassSequence.push_back(PitchClass("A")); - pitchClassSequence.push_back(PitchClass("B")); - pitchClassSequence.push_back(PitchClass("A")); - pitchClassSequence.push_back(PitchClass("G")); - pitchClassSequence.push_back(PitchClass("F#")); - pitchClassSequence.push_back(PitchClass("E")); - pitchClassSequence.push_back(PitchClass("D")); - pitchClassSequence.push_back(PitchClass("C#")); - pitchClassSequence.push_back(PitchClass("D")); - pitchClassSequence.push_back(PitchClass("E")); - pitchClassSequence.push_back(PitchClass("F#")); - pitchClassSequence.push_back(PitchClass("G")); - pitchClassSequence.push_back(PitchClass("A")); - pitchClassSequence.push_back(PitchClass("B")); - pitchClassSequence.push_back(PitchClass("C")); - pitchClassSequence.push_back(PitchClass("B")); - pitchClassSequence.push_back(PitchClass("A")); - pitchClassSequence.push_back(PitchClass("G")); - pitchClassSequence.push_back(PitchClass("F")); - pitchClassSequence.push_back(PitchClass("E")); - pitchClassSequence.push_back(PitchClass("D")); - pitchClassSequence.push_back(PitchClass("C")); // States section Key::KeyVector keyVector = Key::getAllKeysVector(); @@ -90,12 +69,24 @@ int main(int argc, char *argv[]) { // Emission probabilities KeyProfile::KeyProfileMap emissionProbabilities = KeyProfile("temperley", "sapp").getKeyProfileMap(); + HiddenMarkovModel::KeySequence keySequence; HiddenMarkovModel hmm( pitchClassSequence, keyVector, initialProbabilities, transitionProbabilities, emissionProbabilities); - // hmm.printOutput(); - hmm.runViterbi(); + keySequence = hmm.runViterbi(); + ///////////////////////////// + // Second Hidden Markov Model + ///////////////////////////// + KeyTransition::KeyTransitionMap zeroTransiionProbabilities = + KeyTransition("zero").getKeyTransitionMap(); + HiddenMarkovModel hmm2( + keySequence, + keyVector, + initialProbabilities, + zeroTransiionProbabilities, + transitionProbabilities); + HiddenMarkovModel::KeySequence mainKeySequence = hmm2.runViterbi(); }