Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pep8 Corrections #62

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ matrix:
include:
- python: 3.6

before_install:
- pip install pytest pytest-pep8

install:
- pip install -e .[tests]
- pip install tensorflow==2.0.0-alpha0

script:
- PYTHONPATH=$PWD:$PYTHONPATH py.test tests/
- PYTHONPATH=$PWD:$PYTHONPATH py.test --pep8 -m pep8 && py.test tests/;
85 changes: 52 additions & 33 deletions examples/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,49 @@
# Classifier, predicts the class of user input.

CONV_SAMPLES = {
'greetings' : ['Hi', 'hello', 'How are you', 'hey there', 'hey'],
'taxi' : ['book a cab', 'need a ride', 'find me a cab'],
'weather' : ['what is the weather in tokyo', 'weather germany',
'what is the weather like in kochi'],
'datetime' : ['what day is today', 'todays date', 'what time is it now',
'time now', 'what is the time']}
'greetings': ['Hi', 'hello', 'How are you', 'hey there', 'hey'],
'taxi': ['book a cab', 'need a ride', 'find me a cab'],
'weather': ['what is the weather in tokyo', 'weather germany',
'what is the weather like in kochi'],
'datetime': ['what day is today', 'todays date', 'what time is it now',
'time now', 'what is the time']}

CLF = Classifier()
for key in CONV_SAMPLES:
CLF.fit(CONV_SAMPLES[key], key)



# Entitiy Extractor, gets the required entities from the input.

X_WEATHER = ['what is the weather in tokyo', 'weather germany', 'what is the weather like in kochi']
Y_WEATHER = [{'intent': 'weather', 'place': 'tokyo'}, {'intent': 'weather', 'place': 'germany'},
{'intent': 'weather', 'place': 'kochi'}]
X_WEATHER = [
'what is the weather in tokyo',
'weather germany',
'what is the weather like in kochi']
Y_WEATHER = [
{'intent': 'weather', 'place': 'tokyo'},
{'intent': 'weather', 'place': 'germany'},
{'intent': 'weather', 'place': 'kochi'}]

EX_WEATHER = EntityExtractor()
EX_WEATHER.fit(X_WEATHER, Y_WEATHER)


X_TAXI = ['book a cab to kochi ', 'need a ride to delhi', 'find me a cab for manhattan',
'call a taxi to calicut']
Y_TAXI = [{'service': 'cab', 'destination': 'kochi'}, {'service': 'ride', 'destination' : 'delhi'},
{'service': 'cab', 'destination' : 'manhattan'},
{'service': 'taxi', 'destination' : 'calicut'}]
X_TAXI = [
'book a cab to kochi ',
'need a ride to delhi',
'find me a cab for manhattan',
'call a taxi to calicut']
Y_TAXI = [
{
'service': 'cab', 'destination': 'kochi'}, {
'service': 'ride', 'destination': 'delhi'}, {
'service': 'cab', 'destination': 'manhattan'}, {
'service': 'taxi', 'destination': 'calicut'}]

EX_TAXI = EntityExtractor()
EX_TAXI.fit(X_TAXI, Y_TAXI)



X_GREETING = ['Hii', 'helllo', 'Howdy', 'hey there', 'hey', 'Hi']
Y_GREETING = [{'greet': 'Hii'}, {'greet': 'helllo'}, {'greet': 'Howdy'},
{'greet': 'hey'}, {'greet': 'hey'}, {'greet': 'Hi'}]
Expand All @@ -72,18 +81,25 @@
EX_GREETING.fit(X_GREETING, Y_GREETING)


X_DATETIME = ['what day is today', 'date today', 'what time is it now', 'time now']
Y_DATETIME = [{'intent' : 'day', 'target': 'today'}, {'intent' : 'date', 'target': 'today'},
{'intent' : 'time', 'target': 'now'}, {'intent' : 'time', 'target': 'now'}]
X_DATETIME = [
'what day is today',
'date today',
'what time is it now',
'time now']
Y_DATETIME = [
{'intent': 'day', 'target': 'today'},
{'intent': 'date', 'target': 'today'},
{'intent': 'time', 'target': 'now'},
{'intent': 'time', 'target': 'now'}]

EX_DATETIME = EntityExtractor()
EX_DATETIME.fit(X_DATETIME, Y_DATETIME)


_EXTRACTORS = {'taxi':EX_TAXI,
'weather':EX_WEATHER,
'greetings':EX_GREETING,
'datetime':EX_DATETIME}
_EXTRACTORS = {'taxi': EX_TAXI,
'weather': EX_WEATHER,
'greetings': EX_GREETING,
'datetime': EX_DATETIME}


# Response logic.
Expand All @@ -101,40 +117,43 @@ def get_response(u_query):
# Run entity extractor of the predicted class on the query.
q_entities = _EXTRACTORS[q_class].predict(u_query)


