コード例 #1
0
def apiprocess():
    app.logger.info('REST API for process has been invoked')
    targetlang = 'en'
    classification = {"className":"unknown"}
    results = {}
    theData = {"error":"If you see this message then something has gone badly wrong"} 
  
    app.logger.info(request.form['txtdata'])
    if not 'txtdata' in request.form:
        theData = {"error":"Text to be processed must not be blank"} 
    else:   
        del theData["error"]
        try:
            data = request.form['txtdata']
            ltu = LanguageTranslationUtils(app) 
            nlcu = NaturalLanguageClassifierUtils(app)     
            primarylang = theData['language'] = ltu.identifyLanguage(data)["language"]
            if targetlang != primarylang:
                supportedModels = ltu.checkForTranslation(primarylang, targetlang)
                if supportedModels:
                    englishTxt = ltu.performTranslation(data, primarylang, targetlang)
                    classification = nlcu.classifyTheText(englishTxt)
            else:
                classification = nlcu.classifyTheText(data)

            theData['classification'] = classification['className']

        except WatsonException as err:
            theData['error'] = err;

    results["results"] = theData    
    return jsonify(results), 201
コード例 #2
0
def wlhome():
    app.logger.info('wlhome page requested')
    allinfo = {}
    outputTxt = "TBD"
    targetlang = 'en'
    lang = "TBD"
    txt = None
    form = LangForm()
    if form.validate_on_submit():
        lang = "TBC"
        txt = form.txtdata.data
        form.txtdata.data = ''

        try:
            ltu = LanguageTranslationUtils(app)
            nlcu = NaturalLanguageClassifierUtils(app)
            lang = ltu.identifyLanguage(txt)
            primarylang = lang["language"]
            confidence = lang["confidence"]

            outputTxt = "I am {} confident that the language is {}"
            outputTxt = outputTxt.format(confidence, primarylang)
            if targetlang != primarylang:
                supportedModels = ltu.checkForTranslation(
                    primarylang, targetlang)
                if supportedModels:
                    englishTxt = ltu.performTranslation(
                        txt, primarylang, targetlang)
                    outputTxt += ", which in english is %s" % englishTxt
                    classification = nlcu.classifyTheText(englishTxt)
                else:
                    outputTxt += \
                        ", which unfortunately we can't translate into English"
            else:
                classification = nlcu.classifyTheText(txt)
            if classification:
                message = "(and {} confident that it is {} classification)"
                message = message.format(classification['confidence'],
                                         classification['className'])
                outputTxt += message

            session['langtext'] = outputTxt

            allinfo['lang'] = lang
            allinfo['form'] = form
            return redirect(url_for('wlhome'))
        except WatsonException as err:
            allinfo['error'] = err

    allinfo['lang'] = session.get('langtext')
    allinfo['form'] = form
    return render_template('watson/wlindex.html', info=allinfo)
コード例 #3
0
def wlhome():
    app.logger.info('wlhome page requested')
    allinfo = {}
    outputTxt = "TBD"
    targetlang = 'en'    
    lang = "TBD"
    txt = None
    form = LangForm()
    if form.validate_on_submit():
        lang = "TBC"
        txt = form.txtdata.data
        form.txtdata.data = ''

        try:
            ltu = LanguageTranslationUtils(app)
            nlcu = NaturalLanguageClassifierUtils(app)            
            lang = ltu.identifyLanguage(txt)
            primarylang = lang["language"]
            confidence = lang["confidence"]

            outputTxt = "I am %s confident that the language is %s" % (confidence, primarylang)
            if targetlang != primarylang:
                supportedModels = ltu.checkForTranslation(primarylang, targetlang)
                if supportedModels:
                    englishTxt = ltu.performTranslation(txt, primarylang, targetlang)
                    outputTxt += ", which in english is %s" % englishTxt
                    classification = nlcu.classifyTheText(englishTxt)
                else:
                    outputTxt += ", which unfortunately we can't translate into English"
            else:
                classification = nlcu.classifyTheText(txt)
            if classification:
                outputTxt += "(and %s confident that it is %s classification)" \
                                              % (classification['confidence'],
                                                 classification['className'])

            session['langtext'] = outputTxt

            allinfo['lang'] = lang
            allinfo['form'] = form
            return redirect(url_for('wlhome'))
        except WatsonException as err:
          allinfo['error'] = err

    allinfo['lang'] = session.get('langtext')
    allinfo['form'] = form
    return render_template('watson/wlindex.html', info=allinfo)