def __init__(self, APPID, APPKEY):

        self.luis_processor = LUISClient(APPID, APPKEY, verbose=True)

        self.query_history = ''
        self._top_intent_history = ''
        self._entities_history = []
def top_match_txt():
    CLIENT = LUISClient(luis_appid, luis_app_key, True)
    TEXT = request.data
    res = CLIENT.predict(TEXT)
    parsed = process_res(res)

    return json.dumps(calculate_scores(parsed['search'], parsed['coeff']))
Exemple #3
0
 def understand(self, text):
     client = LUISClient(self.appid, self.appkey, True)
     res = client.predict(text)
     top = res.get_top_intent()
     entities = res.get_entities()
     entities = [(x.get_type(), x.get_name()) for x in entities]
     intent = top.get_name()
     result = LuisResult(intent, entities)
     return result
Exemple #4
0
 def get_luis_response(self):
     try:
         client = LUISClient(self.appID, self.app_key, True)
         res = client.predict(self.text)
         while res.get_dialog(
         ) is not None and not res.get_dialog().is_finished():
             self.text = input('%s\n' % res.get_dialog().get_prompt())
             res = client.reply(self.text, res)
         return self.process_res(res)
     except Exception as exc:
         print(exc)
         return {}
Exemple #5
0
 def IdentifyIntent(self):
     try:
         TEXT = self.inputQuery
         CLIENT = LUISClient(self.__APPID__, self.__APPKEY__, True)
         res = CLIENT.predict(TEXT)
         while res.get_dialog(
         ) is not None and not res.get_dialog().is_finished():
             TEXT = raw_input(u'%s\n' % res.get_dialog().get_prompt())
             res = CLIENT.reply(TEXT, res)
         #self.process_res(res)
         return res
     except Exception, exc:
         print(exc)
Exemple #6
0
def get_luis_response(text):
    try:
        APPID = 'YOUR_LUIS_APP_ID'
        APPKEY = 'YOUR_LUIS_APP_KEY'
        CLIENT = LUISClient(APPID, APPKEY, True)
        res = CLIENT.predict(text)
        while res.get_dialog(
        ) is not None and not res.get_dialog().is_finished():
            TEXT = raw_input(u'%s\n' % res.get_dialog().get_prompt())
            res = CLIENT.reply(TEXT, res)
        reply = process_res(res)
        return reply
    except Exception, exc:
        print(exc)
        return "Sorry, something went wrong."
Exemple #7
0
 def make_request(self, message):
     """Send message from user to LUIS for intent analysis"""
     # Hard coded LUIS APP ID and Authoring Key
     APPID = self.LUIS_ID
     APPKEY = self.LUIS_KEY
     try:
         CLIENT = LUISClient(APPID, APPKEY, True)
         res = CLIENT.predict(message)
         while res.get_dialog() is not None and not res.get_dialog()\
                                                       .is_finished():
             TEXT = input('%s\n' % res.get_dialog().get_prompt())
             res = CLIENT.reply(TEXT, res)
         return res
     except Exception as exc:
         print(exc)
Exemple #8
0
def luis_result(question):
    try:
        appid = '1e995b1e-35a7-4349-ae58-e90f9beb6cca'
        appkey = '9fa67485043a4605ab8e8220eb80a940'
        text = question
        client = LUISClient(appid, appkey, True)
        res = client.predict(text)
        print threading.current_thread(
        ).name, "luis result:", res.get_top_intent().get_name(), float(
            res.get_top_intent().get_score()) * 100, res.get_entities()
        return res.get_top_intent().get_name(), float(
            res.get_top_intent().get_score()) * 100, res.get_entities()
    except Exception, exc:
        print threading.current_thread().name, "luis get result error:", exc
        return "None", 0, None
Exemple #9
0
def predict_intent(question):
    # 调用LUIS进行语义预测
    client = LUISClient(APPID, APPKEY, True)
    res = client.predict(question)
    question = question.decode('utf-8')

    intent = res.get_top_intent().get_name()
    entList = []
    ents = res.get_entities()
    for ent in ents:
        entList.append({
            'type': ent.get_type(),
            'entity': question[ent.get_start_idx():ent.get_end_idx() + 1]
        })
    return intent, entList
