Skip to content

Commit f869da7

Browse files
authored
Merge pull request #116 from snipsco/release/0.14.3
Release 0.14.3
2 parents 079631a + ad06bf3 commit f869da7

File tree

14 files changed

+383
-252
lines changed

14 files changed

+383
-252
lines changed

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ matrix:
44
include:
55
- python: 2.7
66
env: TOXENV=py27
7-
- python: 3.4
8-
env: TOXENV=py34
97
- python: 3.5
108
env: TOXENV=py35
119
- python: 3.6
1210
env: TOXENV=py36
11+
- python: 3.7
12+
env: TOXENV=py37
1313

14-
install:
15-
- pip install --upgrade --pre tox
14+
install: pip install --upgrade --pre tox
1615

17-
script:
18-
- tox
16+
script: tox

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.14.3] - 2019-05-10
5+
### Added
6+
- Add optional parameter `intents_filter` to metrics APIs [#115](https://github.com/snipsco/snips-nlu-metrics/pull/115)
7+
48
## [0.14.2] - 2019-03-21
59
### Added
610
- Number of exact parsings
@@ -44,6 +48,7 @@ All notable changes to this project will be documented in this file.
4448
- Samples
4549

4650

51+
[0.14.3]: https://github.com/snipsco/snips-nlu-metrics/compare/0.14.2...0.14.3
4752
[0.14.2]: https://github.com/snipsco/snips-nlu-metrics/compare/0.14.1...0.14.2
4853
[0.14.1]: https://github.com/snipsco/snips-nlu-metrics/compare/0.14.0...0.14.1
4954
[0.14.0]: https://github.com/snipsco/snips-nlu-metrics/compare/0.13.0...0.14.0

MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
recursive-include samples *
12
include snips_nlu_metrics/__version__
2-
include README.rst LICENSE
3+
include README.rst LICENSE CHANGELOG.md
34
global-exclude __pycache__ *.py[cod]

README.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ API as follows:
9494

9595
.. code-block:: python
9696
97-
from snips_nlu import load_resources, SnipsNLUEngine
97+
from snips_nlu import SnipsNLUEngine
9898
from snips_nlu_metrics import compute_train_test_metrics, compute_cross_val_metrics
9999
100-
load_resources("en")
101-
102100
tt_metrics = compute_train_test_metrics(train_dataset="samples/train_dataset.json",
103101
test_dataset="samples/test_dataset.json",
104102
engine_class=SnipsNLUEngine)

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@
3838
author="Adrien Ball",
3939
author_email="adrien.ball@snips.ai",
4040
license="Apache 2.0",
41+
url="https://github.com/snipsco/snips-nlu-metrics",
4142
classifiers=[
4243
"Programming Language :: Python :: 2",
4344
"Programming Language :: Python :: 2.7",
4445
"Programming Language :: Python :: 3",
45-
"Programming Language :: Python :: 3.4",
4646
"Programming Language :: Python :: 3.5",
4747
"Programming Language :: Python :: 3.6",
48+
"Programming Language :: Python :: 3.7",
4849
],
50+
keywords="metrics nlu nlp intent slots entity parsing",
4951
extras_require=extras_require,
5052
install_requires=install_requires,
5153
packages=packages,

snips_nlu_metrics/__version__

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.14.2
1+
0.14.3

snips_nlu_metrics/metrics.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io
44
import json
55
import logging
6-
from builtins import map
76

87
from future.utils import iteritems
98
from joblib import Parallel, delayed
@@ -25,20 +24,20 @@ def compute_cross_val_metrics(
2524
dataset, engine_class, nb_folds=5, train_size_ratio=1.0,
2625
drop_entities=False, include_slot_metrics=True,
2726
slot_matching_lambda=None, progression_handler=None, num_workers=1,
28-
seed=None, out_of_domain_utterances=None):
27+
seed=None, out_of_domain_utterances=None, intents_filter=None):
2928
"""Compute end-to-end metrics on the dataset using cross validation
3029
3130
Args:
32-
dataset (dict or str): Dataset or path to dataset
33-
engine_class: Python class to use for training and inference, this
31+
dataset (dict or str): dataset or path to dataset
32+
engine_class: python class to use for training and inference, this
3433
class must inherit from `Engine`
35-
nb_folds (int, optional): Number of folds to use for cross validation
34+
nb_folds (int, optional): number of folds to use for cross validation
3635
(default=5)
3736
train_size_ratio (float, optional): ratio of intent utterances to use
3837
for training (default=1.0)
39-
drop_entities (bool, optional): Specify whether or not all entity
38+
drop_entities (bool, optional): specify whether or not all entity
4039
values should be removed from training data (default=False)
41-
include_slot_metrics (bool, optional): If false, the slots metrics and
40+
include_slot_metrics (bool, optional): if false, the slots metrics and
4241
the slots parsing errors will not be reported (default=True)
4342
slot_matching_lambda (lambda, optional):
4443
lambda expected_slot, actual_slot -> bool,
@@ -52,9 +51,13 @@ class must inherit from `Engine`
5251
num_workers (int, optional): number of workers to use. Each worker
5352
is assigned a certain number of splits (default=1)
5453
seed (int, optional): seed for the split creation
55-
out_of_domain_utterances (list, optional): If defined, list of
56-
out-of-domain utterances to be added to the pool of test utterances
54+
out_of_domain_utterances (list, optional): if defined, list of
55+
out-of-domain utterances to be added to the pool of test utterances
5756
in each split
57+
intents_filter (list of str, optional): if defined, at inference times
58+
test utterances will be restricted to the ones belonging to this
59+
filter. Moreover, if the parsing API allows it, the inference will
60+
be made using this intents filter.
5861
5962
Returns:
6063
dict: Metrics results containing the following data
@@ -72,7 +75,7 @@ class must inherit from `Engine`
7275
try:
7376
splits = create_shuffle_stratified_splits(
7477
dataset, nb_folds, train_size_ratio, drop_entities,
75-
seed, out_of_domain_utterances)
78+
seed, out_of_domain_utterances, intents_filter)
7679
except NotEnoughDataError as e:
7780
logger.warning("Skipping metrics computation because of: %s"
7881
% e.message)
@@ -93,14 +96,14 @@ def compute_metrics(split_):
9396
logger.info("Computing metrics for dataset split ...")
9497
return compute_split_metrics(
9598
engine_class, split_, intent_list, include_slot_metrics,
96-
slot_matching_lambda)
99+
slot_matching_lambda, intents_filter)
97100

98101
effective_num_workers = min(num_workers, len(splits))
99102
if effective_num_workers > 1:
100103
parallel = Parallel(n_jobs=effective_num_workers)
101104
results = parallel(delayed(compute_metrics)(split) for split in splits)
102105
else:
103-
results = map(compute_metrics, splits)
106+
results = [compute_metrics(s) for s in splits]
104107

105108
for result in enumerate(results):
106109
split_index, (split_metrics, errors, confusion_matrix) = result
@@ -137,7 +140,7 @@ def compute_metrics(split_):
137140

138141
def compute_train_test_metrics(
139142
train_dataset, test_dataset, engine_class, include_slot_metrics=True,
140-
slot_matching_lambda=None):
143+
slot_matching_lambda=None, intents_filter=None):
141144
"""Compute end-to-end metrics on `test_dataset` after having trained on
142145
`train_dataset`
143146
@@ -155,6 +158,10 @@ class must inherit from `Engine`
155158
metrics, otherwise exact match will be used.
156159
`expected_slot` corresponds to the slot as defined in the dataset,
157160
and `actual_slot` corresponds to the slot as returned by the NLU
161+
intents_filter (list of str, optional): if defined, at inference times
162+
test utterances will be restricted to the ones belonging to this
163+
filter. Moreover, if the parsing API allows it, the inference will
164+
be made using this intents filter.
158165
159166
Returns
160167
dict: Metrics results containing the following data
@@ -184,12 +191,13 @@ class must inherit from `Engine`
184191
(intent_name, utterance)
185192
for intent_name, intent_data in iteritems(test_dataset[INTENTS])
186193
for utterance in intent_data[UTTERANCES]
194+
if intents_filter is None or intent_name in intents_filter
187195
]
188196

189197
logger.info("Computing metrics...")
190198
metrics, errors, confusion_matrix = compute_engine_metrics(
191199
engine, test_utterances, intent_list, include_slot_metrics,
192-
slot_matching_lambda)
200+
slot_matching_lambda, intents_filter)
193201
metrics = compute_precision_recall_f1(metrics)
194202
average_metrics = compute_average_metrics(metrics)
195203
nb_utterances = {intent: len(data[UTTERANCES])

snips_nlu_metrics/tests/mock_engine.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from snips_nlu_metrics import Engine
44

55

6-
def dummy_parsing_result(text):
6+
def dummy_parsing_result(text, intent_name=None):
77
return {
88
"input": text,
99
"intent": {
10-
"intentName": None,
10+
"intentName": intent_name,
1111
"probability": 0.5
1212
},
1313
"slots": []
@@ -25,6 +25,26 @@ def parse(self, text):
2525
return dummy_parsing_result(text)
2626

2727

28+
class KeyWordMatchingEngine(Engine):
29+
def __init__(self):
30+
self.fitted = False
31+
self.intents_list = []
32+
33+
def fit(self, dataset):
34+
self.fitted = True
35+
self.intents_list = sorted(dataset["intents"])
36+
37+
def parse(self, text, intents_filter=None):
38+
intent = None
39+
for intent_name in self.intents_list:
40+
if intent_name in text:
41+
intent = intent_name
42+
break
43+
if intents_filter is not None and intent not in intents_filter:
44+
intent = None
45+
return dummy_parsing_result(text, intent)
46+
47+
2848
class MockEngineSegfault(Engine):
2949
def __init__(self):
3050
self.fitted = False

0 commit comments

Comments
 (0)