示例#1
0
class NLC(object):
    def __init__(self, credential_file_path=None):
        self.__nlc = None
        self.__initialize(credential_file_path)

    def __initialize(self, credential_file_path):
        if not credential_file_path:
            credential_file_path = os.path.expanduser(DEFAULT_CREDENTIAL_PATH)
 
        with open(credential_file_path, 'r') as credential_file:
            credential = json.load(credential_file)

            self.__nlc = NaturalLanguageClassifier(url=credential['url'], username=credential['username'], password=credential['password'])

    def create(self, traning_data, name=None, language='en'):
        """
        :param traning_data: A csv file or file path representing the traning data
        :param name: The optional descriptive name for the classifier
        :param language: The language og the input data
        :return: A instance object with the classifier_id of the newly created classifier, still in traning
        """
        create_result = None

        if isinstance(traning_data, file) or isinstance(traning_data, IOBase): # traning_data is file discripter
            create_result = self.__nlc.create(traning_data, name=name, language=language)
        elif isinstance(traning_data, str): # traning_data is file path
            with open(traning_data, newline=None, mode='r', encoding='utf-8') as csv_file:
                if is_valid_recode_num(csv_file):
                    create_result = self.__nlc.create(csv_file, name=name, language=language)

        return CreateResult(create_result)

    def classifiers(self):
        classifiers_raw = self.__nlc.list()
        classifiers_ = [Classifier(c) for c in classifiers_raw['classifiers']]
        return Classifiers(classifiers_)

    def status(self, classifier_id):
        return Status(self.__nlc.status(classifier_id))

    def classify(self, classifier_id, text):
        return ClassifyResult(self.__nlc.classify(classifier_id, text))

    def remove(self, classifier_id):
        """
        param: classifier_id: Unique identifier for the classifier
        retrun: empty dict object
        raise: watson_developer_cloud.watson_developer_cloud_service.WatsonException: Not found
        """
        return self.__nlc.remove(classifier_id)

    def remove_all(self):
        classifiers_ = self.classifiers()
        return [self.remove(c.classifier_id) for c in classifiers_]
    print(str(err))
    print(usage())
    sys.exit(2)
    
for opt, arg in opts:
    if opt == '-h':
        usage()
        sys.exit()
    elif opt in ("-c", "---classifier_id"):
        classifier_id = arg
    elif opt == '-d':
        DEBUG = True

if not classifier_id:
    print('Required argument missing.')
    usage()
    sys.exit(2)
    
try:
    # create classifiers with the training data
    natural_language_classifier = NaturalLanguageClassifier(url=nlcConstants.getUrl(), username=nlcConstants.getUsername(), password=nlcConstants.getPassword())

    # Delete the classifier
    sys.stdout.write('Deleting the classifier %s ...\n' % classifier_id)
    res = natural_language_classifier.remove(classifier_id)
    sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2))

except Exception as e:
    sys.stdout.write(str(e))
    exit(1)
示例#3
0
def remove_classifiers(url, username, password, classifier_ids):
    n = NaturalLanguageClassifier(url=url, username=username, password=password)
    for classifier_id in classifier_ids:
        n.remove(classifier_id)
示例#4
0
def remove_classifiers(url, username, password, classifier_ids):
    n = NaturalLanguageClassifier(url=url, username=username, password=password)
    for classifier_id in classifier_ids:
        n.remove(classifier_id)
示例#5
0
import sys
import operator
import requests
import json
import twitter

from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier

#The IBM Bluemix credentials
nlc_username = '******'
nlc_password = '******'

natural_language_classifier = NaturalLanguageClassifier(username=nlc_username,
                                                        password=nlc_password)

classifierId = 'e4be4cx148-nlc-16'
classes = natural_language_classifier.remove(classifierId)
print(json.dumps(classes, indent=2))
for opt, arg in opts:
    if opt == '-h':
        usage()
        sys.exit()
    elif opt in ("-c", "---classifier_id"):
        classifier_id = arg
    elif opt == '-d':
        DEBUG = True

if not classifier_id:
    print('Required argument missing.')
    usage()
    sys.exit(2)

try:
    # create classifiers with the training data
    natural_language_classifier = NaturalLanguageClassifier(
        url=nlcConstants.getUrl(),
        username=nlcConstants.getUsername(),
        password=nlcConstants.getPassword())

    # Delete the classifier
    sys.stdout.write('Deleting the classifier %s ...\n' % classifier_id)
    res = natural_language_classifier.remove(classifier_id)
    sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2))

except Exception as e:
    sys.stdout.write(str(e))
    exit(1)