Skip to content

Add '--no-logs' option to argument parser #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions rnlp/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)

LOG_HANDLER = logging.FileHandler("rnlp_log.log")
LOG_HANDLER.setLevel(logging.INFO)
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
LOG_HANDLER.setFormatter(FORMATTER)

LOGGER.addHandler(LOG_HANDLER)
LOGGER.info("Started logger.")

LOGGER.info("Started Argument Parser.")
PARSER = argparse.ArgumentParser(
description="rnlp (v{0}): Convert text into relational facts.".format(__version__),
epilog="This program is free software under the {0}. {1}".format(
Expand All @@ -62,7 +53,18 @@
"-d", "--directory", type=str, help="Read all .txt files in directory"
)
FILE_OR_DIR.add_argument("-f", "--file", type=str, help="Read from one .txt file")
PARSER.add_argument("--no-logs", action="store_true", help="Specify that no logs should be created.")

ARGS = PARSER.parse_args()
LOG_HANDLER = logging.NullHandler() if ARGS.no_logs else logging.FileHandler("rnlp_log.log")

LOG_HANDLER.setLevel(logging.INFO)
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
LOG_HANDLER.setFormatter(FORMATTER)

LOGGER.addHandler(LOG_HANDLER)
LOGGER.info("Started logger.")

LOGGER.info("Argument Parsing Successful.")

N_BLOCKS = ARGS.blockSize
Expand Down
54 changes: 54 additions & 0 deletions rnlp/check_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
_err = (
"""
>>> import nltk
>>> nltk.download('punkt')
>>> nltk.download('stopwords')
>>> nltk.download('averaged_perceptron_tagger')

Visit https://rnlp.readthedocs.io/en/latest/getting_started/02_installation.html
for more information.
"""
)


def _ensure_nltk_installed():
"""
Determine if `nltk` is installed.

:raises: Exception if `nltk` is not installed.
"""
try:
import nltk
except ModuleNotFoundError:
raise Exception(
"Unable to `import nltk` because it is not in the current environment."
" Run `pip install nltk`, and then the following in an interpreter:\n"
+ _err
) from None


def _find_nltk_module(module_name):
"""
Determine whether a certain `nltk` module is installed.

:param module_name: Name of a module within nltk
:type module name: str.

:raises: Exception if the module cannot be found
"""
import nltk
try:
nltk.data.find(module_name)
except LookupError as e:
raise Exception(
"Unable to find module '" + module_name + "'. Please run the following:\n"
+ _err
) from None


def ensure_nltk_setup():
"""Function to ensure required packages are installed."""
_ensure_nltk_installed()
nltk_modules = ("tokenizers/punkt", "corpora/stopwords", "taggers/averaged_perceptron_tagger")
for nltk_module in nltk_modules:
_find_nltk_module(nltk_module)
2 changes: 2 additions & 0 deletions rnlp/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
rnlp.parse
----------
"""
import rnlp.check_state as check_state
check_state.ensure_nltk_setup()

import os
import string
Expand Down
3 changes: 3 additions & 0 deletions rnlp/textprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
The depth of reasoning probably depends on the domain you are working on.
"""

import rnlp.check_state as check_state
check_state.ensure_nltk_setup()

import string
from nltk import sent_tokenize
from nltk.corpus import stopwords
Expand Down