Skip to content

Commit

Permalink
Merge pull request #844 from RasaHQ/auto-pep8-all
Browse files Browse the repository at this point in the history
testing everything with pep8
  • Loading branch information
tmbo committed Feb 14, 2018
2 parents 1bf2680 + fd081ab commit 0b57584
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 30 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -26,8 +26,9 @@ before_script:
- mkdir $HOME/tmp
- export TMPDIR=$HOME/tmp
script:
- py.test tests/base --cov rasa_nlu --pep8 -v --cov-append
- py.test tests/training --cov rasa_nlu --pep8 -v --cov-append
- py.test --pep8 -m pep8
- py.test tests/base --cov rasa_nlu -v --cov-append
- py.test tests/training --cov rasa_nlu -v --cov-append
after_success:
- coveralls
jobs:
Expand Down
3 changes: 2 additions & 1 deletion rasa_nlu/classifiers/keyword_intent_classifier.py
Expand Up @@ -31,7 +31,8 @@ def parse(self, text):

_text = text.lower()

def is_present(x): return x in _text
def is_present(x):
return x in _text

if any(map(is_present, self.his)):
return "greet"
Expand Down
3 changes: 1 addition & 2 deletions rasa_nlu/classifiers/mitie_intent_classifier.py
Expand Up @@ -22,7 +22,6 @@


class MitieIntentClassifier(Component):

name = "intent_classifier_mitie"

provides = ["intent"]
Expand All @@ -37,7 +36,7 @@ def required_packages(cls):
# type: () -> List[Text]
return ["mitie"]

def train(self, training_data, config, **kwargs):
def train(self, training_data, config, **kwargs):
# type: (TrainingData, RasaNLUConfig, **Any) -> None
import mitie

Expand Down
2 changes: 1 addition & 1 deletion rasa_nlu/download.py
Expand Up @@ -42,7 +42,7 @@ def download_mitie_fe_file(fe_file): # pragma: no cover
response = requests.get(_fe_file_url, stream=True)

with io.open(fe_file, "wb") as output:
for data in tqdm(response.iter_content(chunk_size=1024*1024), unit='MB', unit_scale=True):
for data in tqdm(response.iter_content(chunk_size=1024 * 1024), unit='MB', unit_scale=True):
output.write(data)
logger.debug("file written! {0}, {1}".format(fe_file, os.path.exists(fe_file)))

Expand Down
2 changes: 1 addition & 1 deletion rasa_nlu/emulators/luis.py
Expand Up @@ -51,5 +51,5 @@ def normalise_response_json(self, data):
"endIndex": None,
"score": None
} for e in data["entities"]
] if "entities" in data else []
] if "entities" in data else []
}
9 changes: 6 additions & 3 deletions rasa_nlu/evaluate.py
Expand Up @@ -22,6 +22,7 @@
"ordinal", "phone-number", "timezone", "temperature", "time", "url", "volume"}
entity_processors = {"ner_synonyms"}


def create_argparser(): # pragma: no cover
import argparse
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -94,6 +95,7 @@ def log_evaluation_table(test_y, preds): # pragma: no cover
logger.info("Accuracy: {}".format(accuracy))
logger.info("Classification report: \n{}".format(report))


def remove_empty_intent_examples(targets, predictions):
"""Removes those examples without intent."""
targets = np.array(targets)
Expand All @@ -103,7 +105,7 @@ def remove_empty_intent_examples(targets, predictions):
return targets, predictions


def prepare_data(data, cutoff = 5):
def prepare_data(data, cutoff=5):
"""Remove intent groups with less than cutoff instances."""
data = data.sorted_intent_examples()
logger.info("Raw data intent examples: {}".format(len(data)))
Expand Down Expand Up @@ -461,6 +463,7 @@ def run_cv_evaluation(data, n_folds, nlu_config):

return dict(results)


if __name__ == '__main__': # pragma: no cover
parser = create_argparser()
args = parser.parse_args()
Expand All @@ -476,10 +479,10 @@ def run_cv_evaluation(data, n_folds, nlu_config):

