Skip to content

Commit

Permalink
added pep8 test and modified source to honor pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed Nov 28, 2016
1 parent 0687d14 commit 2eefe06
Show file tree
Hide file tree
Showing 25 changed files with 263 additions and 254 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ install:
- python setup.py install
- python -m spacy.en.download all > jnk
- python -m spacy.de.download all > jnk
script: py.test
script: py.test --pep8
58 changes: 28 additions & 30 deletions _pytest/test_emulators.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@


def test_luis_request():
from rasa_nlu.emulators.luis import LUISEmulator
em = LUISEmulator()
norm = em.normalise_request_json({"q":["arb text"]})
assert norm == {"text":"arb text"}
norm = em.normalise_request_json({"q": ["arb text"]})
assert norm == {"text": "arb text"}


def test_luis_response():
from rasa_nlu.emulators.luis import LUISEmulator
em = LUISEmulator()
data = {"text":"I want italian food","intent":"inform","entities":{"cuisine":"italian"}}
data = {"text": "I want italian food", "intent": "inform", "entities": {"cuisine": "italian"}}
norm = em.normalise_response_json(data)
assert norm == {
"query": data["text"],
"topScoringIntent": {
"intent": "inform",
"score": None
},
"entities": [
"query": data["text"],
"topScoringIntent": {
"intent": "inform",
"score": None
},
"entities": [
{
"entity": e[0],
"type": e[1],
"startIndex": None,
"endIndex": None,
"score": None
"entity": e[0],
"type": e[1],
"startIndex": None,
"endIndex": None,
"score": None
} for e in data["entities"]
]
}
]
}


def test_wit_request():
from rasa_nlu.emulators.wit import WitEmulator
em = WitEmulator()
norm = em.normalise_request_json({"q":["arb text"]})
assert norm == {"text":"arb text"}
norm = em.normalise_request_json({"q": ["arb text"]})
assert norm == {"text": "arb text"}


def test_wit_response():
from rasa_nlu.emulators.wit import WitEmulator
em = WitEmulator()
data = {"text":"I want italian food","intent":"inform","entities":{"cuisine":"italian"}}
data = {"text": "I want italian food", "intent": "inform", "entities": {"cuisine": "italian"}}
norm = em.normalise_response_json(data)
assert norm == [{'entities': {'cuisine': {'confidence': None, 'type': 'value', 'value': 'italian'}}, 'confidence': None, 'intent': 'inform', '_text': 'I want italian food'}]



assert norm == [
{'entities': {'cuisine': {'confidence': None, 'type': 'value', 'value': 'italian'}}, 'confidence': None,
'intent': 'inform', '_text': 'I want italian food'}]


def test_dummy_request():
from rasa_nlu.emulators import NoEmulator
em = NoEmulator()
norm = em.normalise_request_json({"text":["arb text"]})
assert norm == {"text":"arb text"}
norm = em.normalise_request_json({"text": ["arb text"]})
assert norm == {"text": "arb text"}


def test_dummy_response():
from rasa_nlu.emulators import NoEmulator
em = NoEmulator()
data = {"intent":"greet","text":"hi","entities":{}}
em = NoEmulator()
data = {"intent": "greet", "text": "hi", "entities": {}}
assert em.normalise_response_json(data) == data

4 changes: 2 additions & 2 deletions _pytest/test_featurizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def test_sentence(sentence, language, _ref):

test_sentence(u"hey how are you today",
'en',
_ref=np.array([-0.19649599, 0.32493639, -0.37408298, -0.10622784, 0.062756]))
_ref=np.array([-0.19649599, 0.32493639, -0.37408298, -0.10622784, 0.062756]))

test_sentence(u"hey wie geht es dir",
'de',
_ref=np.array([-0.0518572, -0.13645099, 0.34630662, 0.29546982, -0.0153512]))
_ref=np.array([-0.0518572, -0.13645099, 0.34630662, 0.29546982, -0.0153512]))


def test_mitie():
Expand Down
14 changes: 6 additions & 8 deletions _pytest/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
interpreter = HelloGoodbyeInterpreter()