# Get response based on the class of the query.
if q_class == 'weather':
responses['weather'] = 'The '+q_entities['intent']+' in '+q_entities['place']+ ' is warm'
responses['weather'] = 'The ' + q_entities['intent'] + \
' in ' + q_entities['place'] + ' is warm'

def get_weather():
# Weather api call.
pass

if q_class == 'taxi':
responses['taxi'] = 'Booking a '+q_entities['service']+ ' for '+q_entities['destination']
responses['taxi'] = 'Booking a ' + q_entities['service'] + \
' for ' + q_entities['destination']

def get_taxi():
# Uber/Ola api.
pass

if q_class == 'datetime':
responses['datetime'] = 'Today is '+str(datetime.datetime.today()).split(' ')[0]
responses['datetime'] = 'Today is ' + \
str(datetime.datetime.today()).split(' ')[0]

def get_dateime():
# Calender api.
pass

if q_class == 'greetings':
responses['greetings'] = ['Hey', 'Hi there',
'Hello'][rd_i]+['\n what would you like me to do ?', '',
'\n what would you like me to do ?'][rd_i]

return 'Agent : '+responses[q_class]
responses['greetings'] = ['Hey',
'Hi there',
'Hello'][rd_i] + \
['\n what would you like me to do ?',
'',
'\n what would you like me to do ?'][rd_i]

return 'Agent : ' + responses[q_class]


# Conversation loop.

if __name__ == '__main__':
# Greeting user on startup.
print(get_response('Hi'))
Expand Down
11 changes: 8 additions & 3 deletions eywa/blameflow/blame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ class BlameType:
class Blame(object):

def __init__(self, blame_type, expected=None, confidence=1.0):
assert blame_type in [BlameType.POSITIVE, BlameType.NEGATIVE, BlameType.CORRECTIVE]
assert blame_type in [
BlameType.POSITIVE,
BlameType.NEGATIVE,
BlameType.CORRECTIVE]
self.blame_type = blame_type
self.expected = expected
if blame_type != BlameType.CORRECTIVE and expected is not None:
raise Exception("expected arg should be provided only for corrective blame.")
raise Exception(
"expected arg should be provided only for corrective blame.")
assert confidence >= 0. and confidence <= 1.0
if confidence < 1.0 and blame_type == BlameType.POSITIVE:
raise Exception("Positive blame should always have 100% confidence.")
raise Exception(
"Positive blame should always have 100% confidence.")
self.confidence = confidence
self.parent = None
self.node = None
Expand Down
23 changes: 16 additions & 7 deletions eywa/blameflow/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class Graph(Node):
def __init__(self, input_map, final_node, name=None):
super(Graph, self).__init__(name)
# setup inputs
self.input_map = self._resolve_input_map(input_map) # {graph_input_name: (node, node_input_name), ...}
# {graph_input_name: (node, node_input_name), ...}
self.input_map = self._resolve_input_map(input_map)
for inp_name in self.input_map:
self.add_input(inp_name)
self.final_node = final_node
Expand All @@ -24,7 +25,8 @@ def _resolve_input_map(self, input_map):
assert len(v.input_nodes) <= 1
input_map_2[k] = (v, None)
else:
raise TypeError("Unexpected type in input map: " + str(type(v)))
raise TypeError(
"Unexpected type in input map: " + str(type(v)))
return input_map_2

def _can_reach_final_node(self, node):
Expand All @@ -43,9 +45,14 @@ def _check_disconnected_inputs(self):
for v in self.input_map.values():
inp_node = v[0]
if not self._can_reach_final_node(inp_node):
raise Exception("Disconnected graph: unable to reach final " +
"node " + self.final_node.name + " from input node" +
inp_node.name + " in graph " + self.name)
raise Exception(
"Disconnected graph: unable to reach final " +
"node " +
self.final_node.name +
" from input node" +
inp_node.name +
" in graph " +
self.name)

def _traverse_graph(self):
self._check_disconnected_inputs()
Expand Down Expand Up @@ -95,7 +102,8 @@ def serialize(self):
input_map_config = {}
for k, v in self.input_map.items():
input_map_config[k] = (node_index[v[0]], v[1])
connectome = {i:list() for i in range(len(self.nodes))} # {node_index1: [(node_index2, input_name), ...]...}
# {node_index1: [(node_index2, input_name), ...]...}
connectome = {i: list() for i in range(len(self.nodes))}
for node in self.nodes:
idx = node_index[node]
for onode, inp_name in node.output_nodes:
Expand Down Expand Up @@ -126,7 +134,8 @@ def deserialize(cls, config, custom_objects={}):
node2 = nodes[idx2]
node1.connect(node2, inp_name)
input_map_config = config['input_map']
input_map = {k: (nodes[v[0]], v[1]) for k, v in input_map_config.items()}
input_map = {k: (nodes[v[0]], v[1])
for k, v in input_map_config.items()}
final_node = nodes[config['final_node']]
name = config['name']
graph = cls(input_map, final_node, name=name)
Expand Down
6 changes: 4 additions & 2 deletions eywa/blameflow/graph/namespace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
_namespace = {}
_node_classes = {}


