Created
November 20, 2017 17:34
-
-
Save trianta2/260c56dddc0908e3fae1da2513cd4393 to your computer and use it in GitHub Desktop.
A6 part 2 utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
""" | |
Provides A6 part 2 utils | |
""" | |
import random | |
_CHARS_SPACE = ['A', 'Y', 'Z', ' '] | |
_CHARS = ['A', 'Y', 'Z'] | |
_SYMBOLS = { | |
'A': [1, 0, 1, 1, 1], | |
'Y': [1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1], | |
'Z': [1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], | |
'': [0, 0, 0], | |
' ': [0, 0, 0, 0, 0, 0, 0] | |
} | |
def _create_evidence(string): | |
'''Yields bits of an evidence vector from an input string''' | |
for i, c in enumerate(string[:-1]): | |
yield _SYMBOLS[c] | |
if c != ' ' and string[i + 1] != ' ': | |
yield _SYMBOLS[''] | |
yield _SYMBOLS[string[-1]] | |
def create_evidence(string): | |
'''Returns an evidence vector from a string input | |
Parameters | |
---------- | |
string : str | |
Input string to convert to evidence list | |
Returns | |
------- | |
List[int] | |
A sequence of 1s and 0s corresponding to evidence bits | |
''' | |
evidence = [] | |
for l in _create_evidence(string): | |
evidence.extend(l) | |
return evidence | |
def _generate_string(length=None, max_len=10): | |
'''Yields characters that form a valid HMM input string''' | |
length = random.randint(1, max_len) if length is None else length | |
if length < 3: | |
for _ in xrange(length): | |
yield random.choice(_CHARS) | |
else: | |
c = random.choice(_CHARS) | |
yield c | |
for _ in xrange(length - 2): | |
c = random.choice(_CHARS) if c == ' ' else random.choice(_CHARS_SPACE) | |
yield c | |
yield random.choice(_CHARS) | |
def generate_string(length=None, max_len=10): | |
'''Returns a random HMM input string | |
Parameters | |
---------- | |
length : int, optional | |
Length of generated string. If None, is randomly chosen [1, `max_len`] | |
max_len : int, optional | |
Maximum length of random string if `length` is None | |
Returns | |
------- | |
str | |
Valid HMM input string | |
''' | |
return ''.join(_generate_string(length, max_len)) | |
def merge_dicts(*dicts): | |
'''Merges multiple dicts into a single dict. Useful for combining probability dicts | |
Parameters | |
---------- | |
*dicts | |
Variable number of `dict` objects to merge | |
Returns | |
------- | |
dict | |
A single merged dict | |
''' | |
merged = dict() | |
for d in dicts: | |
merged.update(d) | |
return merged | |
if __name__ == '__main__': | |
'''Example usage''' | |
from hmm_submission import part_2_b | |
# TODO: create variables: states, prior_probs, transition_probs, emission_probs | |
input_string = generate_string() # or set this manually, e.g. 'AYZ' | |
evidence_vector = create_evidence(input_string) | |
output_string, _ = part_2_b(evidence_vector, states, prior_probs, transition_probs, emission_probs) | |
assert input_string == output_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment