class TestAll: def setup(self): self.cont = IntentContainer('temp') def test_simple(self): self.cont.add_intent('hello', ['hello', 'hi', 'how are you', 'whats up']) self.cont.add_intent( 'goodbye', ['see you', 'later', 'bye', 'goodbye', 'another time']) self.cont.train(False) data = self.cont.calc_intent('whats up') assert data.name == 'hello' assert data.conf > 0.5 def test_single_extraction(self): self.cont.add_intent('drive', [ 'drive to {place}', 'driver over to {place}', 'navigate to {place}' ]) self.cont.add_intent('swim', ['swim to {island}', 'swim across {ocean}']) self.cont.train(False) data = self.cont.calc_intent('navigate to los angelos') assert data.name == 'drive' assert data.conf > 0.5 assert data.matches == {'place': 'los angelos'} data = self.cont.calc_intent('swim to tahiti') assert data.name == 'swim' assert data.conf > 0.5 assert data.matches == {'island': 'tahiti'} def test_single_extraction_front_back(self): self.cont.add_intent('start.timer', [ 'Timer {duration}', '{duration} timer', ]) self.cont.train(False) data = self.cont.calc_intent('10 minute timer') assert data.name == 'start.timer' assert data.conf > 0.5 assert data.matches == {'duration': '10 minute'} def test_multi_extraction_easy(self): self.cont.add_intent('search', [ '(search for|find) {query}', '(search for|find) {query} (using|on) {engine}', '(using|on) {engine}, (search for|find) {query}' ]) self.cont.add_intent('order', [ 'order some {food} from {store}', 'place an order for {food} on {store}' ]) self.cont.train(False) data = self.cont.calc_intent('search for funny dog videos') assert data.name == 'search' assert data.matches == {'query': 'funny dog videos'} assert data.conf > 0.5 data = self.cont.calc_intent('search for bananas using foodio') assert data.name == 'search' assert data.matches == {'query': 'bananas', 'engine': 'foodio'} assert data.conf > 0.5 data = self.cont.calc_intent( 'search for big furry cats using the best search engine') assert data.name == 'search' assert data.matches == { 'query': 'big furry cats', 'engine': 'the best search engine' } assert data.conf > 0.5 data = self.cont.calc_intent( 'place an order for a loaf of bread on foodbuywebsite') assert data.name == 'order' assert data.matches == { 'food': 'a loaf of bread', 'store': 'foodbuywebsite' } assert data.conf > 0.5 def test_extraction_dependence(self): self.cont.add_intent('search', ['wiki {query}']) self.cont.train(False) data = self.cont.calc_intent('wiki') assert data.conf < 0.5 def test_entity_recognition(self): self.cont.add_intent('weather', ['weather for {place} {time}'], True) self.cont.add_intent('time', [ 'what time is it', 'whats the time right now', 'what time is it at the moment', 'currently, what time is it' ], True) self.cont.add_entity( 'place', ['los angeles', 'california', 'new york', 'chicago']) self.cont.add_entity('time', [ 'right now', 'currently', 'at the moment', ]) self.cont.train(False) data = self.cont.calc_intent('weather for los angeles right now') assert data.name == 'weather' assert data.matches == {'place': 'los angeles', 'time': 'right now'} assert data.conf > 0.5 def teardown(self): if isdir('temp'): rmtree('temp')
themes = list([ 'Recognition', 'Relationship with Seniors', 'Job Satisfaction', 'Communication', 'Leadership', 'Training and Process Orientation', 'Colleague Relations', 'Personal Growth', 'Vision Alignment', 'Resources and Benefits', 'Company Feedback', 'Others' ]) for item in themes: file = open("/Users/jessicasethi/Documents/intents/" + str(item) + ".txt", "r") container.load_file( item, "/Users/jessicasethi/Documents/intents/" + str(item) + ".txt") file.close() container.train() with open("/Users/jessicasethi/Documents/testing", "rb") as f: tests = pickle.load(f) ''' total = len(tests) count = 0 for item in tests: data = container.calc_intent(item[0]) if(data.name == item[1]): count += 1 else: print(data.sent) print(data.conf) print(item[1])
class TestIntentContainer: test_lines = ['this is a test', 'another test'] other_lines = ['something else', 'this is a different thing'] def setup(self): self.cont = IntentContainer('temp') def test_add_intent(self): self.cont.add_intent('test', self.test_lines) self.cont.add_intent('other', self.other_lines) def test_load_intent(self): if not isdir('temp'): mkdir('temp') fn1 = join('temp', 'test.txt') with open(fn1, 'w') as f: f.writelines(self.test_lines) fn2 = join('temp', 'other.txt') with open(fn2, 'w') as f: f.writelines(self.other_lines) self.cont.load_intent('test', fn1) self.cont.load_intent('other', fn1) assert len(self.cont.intents.train_data.sent_lists) == 2 def test_train(self): def test(a, b): self.setup() self.test_add_intent() self.cont.train(a, b) test(False, False) test(True, True) def test_calc_intents(self): self.test_add_intent() self.cont.train(False) intents = self.cont.calc_intents('this is another test') assert (intents[0].conf > intents[1].conf) == (intents[0].name == 'test') assert self.cont.calc_intent('this is another test').name == 'test' def test_empty(self): self.cont.train(False) self.cont.calc_intent('hello') def _test_entities(self, namespace): self.cont.add_intent(namespace + 'intent', ['test {ent}']) self.cont.add_entity(namespace + 'ent', ['one']) self.cont.train(False) data = self.cont.calc_intent('test one') high_conf = data.conf assert data.conf > 0.5 assert data['ent'] == 'one' data = self.cont.calc_intent('test two') assert high_conf > data.conf assert 'ent' not in data def test_regular_entities(self): self._test_entities('') def test_namespaced_entities(self): self._test_entities('SkillName:') def teardown(self): if isdir('temp'): rmtree('temp')
class TestIntentContainer: test_lines = ['this is a test', 'another test'] other_lines = ['something else', 'this is a different thing'] def setup(self): self.cont = IntentContainer('temp') def test_add_intent(self): self.cont.add_intent('test', self.test_lines) self.cont.add_intent('other', self.other_lines) def test_load_intent(self): if not isdir('temp'): mkdir('temp') fn1 = join('temp', 'test.txt') with open(fn1, 'w') as f: f.writelines(self.test_lines) fn2 = join('temp', 'other.txt') with open(fn2, 'w') as f: f.writelines(self.other_lines) self.cont.load_intent('test', fn1) self.cont.load_intent('other', fn1) assert len(self.cont.intents.train_data.sent_lists) == 2 def test_train(self): def test(a, b): self.setup() self.test_add_intent() self.cont.train(a, b) test(False, False) test(True, True) def test_instantiate_from_disk(self): # train and cache (i.e. persist) self.setup() self.test_add_intent() self.cont.train() # instantiate from disk (load cached files) self.setup() self.cont.instantiate_from_disk() assert len(self.cont.intents.train_data.sent_lists) == 0 assert len(self.cont.intents.objects_to_train) == 0 assert len(self.cont.intents.objects) == 2 def _create_large_intent(self, depth): if depth == 0: return '(a|b|)' return '{0} {0}'.format(self._create_large_intent(depth - 1)) @pytest.mark.skipif(not os.environ.get('RUN_LONG'), reason="Takes a long time") def test_train_timeout(self): self.cont.add_intent('a', [ ' '.join( random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(5)) for __ in range(300) ]) self.cont.add_intent('b', [ ' '.join( random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(5)) for __ in range(300) ]) a = monotonic() self.cont.train(True, timeout=1) b = monotonic() assert b - a <= 2 a = monotonic() self.cont.train(True, timeout=1) b = monotonic() assert b - a <= 0.1 def test_train_timeout_subprocess(self): self.cont.add_intent('a', [ ' '.join( random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(5)) for __ in range(300) ]) self.cont.add_intent('b', [ ' '.join( random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(5)) for __ in range(300) ]) a = monotonic() assert not self.cont.train_subprocess(timeout=0.1) b = monotonic() assert b - a <= 1 def test_train_subprocess(self): self.cont.add_intent('timer', [ 'set a timer for {time} minutes', ]) self.cont.add_entity('time', ['#', '##', '#:##', '##:##']) assert self.cont.train_subprocess(False, timeout=20) intent = self.cont.calc_intent('set timer for 3 minutes') assert intent.name == 'timer' assert intent.matches == {'time': '3'} def test_calc_intents(self): self.test_add_intent() self.cont.train(False) intents = self.cont.calc_intents('this is another test') assert (intents[0].conf > intents[1].conf) == (intents[0].name == 'test') assert self.cont.calc_intent('this is another test').name == 'test' def test_empty(self): self.cont.train(False) self.cont.calc_intent('hello') def _test_entities(self, namespace): self.cont.add_intent(namespace + 'intent', ['test {ent}']) self.cont.add_entity(namespace + 'ent', ['one']) self.cont.train(False) data = self.cont.calc_intent('test one') high_conf = data.conf assert data.conf > 0.5 assert data['ent'] == 'one' data = self.cont.calc_intent('test two') assert high_conf > data.conf assert 'ent' not in data def test_regular_entities(self): self._test_entities('') def test_namespaced_entities(self): self._test_entities('SkillName:') def test_remove(self): self.test_add_intent() self.cont.train(False) assert self.cont.calc_intent('This is a test').conf == 1.0 self.cont.remove_intent('test') assert self.cont.calc_intent('This is a test').conf < 0.5 self.cont.add_intent('thing', ['A {thing}']) self.cont.add_entity('thing', ['thing']) self.cont.train(False) assert self.cont.calc_intent('A dog').conf < 0.5 assert self.cont.calc_intent('A thing').conf == 1.0 self.cont.remove_entity('thing') assert self.cont.calc_intent('A dog').conf == 1.0 def test_overlap(self): self.cont.add_intent('song', ['play {song}']) self.cont.add_intent('news', ['play the news']) self.cont.train(False) assert self.cont.calc_intent('play the news').name == 'news' def test_overlap_backwards(self): self.cont.add_intent('song', ['play {song}']) self.cont.add_intent('news', ['play the news']) self.cont.train(False) assert self.cont.calc_intent('play the news').name == 'news' def test_generalize(self): self.cont.add_intent( 'timer', ['set a timer for {time} minutes', 'make a {time} minute timer']) self.cont.add_entity('time', ['#', '##', '#:##', '##:##']) self.cont.train(False) intent = self.cont.calc_intent('make a timer for 3 minute') assert intent.name == 'timer' assert intent.matches == {'time': '3'} def teardown(self): if isdir('temp'): rmtree('temp')
class TestIntentContainer: test_lines = ['this is a test', 'another test'] other_lines = ['something else', 'this is a different thing'] def setup(self): self.cont = IntentContainer('temp') def test_add_intent(self): self.cont.add_intent('test', self.test_lines) self.cont.add_intent('other', self.other_lines) def test_load_intent(self): if not isdir('temp'): mkdir('temp') fn1 = join('temp', 'test.txt') with open(fn1, 'w') as f: f.writelines(self.test_lines) fn2 = join('temp', 'other.txt') with open(fn2, 'w') as f: f.writelines(self.other_lines) self.cont.load_intent('test', fn1) self.cont.load_intent('other', fn1) assert len(self.cont.intents.train_data.sent_lists) == 2 def test_train(self): def test(a, b): self.setup() self.test_add_intent() self.cont.train(a, b) test(False, False) test(True, True) def test_calc_intents(self): self.test_add_intent() self.cont.train(False) intents = self.cont.calc_intents('this is another test') assert (intents[0].conf > intents[1].conf) == (intents[0].name == 'test') assert self.cont.calc_intent('this is another test').name == 'test' def test_empty(self): self.cont.train(False) self.cont.calc_intent('hello') def _test_entities(self, namespace): self.cont.add_intent(namespace + 'intent', [ 'test {ent}' ]) self.cont.add_entity(namespace + 'ent', [ 'one' ]) self.cont.train(False) data = self.cont.calc_intent('test one') high_conf = data.conf assert data.conf > 0.5 assert data['ent'] == 'one' data = self.cont.calc_intent('test two') assert high_conf > data.conf assert 'ent' not in data def test_regular_entities(self): self._test_entities('') def test_namespaced_entities(self): self._test_entities('SkillName:') def test_remove(self): self.test_add_intent() self.cont.train(False) assert self.cont.calc_intent('This is a test').conf == 1.0 self.cont.remove_intent('test') assert self.cont.calc_intent('This is a test').conf < 0.5 self.cont.add_intent('thing', ['A {thing}']) self.cont.add_entity('thing', ['thing']) self.cont.train(False) assert self.cont.calc_intent('A dog').conf < 0.5 assert self.cont.calc_intent('A thing').conf == 1.0 self.cont.remove_entity('thing') assert self.cont.calc_intent('A dog').conf == 1.0 def test_overlap(self): self.cont.add_intent('song', ['play {song}']) self.cont.add_intent('news', ['play the news']) self.cont.train(False) assert self.cont.calc_intent('play the news').name == 'news' def test_overlap_backwards(self): self.cont.add_intent('song', ['play {song}']) self.cont.add_intent('news', ['play the news']) self.cont.train(False) assert self.cont.calc_intent('play the news').name == 'news' def test_generalize(self): self.cont.add_intent('timer', [ 'set a timer for {time} minutes', 'make a {time} minute timer' ]) self.cont.add_entity('time', [ '#', '##', '#:##', '##:##' ]) self.cont.train(False) intent = self.cont.calc_intent('make a timer for 3 minute') print(intent) assert intent.name == 'timer' assert intent.matches == {'time': '3'} def teardown(self): if isdir('temp'): rmtree('temp')