Exemple #10
0
    def __init__(self,
                 map_key,
                 speech_key,
                 luis_appid,
                 luis_appkey,
                 gSheet_sskey,
                 use_mic=True):

        self.map_key = map_key
        self.speech_key = speech_key

        self.luis = LUISClient(luis_appid, luis_appkey, True)
        self.gSheet = gSheet_Client(gSheet_sskey)

        self.use_mic = use_mic
        self.begin()
Exemple #11
0
def preprocess(input_text):
    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '{49a3e79bc72f414db090cd8a2eeddc03}',
    }

    APPID = "cf321ffa-6919-4306-9ff1-2ec8fc81328c"
    APPKEY = "49a3e79bc72f414db090cd8a2eeddc03"
    TEXT = input_text
    CLIENT = LUISClient(APPID, APPKEY, True)
    res = CLIENT.predict(TEXT)
    while res.get_dialog() is not None and not res.get_dialog().is_finished():
        TEXT = raw_input(u'%s\n' % res.get_dialog().get_prompt())
        res = CLIENT.reply(TEXT, res)
    return process_res(res)
Exemple #12
0
    def handleMessage(self):
        # Echo message back to client
        message = self.data

        try:
            #Create a LUIS Client by passing in the APP ID and APP Key
            #Then uses the LUIS Client methods provided in the LUIS SDK
            CLIENT = LUISClient(APPID, APPKEY, True)
            response = CLIENT.predict(message)
            #The response received from LUIS predict would be passed in to process_res

            resp = process_res(response)
            self.sendMessage(resp)

        except Exception as exc:
            print(exc)
            self.sendMessage('I\'m sorry I do not understand you.')
    def __handle_message_activity(self, activity):
        """
        Handle the messages.  STATE used to collect APPID and APPKEY up front.  All other messsages sent to
        LUIS for parsing.  APPID and APPKEY specify the LUIS service called via REST. For example:
          https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/((APPID))?subscription-key=((APPKEY))
                &verbose=true&timezoneOffset=0&q=((TEXT-FOR-LUIS-TO-PARSE
        """
        BotRequestHandler.STATE+=1   ## POORMAN'S STATE TRACKING
        self.send_response(200)
        self.end_headers()
        credentials = MicrosoftAppCredentials(APPID, APPPASSWORD)
        connector = ConnectorClient(credentials, base_url=activity.service_url)
        LUIStext = ''

        ## FIRST, GET APPID
        if self.STATE==1:
            if activity.text:
                BotRequestHandler.LUISAPPID=activity.text
            reply = BotRequestHandler.__create_reply_activity(activity, "You entered application ID: %s\nNow, please input your subscription key (default: %s):" % (activity.text,self.LUISAPPKEY))

        ## SECOND, GET APPKEY
        elif self.STATE==2:
            if activity.text:
                BotRequestHandler.LUISAPPKEY=activity.text
            reply = BotRequestHandler.__create_reply_activity(activity, "Great! You entered application key: %s\nNow, enter some text for the LUIS model to render:" % activity.text)

        ## THIRD AND ONWARDS: SEND TEXT TO LUIS AND REPORT LUIS RESPONSE TO THE USER
        else:
            try:
                CLIENT = LUISClient(self.LUISAPPID, self.LUISAPPKEY, True)
                res = CLIENT.predict(activity.text)
                while res.get_dialog() is not None and not res.get_dialog().is_finished():
                    TEXT = input('%s\n'%res.get_dialog().get_prompt())
                    res = CLIENT.reply(TEXT, res)
                LUIStext=self.__handle_LUIS_response(res)
                reply = BotRequestHandler.__create_reply_activity(activity, 'LUIS says: %s' % LUIStext)
            except Exception as exc:
                LUIStext=exc
                print("Error: %s" % exc)
                reply = BotRequestHandler.__create_reply_activity(activity, 'About %s, LUIS complains: %s' % (activity.text,LUIStext))

        connector.conversations.send_to_conversation(reply.conversation.id, reply)
