Skip to content

Commit

Permalink
Will not store graph by default
Browse files Browse the repository at this point in the history
  • Loading branch information
howl-anderson committed Sep 2, 2018
1 parent 765fc07 commit fb0a686
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions MicroHMM/viterbi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
import networkx as nx


class MockedGraph:
def do_nothing(self, *args, **kwargs):
pass

add_node = do_nothing
add_edge = do_nothing


class Viterbi(object):
def __init__(self, A, B, vocabulary, start_state='<start>', end_state='<end>', very_small_probability=1e-32):
def __init__(self, A, B, vocabulary, start_state='<start>', end_state='<end>', very_small_probability=1e-32, store_graph=False):
self.A = A
self.B = B
self.vocabulary = vocabulary
Expand All @@ -20,7 +28,7 @@ def __init__(self, A, B, vocabulary, start_state='<start>', end_state='<end>', v
self.very_small_probability = math.log(very_small_probability)

# create networkx graph
self.G = nx.Graph()
self.G = nx.Graph() if store_graph else MockedGraph()

def _do_predict(self, word_list):
N = len(word_list)
Expand Down Expand Up @@ -187,6 +195,9 @@ def predict_state(self, word_list):
return list(reversed(reverse_state_sequence))

def write_graphml(self, graphml_file):
if isinstance(self.G, MockedGraph):
raise ValueError("store_graph is False when init Viterbi, so there no graph")

nx.write_graphml(
self.G,
graphml_file,
Expand Down

0 comments on commit fb0a686

Please sign in to comment.