if args.mode == "crossvalidation":
data = training_data.load_data(args.data)
data = prepare_data(data, cutoff = 5)
data = prepare_data(data, cutoff=5)
results = run_cv_evaluation(data, int(args.folds), nlu_config)
logger.info("CV evaluation (n={})".format(args.folds))
for k,v in results.items():
for k, v in results.items():
logger.info("{}: {:.3f} ({:.3f})".format(k, np.mean(v), np.std(v)))
elif args.mode == "evaluation":
run_evaluation(nlu_config, args.model)
Expand Down
2 changes: 1 addition & 1 deletion rasa_nlu/training_data/formats/dialogflow.py
Expand Up @@ -20,6 +20,7 @@
DIALOGFLOW_ENTITIES = "dialogflow_entities"
DIALOGFLOW_ENTITY_ENTRIES = "dialogflow_entity_entries"


class DialogflowReader(TrainingDataReader):
def read(self, fn, **kwargs):
# type: ([Text]) -> TrainingData
Expand All @@ -42,7 +43,6 @@ def read(self, fn, **kwargs):
elif fformat == DIALOGFLOW_ENTITIES:
return self._read_entities(examples_js)


def _read_intent(self, intent_js, examples_js):
"""Reads the intent and examples from respective jsons."""
intent = intent_js.get("name")
Expand Down
2 changes: 1 addition & 1 deletion rasa_nlu/training_data/formats/luis.py
Expand Up @@ -48,4 +48,4 @@ def read_from_json(self, js, **kwargs):
if intent:
data["intent"] = intent
training_examples.append(Message(text, data))
return TrainingData(training_examples, regex_features=regex_features)
return TrainingData(training_examples, regex_features=regex_features)
9 changes: 4 additions & 5 deletions rasa_nlu/training_data/formats/markdown.py
Expand Up @@ -147,10 +147,10 @@ def dumps(self, training_data):
def _generate_training_examples_md(self, training_data):
"""generates markdown training examples."""
training_examples = sorted([e.as_dict() for e in training_data.training_examples],
key=lambda k: k['intent'])
key=lambda k: k['intent'])
md = u''
for i, example in enumerate(training_examples):
if i == 0 or training_examples[i-1]['intent'] != example['intent']:
if i == 0 or training_examples[i - 1]['intent'] != example['intent']:
md += self._generate_section_header_md(INTENT, example['intent'], i != 0)

md += self._generate_item_md(self._generate_message_md(example))
Expand All @@ -160,7 +160,7 @@ def _generate_training_examples_md(self, training_data):
def _generate_synonyms_md(self, training_data):
"""generates markdown for entity synomyms."""
entity_synonyms = sorted(training_data.entity_synonyms.items(),
key=lambda x: x[1])
key=lambda x: x[1])
md = u''
for i, synonym in enumerate(entity_synonyms):
if i == 0 or entity_synonyms[i - 1][1] != synonym[1]:
Expand All @@ -183,7 +183,7 @@ def _generate_regex_features_md(self, training_data):

return md

def _generate_section_header_md(self, section_type, title, prepend_newline = True):
def _generate_section_header_md(self, section_type, title, prepend_newline=True):
"""generates markdown section header."""
prefix = "\n" if prepend_newline else ""
return prefix + "## {}:{}\n".format(section_type, title)
Expand Down Expand Up @@ -218,4 +218,3 @@ def _generate_entity_md(self, text, entity):
entity_type += ":{}".format(entity['value'])

return '[{}]({})'.format(entity_text, entity_type)

