Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sentiment(text):
global classifier
if not classifier:
if os.path.exists(model_path):
classifier = TextClassifier.load(model_path)
else:
logger.error(
f"Could not load model at {model_path}.\n"
f"Download model with \"underthesea download {UTSModel.sa_bank.value}\".")
sys.exit(1)
sentence = Sentence(text)
classifier.predict(sentence)
labels = sentence.labels
return [label.value for label in labels]
def sentiment(text):
global classifier
if not classifier:
if os.path.exists(model_path):
classifier = TextClassifier.load(model_path)
else:
logger.error(
f"Could not load model at {model_path}.\n"
f"Download model with \"underthesea download {UTSModel.sa_general.value}\".")
sys.exit(1)
sentence = Sentence(text)
classifier.predict(sentence)
label = sentence.labels[0]
if label == "1":
label = "negative"
if label == "0":
label = "positive"
return label
def classify(X):
global classifier
if not classifier:
if os.path.exists(model_path):
classifier = TextClassifier.load(model_path)
else:
logger.error(
f"Could not load model at {model_path}.\n"
f"Download model with \"underthesea download {UTSModel.tc_general.value}\".")
sys.exit(1)
sentence = Sentence(X)
classifier.predict(sentence)
labels = sentence.labels
return labels
def classify(X):
global classifier
if not classifier:
if os.path.exists(model_path):
classifier = TextClassifier.load(model_path)
else:
logger.error(
f"Could not load model at {model_path}.\n"
f"Download model with \"underthesea download {UTSModel.tc_bank.value}\".")
sys.exit(1)
sentence = Sentence(X)
classifier.predict(sentence)
labels = sentence.labels
if not labels:
return None
return [label.value for label in labels]
# model_file = join(model_folder, "model.bin")
# classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.FAST_TEXT)
# classifier.ft = fastText.load_model(model_file)
# return classifier
if estimator == TEXT_CLASSIFIER_ESTIMATOR.SVC:
classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.SVC)
classifier.svc = joblib.load(join(model_folder, "estimator.joblib"))
x_transformer = joblib.load(join(model_folder, "x_transformer.joblib"))
classifier.x_transformer = x_transformer
y_transformer = joblib.load(join(model_folder, "y_transformer.joblib"))
classifier.y_transformer = y_transformer
return classifier
if estimator == TEXT_CLASSIFIER_ESTIMATOR.PIPELINE:
classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.PIPELINE)
if "multilabel" in metadata:
if metadata["multilabel"]:
classifier.multilabel = True
classifier.y_encoder = joblib.load(join(model_folder, "y_encoder.joblib"))
classifier.pipeline = joblib.load(join(model_folder, "pipeline.joblib"))
return classifier
if metadata['estimator'] == 'SVC':
estimator = TEXT_CLASSIFIER_ESTIMATOR.SVC
if metadata['estimator'] == 'FAST_TEXT':
estimator = TEXT_CLASSIFIER_ESTIMATOR.FAST_TEXT
if metadata['estimator'] == 'PIPELINE':
estimator = TEXT_CLASSIFIER_ESTIMATOR.PIPELINE
# GH-304: remove fasttext
# if estimator == TEXT_CLASSIFIER_ESTIMATOR.FAST_TEXT:
# model_file = join(model_folder, "model.bin")
# classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.FAST_TEXT)
# classifier.ft = fastText.load_model(model_file)
# return classifier
if estimator == TEXT_CLASSIFIER_ESTIMATOR.SVC:
classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.SVC)
classifier.svc = joblib.load(join(model_folder, "estimator.joblib"))
x_transformer = joblib.load(join(model_folder, "x_transformer.joblib"))
classifier.x_transformer = x_transformer
y_transformer = joblib.load(join(model_folder, "y_transformer.joblib"))
classifier.y_transformer = y_transformer
return classifier
if estimator == TEXT_CLASSIFIER_ESTIMATOR.PIPELINE:
classifier = TextClassifier(estimator=TEXT_CLASSIFIER_ESTIMATOR.PIPELINE)
if "multilabel" in metadata:
if metadata["multilabel"]:
classifier.multilabel = True
classifier.y_encoder = joblib.load(join(model_folder, "y_encoder.joblib"))
classifier.pipeline = joblib.load(join(model_folder, "pipeline.joblib"))
return classifier