コード例 #1
0
def predict(text):
    print(f"\nText: {text}")

    sentence = Sentence(text)
    classifer.predict(sentence)
    labels = sentence.labels
    print(f"Labels: {labels}")
コード例 #2
0
 def read_text_classification_file(path_to_file) -> List[Sentence]:
     sentences = []
     with open(path_to_file) as f:
         lines = f.read().splitlines()
         for line in lines:
             label_pattern = r"__label__(?P<label>[\w#]+)"
             labels = re.findall(label_pattern, line)
             labels = [Label(label) for label in labels]
             text = re.sub(label_pattern, "", line)
             s = Sentence(text, labels)
             sentences.append(s)
     return sentences
コード例 #3
0
ファイル: text_classifier.py プロジェクト: xmobe/underthesea
    def predict(self, sentence: Sentence):
        if self.estimator == TEXT_CLASSIFIER_ESTIMATOR.FAST_TEXT:
            values, scores = self.ft.predict(sentence.text)
            labels = []
            for value, score in zip(values, scores):
                value = value.replace("__label__", "")
                label = Label(value, score)
                labels.append(label)
            sentence.add_labels(labels)

        if self.estimator == TEXT_CLASSIFIER_ESTIMATOR.SVC:
            text = sentence.text
            X = self.x_transformer.transform([text])
            y = self.svc.predict(X)
            y = self.y_transformer.inverse_transform(y)
            sentence.add_labels(y)

        if self.estimator == TEXT_CLASSIFIER_ESTIMATOR.PIPELINE:
            text = sentence.text
            y = self.pipeline.predict([text])
            if self.multilabel:
                y = self.y_encoder.inverse_transform(y)
                y = list(y[0])
            else:
                y = list(y)
            sentence.add_labels(y)
コード例 #4
0
def classify(X):
    global classifier
    model_name = 'TC_GENERAL_V131'
    model_path = ModelFetcher.get_model_path(model_name)

    if not classifier:
        if not os.path.exists(model_path):
            ModelFetcher.download(model_name)
        classifier = TextClassifier.load(model_path)

    sentence = Sentence(X)
    classifier.predict(sentence)
    labels = sentence.labels
    return labels
コード例 #5
0
ファイル: __init__.py プロジェクト: xmobe/underthesea
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
コード例 #6
0
def sentiment(X):
    global classifier
    model_name = 'SA_BANK_V131'
    model_path = ModelFetcher.get_model_path(model_name)

    if not classifier:
        if not os.path.exists(model_path):
            ModelFetcher.download(model_name)
        classifier = TextClassifier.load(model_path)

    sentence = Sentence(X)
    classifier.predict(sentence)
    labels = sentence.labels
    if not labels:
        return None
    labels = [label.value for label in labels]
    return labels
コード例 #7
0
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
    if labels is None:
        return None
    return [label.value for label in labels]
コード例 #8
0
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
コード例 #9
0
def sentiment(X):
    global classifier
    model_name = 'SA_GENERAL_V131'
    model_path = ModelFetcher.get_model_path(model_name)

    if not classifier:
        if not os.path.exists(model_path):
            ModelFetcher.download(model_name)
        classifier = TextClassifier.load(model_path)

    sentence = Sentence(X)
    classifier.predict(sentence)
    labels = sentence.labels
    try:
        label_map = {'POS': 'positive', 'NEG': 'negative'}
        label = label_map[labels[0]]
        return label
    except Exception:
        return None