Skip to content

Commit 1becd1d

Browse files
committed
updates to graph and documenting code
1 parent 815acd6 commit 1becd1d

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

final.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@
179179
"""
180180
from enum import Enum
181181
import graph
182+
import final_tests.py
182183

183184

184185
class Tokenization(Enum):
185186
word = 1
186187
character = 2
187188
byte = 3
188-
none = 4
189+
none = None
189190

190191

191192
class RandomWriter(object):
@@ -315,7 +316,25 @@ def train_iterable(self, data):
315316
# you need to convert iterables, remember how much I hate
316317
# indexing into lists and that not every iterable you get here
317318
# will support indexing.
318-
raise NotImplementedError
319+
320+
# Using Arthur Peters' function, convert the input iterable into
321+
# a new iterable with properly sized windows
322+
states = final_tests.windowed(data, self.level)
323+
# data is a string
324+
if self.tokenization is Tokenization.character or self.tokenization \
325+
== Tokenization.word:
326+
for state in states:
327+
...
328+
# data is bytes
329+
elif self.tokenization == Tokenization.byte:
330+
...
331+
# data is an iterable
332+
elif self.tokenization == Tokenization.none:
333+
...
334+
else:
335+
raise TypeError("Error: the data passed in is not of acceptable "
336+
"form: iterable/string/bytes")
337+
319338

320339
"""Modules you will want to look at:
321340
* enum

final_tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,20 @@ def windowed(iterable, size):
6767
6868
The windows are produced by sliding a window over the input iterable.
6969
"""
70+
# empty list to be returned
7071
window = list()
72+
# iterate over the variables in the iterable
7173
for v in iterable:
74+
# check if the size of the window is in the proper window
7275
if len(window) < size:
76+
# if so then add the next word/char/byte to the window
7377
window.append(v)
7478
else:
79+
# otherwise, pop front and add to back, sliding the window down
80+
# by one unit
7581
window.pop(0)
7682
window.append(v)
83+
# if the window has reached the size, yield it as a tuple
7784
if len(window) == size:
7885
yield tuple(window)
7986

graph.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ def __init__(self):
2222
"""Our constructor for the Graph class.
2323
2424
Each graph should store a container of all the nodes in the graph.
25-
The container should be a dict that maps the specific state of the
26-
Markov chain to the specific instantiation of the Vertex that
25+
The container should be a dict that maps the specific data of a
26+
vertex to the specific instantiation of the Vertex that
2727
encapsulates that state
2828
2929
TODO: Make sure to incorporate the empty graph case.
3030
"""
31-
# Our container of all the vertices in this Graph
31+
# Our container of all the vertices in this Graph, this dictionary
32+
# should map data to the vertices that encapsulate the data
3233
self._vertices = {}
3334

3435
@property
@@ -38,35 +39,38 @@ def vertices(self):
3839
"""
3940
return frozenset(self._vertices.keys())
4041

41-
def add_vertex(self, state):
42+
def add_vertex(self, data):
4243
"""Method to add another Vertex to this Graph. Simply store a newly
4344
instantiated Vertex in our container
4445
"""
45-
# TODO: make sure that the state can be any value?
46-
# Create a new Vertex and add the (k, v) pair of mapping state to
47-
# actual Vertex to our Graph
48-
self._vertices[state] = Vertex(state)
49-
50-
def __contains__(self, state):
51-
"""Method overriding. Make sure that we can quickly check to see if
52-
a given state is contained in the graph
46+
if data is None:
47+
raise TypeError("Error: data must be non-null")
48+
else:
49+
# Create a new Vertex and add the (k, v) pair of mapping data to
50+
# actual Vertex to our Graph
51+
self._vertices[data] = Vertex(data)
52+
53+
def __contains__(self, data):
54+
"""Contains method overriding. Make sure that we can quickly check to
55+
see if a given data piece is contained in the graph
5356
"""
54-
return state in self._vertices.keys()
57+
return data in self._vertices.keys()
5558

56-
def __getitem__(self, state):
57-
"""Contains method for our Graph class. Make sure that we can quickly
58-
access any given state in our graph
59+
def __getitem__(self, data):
60+
"""Get method overriding. Make sure that we can quickly
61+
access any given Vertex in our graph
5962
"""
60-
# attempt to hash the given state to return its corresponding Vertex
63+
# attempt to hash the given data to return its corresponding Vertex
6164
try:
62-
return self._vertices[state]
65+
return self._vertices[data]
6366
except ValueError:
64-
raise KeyError(f"State {repr(state)} is not in the Graph")
67+
raise KeyError(f"State {repr(data)} is not in the Graph")
6568

6669
def compute_probabilities(self):
6770
"""Method to traverse our graph entirely and compute the
6871
probabilities of each of the paths
6972
"""
73+
# TODO: KEEP GRAPH CONTEXT MAN. SIMPLE AND GENERIC, NO COMPUTATION HERE
7074
raise NotImplementedError()
7175

7276

@@ -78,27 +82,26 @@ def compute_probabilities(self):
7882

7983

8084
class Vertex:
81-
def __init__(self, state):
85+
def __init__(self, data):
8286
"""Our constructor for our Vertex class.
8387
84-
Vertices should contain the current state of the Markov chain in which
85-
we are in as well of a storage container of all the outgoing edges that
86-
are directed from this Vertex to another Vertex
88+
Vertices should contain data as well of a storage container of all the
89+
outgoing edges that are directed from this Vertex to another Vertex
8790
"""
8891
# Our current state of history in regards to
89-
self._state = state
90-
# Our container for possible next states which is encapsulated by
91-
# dictionary mappings of tokens to Edges
92+
self._data = data
93+
# Our container for edges that leave this vertex which is
94+
# encapsulated by dictionary mappings of tokens to Edges
9295
self._outgoing_edges = {}
9396
# TODO: may need to add another dict for fast lookups that maps Edge
9497
# objects to probabilities
9598

9699
@property
97-
def state(self):
100+
def data(self):
98101
"""Getter for Vertex that returns the current state (differentiates
99102
vertices from one another).
100103
"""
101-
return self._state
104+
return self._data
102105

103106
@property
104107
def outgoing_edges(self):

0 commit comments

Comments
 (0)