Skip to content
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

Allow to set NLTK options via TabPy config #471

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions docs/server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ at [`logging.config` documentation page](https://docs.python.org/3.6/library/log
value - `30`. This timeout does not apply when evaluating models either
through the `/query` method, or using the `tabpy.query(...)` syntax with
the `/evaluate` method.

`[nltk]` parameters:

- `NLTK_DOWNLOAD_PATH` - Set the NLTK download path. Defaul '~/nltk_data'.
- `NLTK_PROXY` - Set the proxy server used for for NLTK `nltk.download()`.

### Configuration File Example

Expand Down Expand Up @@ -130,6 +135,14 @@ settings._
# The value should be a float representing the timeout time in seconds.
# TABPY_EVALUATE_TIMEOUT = 30

[nltk]
# Set the download directory for nltk downloads.
# NLTK_DOWNLOAD_PATH = ~/nltk_data

# If TabPy is behind a proxy and it needs to connect to the internet for
# some tasks, you can define a proxy here.
# NLTK_PROXY = http://proxy:3128

[loggers]
keys=root

Expand Down
16 changes: 14 additions & 2 deletions tabpy/models/scripts/SentimentAnalysis.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from textblob import TextBlob
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
Expand All @@ -10,8 +11,18 @@
ssl._create_default_https_context = _ctx


nltk.download("vader_lexicon")
nltk.download("punkt")
def setup():
file_path = sys.argv[1] if len(sys.argv) > 1 else setup_utils.get_default_config_file_path()
config = setup_utils.get_config(file_path)
download_dir = None
if "nltk" in config:
nltk_config = config["nltk"]
download_dir = nltk_config.get("NLTK_DOWNLOAD_PATH")
if "NLTK_PROXY" in nltk_config:
nltk.set_proxy(nltk_config["NLTK_PROXY"])

nltk.download("vader_lexicon", download_dir=download_dir)
nltk.download("punkt", download_dir=download_dir)


def SentimentAnalysis(_arg1, library="nltk"):
Expand Down Expand Up @@ -45,6 +56,7 @@ def SentimentAnalysis(_arg1, library="nltk"):


if __name__ == "__main__":
setup()
setup_utils.deploy_model(
"Sentiment Analysis",
SentimentAnalysis,
Expand Down
8 changes: 6 additions & 2 deletions tabpy/models/utils/setup_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ def get_default_config_file_path():
return config_file_path


def parse_config(config_file_path):
def get_config(config_file_path):
config = configparser.ConfigParser()
config.read(config_file_path)
tabpy_config = config["TabPy"]
return config


def parse_config(config_file_path):
tabpy_config = get_config(config_file_path)["TabPy"]

port = 9004
if "TABPY_PORT" in tabpy_config:
Expand Down