Exemple #14
0
    print('Top Scoring Intent: ' + res.get_top_intent().get_name())
    if res.get_dialog() is not None:
        if res.get_dialog().get_prompt() is None:
            print('Dialog Prompt: None')
        else:
            print('Dialog Prompt: ' + res.get_dialog().get_prompt())
        if res.get_dialog().get_parameter_name() is None:
            print('Dialog Parameter: None')
        else:
            print('Dialog Parameter Name: ' +
                  res.get_dialog().get_parameter_name())
        print('Dialog Status: ' + res.get_dialog().get_status())
    print('Entities:')
    for entity in res.get_entities():
        print('"%s":' % entity.get_name())
        print('Type: %s, Score: %s' % (entity.get_type(), entity.get_score()))


try:
    APPID = 'b81195cb-904c-48ef-adaf-7b12c664dc42'
    APPKEY = '5efceb9fffa84210808ec4f48e3434d1'
    TEXT = input('Please input the text to predict:\n')
    CLIENT = LUISClient(APPID, APPKEY, True)
    res = CLIENT.predict(TEXT)
    while res.get_dialog() is not None and not res.get_dialog().is_finished():
        TEXT = input('%s\n' % res.get_dialog().get_prompt())
        res = CLIENT.reply(TEXT, res)
    process_res(res)
except Exception as exc:
    print(exc)
Exemple #15
0
def main():
    # 12.06.2017, modified to auto-read saved preprocessed info.
    #
    # 31.08.2017 Ver2: allow multiple query and evaluate by keeping info in the allGeneFileNames.txt
    #

    parser = argparse.ArgumentParser(description='query LUIS web agent.')
    parser.add_argument('--timestamp',
                        dest='timestamp',
                        action='store',
                        metavar='TIMESTAMP',
                        required=True,
                        help='The timestamp of generated data.')
    parser.add_argument('--kfold',
                        dest='kfold',
                        action='store',
                        metavar='KFOLD',
                        required=True,
                        help='order number of the kfold, e.g. 1,2,...')

    parser.add_argument(
        '--appid',
        dest='appid',
        action='store',
        metavar='APPID',
        required=True,
        help=
        'The APPID from LUIS agent which you get when you create the agent.')
    parser.add_argument(
        '--appkey',
        dest='appkey',
        action='store',
        metavar='APPKEY',
        required=True,
        help=
        'The APPKEY from LUIS agent which you get when you create the agent.')

    args = parser.parse_args()

    appID = ""
    appKey = ""
    timestamp = ""
    if args.timestamp is not None:
        timestamp = args.timestamp
    else:
        raise RuntimeError, 'Usage: python progName  --timestamp tm --kfold Num  --appid id --appkey key'

    kfold = ""
    if args.kfold is not None:
        kfold = args.kfold
    else:
        raise RuntimeError, 'Usage: python progName  --timestamp tm --kfold Num  --appid id --appkey key'

    if args.appid is not None:
        appID = args.appid
    else:
        raise RuntimeError, 'Usage: python progName --timestamp tm --kfold Num  --appid id --appkey key'

    if args.appkey is not None:
        appKey = args.appkey
    else:
        raise RuntimeError, 'Usage: python progName  --timestamp tm --kfold Num  --appid id --appkey key'

    rootDirTop = "../.."

    textTestsetDir = rootDirTop + "/preprocessResults/autoGeneFromRealAnno/autoGene_" + timestamp + "/CrossValidation/KFold_" + kfold + "/testset/text"
    srcFiles = getFiles(textTestsetDir)

    crntIdx = 1
    serviceName = "LUIS"
    trainset = "KFold"
    trainTimeStamp = timestamp

    baseDestDir = rootDirTop + "/testResults/" + serviceName
    if (not os.path.exists(baseDestDir)):
        os.mkdir(baseDestDir)

    baseDestDir = baseDestDir + "/" + serviceName + "_TestResults_" + trainset + "_" + trainTimeStamp + "-" + str(
        crntIdx)
    baseDestDir = baseDestDir + "/KFold_" + kfold
    if (not os.path.exists(baseDestDir)):
        os.mkdir(baseDestDir)

    CLIENT = LUISClient(appID, appKey, True)

    numUtt = 500  # maximum
    totalUttProcessed = 0
    srcBaseDir = textTestsetDir
    start_time = time.time()
    for i in range(len(srcFiles)):
        results = {'results': []}
        domainFile = srcFiles[i]
        print "srcFiles:" + domainFile

        fileUri = os.path.join(srcBaseDir, domainFile)
        print "fileUri:" + fileUri
        utterances = readFile2List(fileUri)
        num = numUtt
        if (num > len(utterances)):
            num = len(utterances)
            print num

        for j in range(num):
            utt = utterances[j]
            utt = utt.lower()
            print "query:" + str(j) + " = " + utt
            res = CLIENT.predict(utt)
            res_j = process_res_json(res)
            results['results'].append(res_j)
            totalUttProcessed = totalUttProcessed + 1
            print "totalUttProcessed:" + str(totalUttProcessed)

            # wait a bit, then query again for another utt.
            # LUIS limit: 5 calls Per seconds, 10K calls per month for Free Account
            #time.sleep(0.3) # Time in seconds.
            # time.sleep(0.6) # Time in seconds. For Pay-As-You-Go account, 50 per second
            time.sleep(0.2)  # 0.2 should be enough, as query itself takes time

        domainName = domainFile[0:domainFile.rfind(".")]
        print "domainName:" + domainName

        saveJson2File(baseDestDir, domainName, results)

    elapsed_time = time.time() - start_time
    print "==== LUIS query finished ==== Elapsed Time: " + str(elapsed_time)
