Skip to content

Commit

Permalink
Fix issue when cleaning up crfsuite files (#843)
Browse files Browse the repository at this point in the history
fix #806
  • Loading branch information
adrienball authored Aug 19, 2019
1 parent d9c188c commit 36249d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
### Fixed
- Invalidate importlib caches after dynamically installing module [#838](https://github.com/snipsco/snips-nlu/pull/838)
- Automatically generate documentation for supported languages and builtin entities [#841](https://github.com/snipsco/snips-nlu/pull/841)
- Fix issue when cleaning up crfsuite files [#843](https://github.com/snipsco/snips-nlu/pull/843)

## [0.20.0] - 2019-07-16
### Added
Expand Down
12 changes: 5 additions & 7 deletions snips_nlu/slot_filler/crf_slot_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,12 @@ def from_path(cls, path, **shared):
slot_filler.crf_model = crf
return slot_filler

def _cleanup(self):
if self.crf_model is not None:
self.crf_model.modelfile.cleanup()

def __del__(self):
if self.crf_model is None or self.crf_model.modelfile.name is None:
return
try:
Path(self.crf_model.modelfile.name).unlink()
except OSError as e:
logger.warning("Unable to remove CRF model file at path '%s': %s",
self.crf_model.modelfile.name, repr(e))
self._cleanup()


def _get_crf_model(crf_args):
Expand Down
20 changes: 20 additions & 0 deletions snips_nlu/tests/test_crf_slot_filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,23 @@ def test_training_should_be_reproducible(self):
slot_filler2.crf_model.state_features_)
self.assertDictEqual(slot_filler1.crf_model.transition_features_,
slot_filler2.crf_model.transition_features_)

def test_should_cleanup(self):
# Given
dataset_stream = io.StringIO("""
---
type: intent
name: MakeTea
utterances:
- make me a [beverage_temperature:Temperature](hot) cup of tea
- make me [number_of_cups:snips/number](five) tea cups""")
dataset = Dataset.from_yaml_files("en", [dataset_stream])
slot_filler = CRFSlotFiller().fit(dataset, "MakeTea")
crf_file = Path(slot_filler.crf_model.modelfile.name)
self.assertTrue(crf_file.exists())

# When
slot_filler._cleanup() # pylint:disable=protected-access

# Then
self.assertFalse(crf_file.exists())

0 comments on commit 36249d1

Please sign in to comment.