2 changes: 1 addition & 1 deletion rasa_nlu/training_data/formats/wit.py
Expand Up @@ -41,4 +41,4 @@ def read_from_json(self, js, **kwargs):
if entities is not None:
data["entities"] = entities
training_examples.append(Message(text, data))
return TrainingData(training_examples)
return TrainingData(training_examples)
8 changes: 4 additions & 4 deletions rasa_nlu/training_data/training_data.py
Expand Up @@ -175,13 +175,13 @@ def validate(self):
for intent, count in self.examples_per_intent.items():
if count < self.MIN_EXAMPLES_PER_INTENT:
warnings.warn("Intent '{}' has only {} training examples! "
"Minimum is {}, training may fail.".format(intent, count,
self.MIN_EXAMPLES_PER_INTENT))
"Minimum is {}, training may fail.".format(intent, count,
self.MIN_EXAMPLES_PER_INTENT))
for entity_type, count in self.examples_per_entity.items():
if count < self.MIN_EXAMPLES_PER_ENTITY:
warnings.warn("Entity '{}' has only {} training examples! "
"minimum is {}, training may fail.".format(entity_type, count,
self.MIN_EXAMPLES_PER_ENTITY))
"minimum is {}, training may fail.".format(entity_type, count,
self.MIN_EXAMPLES_PER_ENTITY))

def print_stats(self):
logger.info("Training data stats: \n" +
Expand Down
2 changes: 1 addition & 1 deletion rasa_nlu/training_data/util.py
Expand Up @@ -23,4 +23,4 @@ def transform_entity_synonyms(synonyms, known_synonyms=None):
def check_duplicate_synonym(entity_synonyms, text, syn, context_str=""):
if text in entity_synonyms and entity_synonyms[text] != syn:
logger.warning("Found inconsistent entity synonyms while {0}, overwriting {1}->{2}"
"with {1}->{2} during merge".format(context_str, text, entity_synonyms[text], syn))
"with {1}->{2} during merge".format(context_str, text, entity_synonyms[text], syn))
4 changes: 4 additions & 0 deletions setup.cfg
Expand Up @@ -3,7 +3,11 @@
pep8maxlinelength = 120
pep8ignore =
docs/conf.py ALL
*.py W503
*.py E126

# ignoring W503: line break occurred before a binary operator
# ignoring E126: continuation line over-indented for hanging indent

[metadata]
description-file = README.md
2 changes: 1 addition & 1 deletion setup.py
@@ -1,7 +1,7 @@
from setuptools import setup

__version__ = None # Avoids IDE errors, but actual version is read from version.py
exec (open('rasa_nlu/version.py').read())
exec(open('rasa_nlu/version.py').read())

tests_requires = [
"pytest",
Expand Down
2 changes: 1 addition & 1 deletion tests/base/test_emulators.py
Expand Up @@ -70,7 +70,7 @@ def test_luis_response():
"endIndex": None,
"score": None
} for e in data["entities"]
]
]
}


Expand Down
11 changes: 6 additions & 5 deletions tests/evaluation.py
Expand Up @@ -21,6 +21,7 @@

logging.basicConfig(level="DEBUG")


@pytest.fixture(scope="module")
def duckling_interpreter(component_builder):
_conf = utilities.base_test_conf("")
Expand Down Expand Up @@ -214,11 +215,11 @@ def test_prepare_data():
td = training_data.load_data('data/examples/rasa/demo-rasa.json')
clean_data = prepare_data(td, 0)
unique_intents = sorted(set([i.data["intent"] for i in clean_data]))
assert(unique_intents == ['affirm', 'goodbye', 'greet', 'restaurant_search'])
assert (unique_intents == ['affirm', 'goodbye', 'greet', 'restaurant_search'])

clean_data = prepare_data(td, 10)
unique_intents = sorted(set([i.data["intent"] for i in clean_data]))
assert(unique_intents == ['affirm', 'restaurant_search'])
assert (unique_intents == ['affirm', 'restaurant_search'])


def test_run_cv_evaluation():
Expand All @@ -230,11 +231,11 @@ def test_run_cv_evaluation():
np.seed(2018)
results = run_cv_evaluation(td, n_folds, nlu_config)

rel_tol=1e-09
abs_tol=0.01
rel_tol = 1e-09
abs_tol = 0.01

acc = np.mean(results["accuracy"])
exp_acc = 0.65 # expected result
exp_acc = 0.65 # expected result
np.testing.assert_approx_equal(acc, exp_acc, significant=5)


Expand Down

0 comments on commit 0b57584

Please sign in to comment.