Add the Natural Language Classifier to your application
-
- Find the credentials for your Natural Language Classifier service on Bluemix.
- Find the credentials for your Natural Language Classifier service on Bluemix.
-
- 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
- Import the Natural language classifier service from the Watson Developer Cloud SDK:
-
- Change the index function to initialize a classification variable:
def index(request):
allinfo = {}
outputTxt = "TBD"
targetlang = 'en'
classification = None
form = None
- Change the index function to initialize a classification variable:
-
- 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:
logger.info("Language %s is not %s" % (primarylang, targetlang))
supportedModels = checkForTranslation(primarylang, targetlang)
if supportedModels:
logger.info("We have some supported translation models")
englishTxt = performTranslation(data, primarylang, targetlang)
outputTxt += ", which in english is %s. " % englishTxt
classification = classifyTheText(englishTxt)
else:
outputTxt += " ,which unfortunately we can't translate into English"
else:
classification = classifyTheText(data)
if classification:
outputTxt += "(and %s confident that it is %s classification)" % (classification['confidence'],
classification['className'])
- Modify the code to run a classifier on the translated text or run it on the original text if that text is in English:
-
- Add the module to perform the classification:
def classifyTheText(txt):
logger.info("About to run the classification")
classification = {}
nlc = getNLCService()
classificationList = nlc.list()
logger.info(json.dumps(classificationList, indent=2))
if "classifiers" in classificationList:
logger.info(classificationList["classifiers"][0]['classifier_id'])
if "classifier_id" in classificationList["classifiers"][0]:
classID = classificationList["classifiers"][0]['classifier_id']
status = nlc.status(classID)
logger.info(json.dumps(status, indent=2))
if "status" in status and "Available" == status["status"]:
logger.info("Classifier is Ready to use")
classes = nlc.classify(classID, txt)
logger.info(json.dumps(classes, indent=2))
if "classes" in classes:
className = classes["classes"][0]["class_name"]
confidence = classes["classes"][0]["confidence"]
classification = {"confidence": confidence,
"className" : className}
logger.info(classification)
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"]
- Add the module to perform the classification:
-
- To complete the task of detecting and translating the text, add the getNLCService function:
def getNLCService():
return NaturalLanguageClassifier(username='<uid>, password='<pwd>')
- To complete the task of detecting and translating the text, add the getNLCService function:
- Try using your application with text that both matches and does not match your classification.
MatchesDoes not match