def main():
    # 12.06.2017, modified to auto-read saved preprocessed info.
    #
    # 31.08.2017 Ver2: allow multiple query and evaluate by keeping info in the allGeneFileNames.txt
    #
    
    parser = argparse.ArgumentParser(description='query LUIS web agent.')
    parser.add_argument('--testset', dest='testset', action='store', metavar='TESTSET', required=True,
                        help='The testset dir to be used, value can be: default or specified. If specified specifyTestSetInfo.txt will be read.')

    parser.add_argument('--appid', dest='appid', action='store', metavar='APPID', required=True,
                        help='The APPID from LUIS agent which you get when you create the agent.')
    parser.add_argument('--appkey', dest='appkey', action='store', metavar='APPKEY', required=True,
                        help='The APPKEY from LUIS agent which you get when you create the agent.')
                        
    args = parser.parse_args()
    appID = ""
    appKey = ""    
    testset = ""
    if args.testset is not None:
        testset = args.testset
    else:
        raise RuntimeError,'Usage: python progName --testset default/specified  --appid id --appkey key'
    
    if args.appid is not None:
        appID = args.appid
    else:
        raise RuntimeError,'Usage: python progName --testset default/specified  --appid id --appkey key'
           
    if args.appkey is not None:
        appKey = args.appkey
    else:
        raise RuntimeError,'Usage: python progName --testset default/specified  --appid id --appkey key'

    testset = testset.lower()
    rootDirTop = "../.."
    infoFile = rootDirTop + '/allGeneFileNames/allGeneFileNames.txt'
    dictkv = getDictInfoFromFile(infoFile)
    serviceName = dictkv["serviceName_LUIS"]
    trainset = dictkv["luisActuallyUsedTrainset"]
    
    resultsIdx = dictkv["luisLatestTestResultsIdx"] # used for label testResults dir to allow multiple tests
    crntIdx = int(resultsIdx) +1;
    srcFiles = []
    trainTimeStamp = dictkv["timeStamp"]
    textTestsetDir = ""
    annoTestsetDir = ""   # for saveInfo, used by evaluator
    if testset == "default":
        annoTestsetDir = dictkv["testSetAnnotated"]  # testSetAnnotated was added when first generating data
        textTestsetDir = dictkv["testSetPureTextDir"]
        srcFiles = getFiles(textTestsetDir)
    else:
        testInfoFile = rootDirTop + '/allGeneFileNames/specifyTestSetInfo.txt'
        testsetkv = getDictInfoFromFile(testInfoFile)
        testTimeStamp = testsetkv["testTimeStamp"]
        testFileStr = testsetkv["testIntentFiles"]
        testDirPref = rootDirTop + "/preprocessResults/autoGeneFromRealAnno/autoGene_"
        testDirSuff = "/DomainIntentNoDupShuffleSubset_Test/text"
        textTestsetDir = testDirPref + testTimeStamp + testDirSuff
        annoTestsetDir = testDirPref + testTimeStamp + "/DomainIntentNoDupShuffleSubset_Test/annotated"
        if testFileStr == "full":
            srcFiles = getFiles(textTestsetDir)
        else:
            srcFiles = getFilesFromStr(testFileStr)
    
    srcBaseDir = textTestsetDir
    baseDestDir = rootDirTop + "/testResults/" + serviceName
    if(not os.path.exists(baseDestDir)):
        os.mkdir(baseDestDir)
    
    baseDestDir = baseDestDir + "/" + serviceName + "_TestResults_" + trainset + "_" + trainTimeStamp + "-" + str(crntIdx)
    if(not os.path.exists(baseDestDir)):
        os.mkdir(baseDestDir)
    
    # save saveinfo -- append to file: allGeneFileNames/allGeneFileNames.txt
    crntTimeStamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S-%f')[:-3]
    saveinfo =[]
    saveinfo.append("\n# ---- written by queryLuisAuto2.py:")
    saveinfo.append("crntTimeStamp=" + crntTimeStamp)
    saveinfo.append("luisLatestTestResultsIdx=" + str(crntIdx)) #TODO rasa would use serviceName
    saveinfo.append("luisLatestTestResultsDir=" + baseDestDir)  # evaluater read this       
    saveinfo.append("luisTestSetAnnotated=" + annoTestsetDir)      # evaluator read this 
    saveinfo.append("luisQueriedAppID="+ appID)
    saveinfo.append("luisQueriedAppKey="+ appKey)    
    saveinfo.append("luisQueriedTestset="+ testset)        
    
    write2fileAppend(infoFile, saveinfo)
    
    CLIENT = LUISClient(appID, appKey, True)
    
    srcBaseDir = textTestsetDir

    numUtt = 500 # maximum 
    totalUttProcessed = 0
    start_time=time.time()    
    for i in range(len(srcFiles)):    
        results = {'results': []}
        domainFile = srcFiles[i]
        print "srcFiles:" + domainFile

        fileUri = os.path.join(srcBaseDir, domainFile)
        print "fileUri:" + fileUri
        utterances = readFile2List(fileUri)
        num = numUtt
        if(num > len(utterances) ):
            num = len(utterances)
            print num
            
        for j in range(num):
            utt = utterances[j]
            utt = utt.lower()
            print "query:" + str(j) + " = " + utt
            res = CLIENT.predict(utt)
            res_j = process_res_json(res)
            results['results'].append(res_j)
            totalUttProcessed = totalUttProcessed +1
            print "totalUttProcessed:" + str(totalUttProcessed)
            
            # wait a bit, then query again for another utt.
            # LUIS limit: 5 calls Per seconds, 10K calls per month for Free Account
            #time.sleep(0.3) # Time in seconds.
            # time.sleep(0.6) # Time in seconds. For Pay-As-You-Go account, 50 per second
            time.sleep(0.2) # 0.2 should be enough, as query itself takes time
            
        domainName = domainFile[0:domainFile.rfind(".")]
        print "domainName:" + domainName
        
        saveJson2File(baseDestDir, domainName, results)
        
    elapsed_time=time.time()-start_time
    print "==== LUIS query finished ==== Elapsed Time: " + str(elapsed_time)