def test_samples():
samples = [
("Hey there",{'text':"Hey there",'intent':'greet', 'entities' : {}}),
("good bye for now",{'text':"good bye for now",'intent':'goodbye', 'entities' : {}})
]
("Hey there", {'text': "Hey there", 'intent': 'greet', 'entities': {}}),
("good bye for now", {'text': "good bye for now", 'intent': 'goodbye', 'entities': {}})
]

for text, result in samples:
assert interpreter.parse(text) == result, "text : {0} \nresult : {1}, expected {2}".format(text,interpreter.parse(text),result)



assert interpreter.parse(text) == result, "text : {0} \nresult : {1}, expected {2}".format(text,
interpreter.parse(
text), result)
1 change: 1 addition & 0 deletions _pytest/test_tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest


def test_whitespace():
from rasa_nlu.tokenizers.whitespace_tokenizer import WhitespaceTokenizer
tk = WhitespaceTokenizer()
Expand Down
1 change: 1 addition & 0 deletions _pytest/test_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_rasa_whitespace():
td = TrainingData('data/demo-rasa.json', '', 'en')
assert td.fformat == 'rasa_nlu'


def test_api_mitie():
td = TrainingData('data/restaurantBot', 'mitie', 'en')
assert td.fformat == 'api'
1 change: 1 addition & 0 deletions _pytest/trainers/test_mitie_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rasa_nlu.trainers.mitie_trainer import MITIETrainer
from rasa_nlu.train import load_configuration


class TestMitieTrainer:
def test_failure_on_invalid_lang(self):
config = load_configuration("config_example.json")
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ spacy==1.2.0
scikit-learn
pathlib
cloudpickle


pytest-pep8
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pytest PEP8 configuration
[tool:pytest]
pep8maxlinelength = 120
8 changes: 3 additions & 5 deletions src/emulators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@


class NoEmulator(object):
def __init__(self):
self.service= None
self.service = None

def normalise_request_json(self,data):
def normalise_request_json(self, data):
for key, val in data.iteritems():
if type(val) == list:
data[key] = val[0]
return data

def normalise_response_json(self,data):
def normalise_response_json(self, data):
return data
17 changes: 9 additions & 8 deletions src/emulators/api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import uuid
from datetime import datetime


class ApiEmulator(object):
def __init__(self):
self.name='api'
self.name = 'api'

def normalise_request_json(self,data):
def normalise_request_json(self, data):
_data = {}
_data["text"]=data['q'][0]
_data["text"] = data['q'][0]
return _data

def normalise_response_json(self,data):
def normalise_response_json(self, data):
return {
"id": unicode(uuid.uuid1()),
"timestamp": datetime.now().isoformat("T"),
Expand All @@ -19,12 +20,12 @@ def normalise_response_json(self,data):
"resolvedQuery": data["text"],
"action": None,
"actionIncomplete": None,
"parameters": {key:val for key,val in data["entities"].items()},
"parameters": {key: val for key, val in data["entities"].items()},
"contexts": [],
"metadata": {
"intentId": unicode(uuid.uuid1()),
"webhookUsed": "false",
"intentName": data["intent"]
"intentId": unicode(uuid.uuid1()),
"webhookUsed": "false",
"intentName": data["intent"]
},
"fulfillment": {},
"score": None,
Expand Down
38 changes: 17 additions & 21 deletions src/emulators/luis.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@


class LUISEmulator(object):
def __init__(self):
self.name='luis'
self.name = 'luis'

def normalise_request_json(self,data):
def normalise_request_json(self, data):
_data = {}
_data["text"]=data['q'][0]
_data["text"] = data['q'][0]
return _data

def normalise_response_json(self,data):
def normalise_response_json(self, data):
return {
"query": data["text"],
"query": data["text"],
"topScoringIntent": {
"intent": "inform",
"score": None
"intent": "inform",
"score": None
},
"entities": [
{
"entity": e[0],
"type": e[1],
"startIndex": None,
"endIndex": None,
"score": None
} for e in data["entities"]
]
}


"entities": [
{
"entity": e[0],
"type": e[1],
"startIndex": None,
"endIndex": None,
"score": None
} for e in data["entities"]
]
}
22 changes: 11 additions & 11 deletions src/emulators/wit.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

