Add the Natural Language Classifier to your application

    1. Find the credentials for your Natural Language Classifier service on Bluemix.


    1. Import the Natural language classifier service from the Watson Developer Cloud SDK:

      from watson_developer_cloud import LanguageTranslationV2 as LanguageTranslation

      from watson_developer_cloud import NaturalLanguageClassifierV1 as NaturalLanguageClassifier


    1. Change the index function to initialize a classification variable:
      def index(request):

        allinfo = {}

        outputTxt = "TBD"

        targetlang = 'en'

        classification = None

        form = None 


    1. Modify the code to run a classifier on the translated text or run it on the original text if that text is in English:
      if targetlang != primarylang:

      "Language %s is not %s" % (primarylang, targetlang))

                supportedModels = checkForTranslation(primarylang, targetlang)

                if supportedModels:

        "We have some supported translation models")

                  englishTxt = performTranslation(data, primarylang, targetlang)

                  outputTxt += ", which in english is %s. " % englishTxt

                  classification = classifyTheText(englishTxt)            


                  outputTxt += " ,which unfortunately we can't translate into English"


                classification = classifyTheText(data)

              if classification:

                outputTxt += "(and %s confident that it is %s classification)" % (classification['confidence'],



    1. Add the module to perform the classification:
      def classifyTheText(txt):"About to run the classification")

        classification = {}

        nlc = getNLCService()


        classificationList = nlc.list(), indent=2))

        if "classifiers" in classificationList: 


          if "classifier_id" in classificationList["classifiers"][0]:

             classID =  classificationList["classifiers"][0]['classifier_id']

             status = nlc.status(classID)

   , indent=2))              

             if "status" in status and "Available" == status["status"]:

     "Classifier is Ready to use")                 

               classes = nlc.classify(classID, txt)

     , indent=2))

               if "classes" in classes:

                 className = classes["classes"][0]["class_name"]

                 confidence = classes["classes"][0]["confidence"]    

                 classification = {"confidence": confidence,

                                   "className" : className}


        return classification

      The following function first finds the Natural Language Classifier service, which you will code next:nlc = getNLCService()

      It then fetches a list of all the classifiers that you created:classificationList = nlc.list()

      If any are found, a status check is completed on the first classifier in the list:if "classifiers" in classificationList: 
      status = nlc.status(classID)

      If the classifier is showing a status of “ready,” the classification for the entered (or translated text) is completed:classes = nlc.classify(classID, txt)

      The following function returns a list with the class for which the service has the highest confidence first in the list:className = classes["classes"][0]["class_name"]


    1. To complete the task of detecting and translating the text, add the getNLCService function:def getNLCService():                                

        return NaturalLanguageClassifier(username='<uid>, password='<pwd>')


  1. Try using your application with text that both matches and does not match your classification.

    Does not match