Exemple #17
0
    if res.get_dialog() is not None:
        if res.get_dialog().get_prompt() is None:
            print(u'Dialog Prompt: None')
        else:
            print(u'Dialog Prompt: ' + res.get_dialog().get_prompt())
        if res.get_dialog().get_parameter_name() is None:
            print(u'Dialog Parameter: None')
        else:
            print('Dialog Parameter Name: ' +
                  res.get_dialog().get_parameter_name())
        print(u'Dialog Status: ' + res.get_dialog().get_status())
    print(u'Entities:')
    for entity in res.get_entities():
        print(u'"%s":' % entity.get_name())
        print(u'Type: %s, Score: %s' % (entity.get_type(), entity.get_score()))


try:
    APPID = raw_input(u'Please enter your app Id:\n')
    APPKEY = raw_input(u'Please input your subscription key:\n')
    REGION = raw_input(u'Please input your region:\n')
    TEXT = raw_input(u'Please input the text to predict:\n')
    CLIENT = LUISClient(APPID, APPKEY, REGION, True, True)
    res = CLIENT.predict(TEXT)
    while res.get_dialog() is not None and not res.get_dialog().is_finished():
        TEXT = raw_input(u'%s\n' % res.get_dialog().get_prompt())
        res = CLIENT.reply(TEXT, res)
    process_res(res)
except Exception, exc:
    print(exc)