Skip to content

Commit

Permalink
Don't fail training if persistence directory already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed May 17, 2017
1 parent 1fd5c47 commit 1e54d0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions rasa_nlu/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import copy

import errno
from builtins import object
from builtins import str
from typing import Any
Expand All @@ -25,6 +26,7 @@
from rasa_nlu.config import RasaNLUConfig
from rasa_nlu.persistor import Persistor
from rasa_nlu.training_data import TrainingData
from rasa_nlu.utils import create_dir


class InvalidModelError(Exception):
Expand Down Expand Up @@ -162,7 +164,7 @@ def train(self, data):
return Interpreter(self.pipeline, context=init_context, config=self.config.as_dict())

def persist(self, path, persistor=None, model_name=None):
# type: (Text, Optional[Persistor], bool) -> Text
# type: (Text, Optional[Persistor], Text) -> Text
"""Persist all components of the pipeline to the passed path. Returns the directory of the persited model."""

timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
Expand All @@ -176,7 +178,7 @@ def persist(self, path, persistor=None, model_name=None):
else:
dir_name = os.path.join(path, model_name)

os.makedirs(dir_name)
create_dir(dir_name)
metadata.update(self.training_data.persist(dir_name))

for component in self.pipeline:
Expand Down
18 changes: 16 additions & 2 deletions rasa_nlu/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import absolute_import
import os

import errno
from typing import List
from typing import Optional
from typing import Text
Expand All @@ -19,15 +20,28 @@ def relative_normpath(f, path):
return None


def create_dir(dir_path):
# type: (Text) -> None
"""Creates a directory and its super paths. Succeeds even if the path already exists."""

try:
os.makedirs(dir_path)
except OSError as e:
# be happy if someone already created the path
if e.errno != errno.EEXIST:
raise


def create_dir_for_file(file_path):
# type: (Text) -> None
"""Creates any missing parent directories of this files path."""

try:
os.makedirs(os.path.dirname(file_path))
except OSError:
except OSError as e:
# be happy if someone already created the path
pass
if e.errno != errno.EEXIST:
raise


def recursively_find_files(resource_name):
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
pep8maxlinelength = 120
pep8ignore =
docs/conf.py ALL
addopts = -n4

[metadata]
description-file = README.md

0 comments on commit 1e54d0c

Please sign in to comment.