class WitEmulator(object):
def __init__(self):
self.name='wit'
self.name = 'wit'

def normalise_request_json(self,data):
def normalise_request_json(self, data):
_data = {}
_data["text"]=data['q'][0]
_data["text"] = data['q'][0]
return _data

def normalise_response_json(self,data):
def normalise_response_json(self, data):
print('plain response {0}'.format(data))
return [
{
"_text": data["text"],
"confidence": None,
"intent": data["intent"],
"entities" : {key:{"confidence":None,"type":"value","value":val} for key,val in data["entities"].items()}
}
{
"_text": data["text"],
"confidence": None,
"intent": data["intent"],
"entities": {key: {"confidence": None, "type": "value", "value": val} for key, val in
data["entities"].items()}
}
]
11 changes: 6 additions & 5 deletions src/featurizers/mitie_featurizer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from mitie import *
import numpy as np


class MITIEFeaturizer(object):
def __init__(self,fe_file):
def __init__(self, fe_file):
self.feature_extractor = total_word_feature_extractor(fe_file)
self.ndim = self.feature_extractor.num_dimensions
def create_bow_vecs(self,sentences):
X=np.zeros((len(sentences),self.ndim))

def create_bow_vecs(self, sentences):
X = np.zeros((len(sentences), self.ndim))
for idx, sent in enumerate(sentences):
tokens = tokenize(sent)
vec = np.zeros(self.ndim)
for token in tokens:
vec += self.feature_extractor.get_feature_vector(token)
X[idx,:] = vec / len(tokens)
X[idx, :] = vec / len(tokens)
return X
13 changes: 7 additions & 6 deletions src/featurizers/spacy_featurizer.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import spacy
import numpy as np


class SpacyFeaturizer(object):
def __init__(self,nlp):
def __init__(self, nlp):
self.nlp = nlp
self.ndim = 300
def create_bow_vecs(self,sentences):
X = np.zeros((len(sentences),self.ndim))

def create_bow_vecs(self, sentences):
X = np.zeros((len(sentences), self.ndim))
for idx, sentence in enumerate(sentences):
doc = self.nlp(sentence)
vec = np.zeros(self.ndim)
for token in doc:
vec += token.vector
X[idx,:] = vec / len(doc)
return X
X[idx, :] = vec / len(doc)
return X
22 changes: 11 additions & 11 deletions src/interpreters/mitie_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
from mitie import *
from rasa_nlu import Interpreter


class MITIEInterpreter(Interpreter):
def __init__(self,intent_classifier=None,entity_extractor=None,feature_extractor=None,**kwargs):
self.extractor = named_entity_extractor(entity_extractor,feature_extractor)
self.classifier = text_categorizer(intent_classifier,feature_extractor)
def get_entities(self,tokens):
def __init__(self, intent_classifier=None, entity_extractor=None, feature_extractor=None, **kwargs):
self.extractor = named_entity_extractor(entity_extractor, feature_extractor)
self.classifier = text_categorizer(intent_classifier, feature_extractor)

def get_entities(self, tokens):
d = {}
entities = self.extractor.extract_entities(tokens)
for e in entities:
_range = e[0]
d[e[1]] = " ".join(tokens[i] for i in _range)
d[e[1]] = " ".join(tokens[i] for i in _range)
return d

def get_intent(self,tokens):
label, _ = self.classifier(tokens) # don't use the score
def get_intent(self, tokens):
label, _ = self.classifier(tokens) # don't use the score
return label

def parse(self,text):
def parse(self, text):
tokens = tokenize(text)
intent = self.get_intent(tokens)
entities = self.get_entities(tokens)

return {'text':text,'intent':intent,'entities': entities}

return {'text': text, 'intent': intent, 'entities': entities}
Loading

0 comments on commit 2eefe06

Please sign in to comment.