def get_node(name):
try:
return _namespace[name]
Expand Down Expand Up @@ -28,8 +29,9 @@ def register_node(node):
if _namespace[node.name] == node:
raise Exception('Node already registered: ' + node.name)
else:
raise Exception('Another node with the same name (' + node.name + ')'
' already exists.')
raise Exception(
'Another node with the same name (' + node.name + ')'
' already exists.')
node._auto_name = False
_namespace[node.name] = node

Expand Down
6 changes: 4 additions & 2 deletions eywa/blameflow/graph/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def inputs_ready(self):
return True

def run(self):
if not self.cache or not self.input_nodes or self._inputs_changed and self.inputs_ready():
if not self.cache or not self.input_nodes \
or self._inputs_changed and self.inputs_ready():
self.value = self.f(self.input_values)
self._inputs_changed = False

Expand All @@ -37,7 +38,8 @@ def pull(self):
for inp_name, inp_node in self.input_nodes.items():
if inp_node is None:
if self.input_values[inp_name] is None:
raise Exception("Value not provided for input node " + inp_name)
raise Exception(
"Value not provided for input node " + inp_name)
else:
self.input_values[inp_name] = inp_node.pull()
self.run()
Expand Down
10 changes: 7 additions & 3 deletions eywa/blameflow/map.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from .graph import Node
from .blame import Blame, BlameType


class Map(Node):
def __init__(self, map, default=None, name=None):
self.map = map.copy()
self.rev_map = {v:k for k, v in self.map.items()}
assert len(self.map) == len(self.rev_map), "Map node requires a 1:1 dictionary."
self.rev_map = {v: k for k, v in self.map.items()}
assert len(
self.map) == len(
self.rev_map), "Map node requires a 1:1 dictionary."
self.default = None
super(Map, self).__init__(name=name)

Expand All @@ -32,7 +35,8 @@ def serialize(self):
def blame(self, blame):
super(Map, self).blame(blame)
if blame.blame_type == BlameType.CORRECTIVE:
self.input_node.blame(blame.fork(expected=self.rev_map[blame.expected]))
self.input_node.blame(blame.fork(
expected=self.rev_map[blame.expected]))
else:
self.input_node.blame(blame.fork())

Expand Down
21 changes: 13 additions & 8 deletions eywa/blameflow/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def pull(self): # we dont need all inputs

def pull_all(self):
return super(Select, self).pull()

def inrange(self, y):
for name, node in self.input_nodes.items():
if name == 'selector':
Expand All @@ -76,10 +76,13 @@ def blame(self, blame):
continue
if val == blame.expected:
if name == self.input_values['selector']:
self.input_nodes['selector'].blame(blame.fork(BlameType.POSITIVE))
self.input_nodes['selector'].blame(
blame.fork(BlameType.POSITIVE))
else:
self.input_nodes['selector'].blame(blame.fork(BlameType.CORRECTIVE, name))
self.input_nodes[name].blame(blame.fork(BlameType.POSITIVE))
self.input_nodes['selector'].blame(
blame.fork(BlameType.CORRECTIVE, name))
self.input_nodes[name].blame(
blame.fork(BlameType.POSITIVE))
return
for name, node in self.input_nodes:
if node is None:
Expand All @@ -88,12 +91,14 @@ def blame(self, blame):
continue
if node.inrange(blame.expected):
if name == self.input_values['selector']:
self.input_nodes['selector'].blame(blame.fork(BlameType.POSITIVE))
self.input_nodes['selector'].blame(
blame.fork(BlameType.POSITIVE))
else:
return self.input_nodes['selector'].blame(blame.fork(BlameType.CORRECTIVE, name))
return self.input_nodes['selector'].blame(
blame.fork(BlameType.CORRECTIVE, name))
node.blame(blame.fork())
elif blame.blame_type == BlameType.NEGATIVE:
c = blame.confidence * 0.5
self.input_nodes['selector'].blame(blame.fork(confidence=c))
self.input_nodes[self.input_values['selector']].blame(blame.fork(confidence=c))

self.input_nodes[self.input_values['selector']].blame(
blame.fork(confidence=c))
10 changes: 6 additions & 4 deletions eywa/blameflow/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def remove_option(self, opt):
self.options.remove(opt)

def switch_f(self, inputs):
raise NotImplementedError("switch_f() not implemented by class " + self.__class__.__name__)
raise NotImplementedError(
"switch_f() not implemented by class " +
self.__class__.__name__)

def f(self, inputs):
out = self.switch_f(inputs)
Expand All @@ -37,12 +39,12 @@ def deserialize(cls, config):
for opt in config['options']:
sw.add_option(opt)
return sw



class RandomSwitch(Switch):
def __init__(self, name=None):
super(RandomSwitch, self).__init__(name=name)
self.cache = False # different output each time
self.cache = False # different output each time

def switch_f(self, inputs):
return random.choice(list(self.options))

Loading