def CreateNewCommand(self, probCmd, sourceCmd=None): newCmd = TCommand() newCmd = copy.deepcopy(sourceCmd) newCmd.Prob = probCmd.Prob # returns new command object newCmd.CmdType = probCmd.Name if newCmd.CmdType not in TCommandType: print >>sys.stderr, "No such command" + probCmd.Name + ". DefaultCommand set." newCmd.CmdType = "DefaultCommand" # create Exec Object for operation cmdProps = TCommandType[newCmd.CmdType] defaultProps = TCommandType["DefaultCommand"] operationType = GetCommandProperty(newCmd.CmdType, "OperationType") try: # cmdProps["OperationType"] is type newCmd.CmdExecObj = operationType(newCmd.CmdType) except: raise Exception("No such class " + str(operationType)) newCmd.CmdExecObj.Name = newCmd.CmdType newCmd.CmdExecObj.ModuleRelPath = GetCommandProperty(newCmd.CmdType, "ModuleRelPath") newCmd.RequestFields = GetCommandProperty(newCmd.CmdType, "RequestFields") # print "RequestFields", newCmd.RequestFields # TODO: set Preparer class instead of following operation if len(sourceCmd.LexemsList) > 0: newCmd.CommandLexems = [ sourceCmd.LexemsList[wnum] for wnum in probCmd.CmdTokensNumList if ((wnum > -1) and (wnum < len(sourceCmd.LexemsList))) ] newCmd.RequestLexems = [ sourceCmd.LexemsList[i] for i in xrange(len(sourceCmd.LexemsList)) if i not in probCmd.CmdTokensNumList ] return newCmd
sys.path.append("../..") import dialog_server from dialog_server.command_matcher.TCommandType import * from dialog_server.command.TCommand import * from dialog_server.command_matcher.TParser import * from dialog_server.classifier.TBayesFeatureExtractor import * from dialog_server.fact_extract.TFactExtractor import * PROJECT_BASE_DIR = os.getcwd() + "/../.." # init all main objects parser = TParser(\ confPath=PROJECT_BASE_DIR + \ "/conf/dialog_server.parser.conf",\ wizardConfPath=PROJECT_BASE_DIR + \ "/conf/extern_lib.wizard_bind.conf") command = TCommand() factExtractor = TFactExtractor(PROJECT_BASE_DIR) # make preprocessed commands FCMD = open("test_phrases.txt", "r") FSERCMD = open("preproc_cmds.txt", "w") for line in FCMD: if line.find("\t") != -1: cmdType, cmdText = line[:-1].decode('utf-8').split("\t") parser(command, cmdText) factExtractor(command) time.sleep(0.5) command.CmdType = cmdType outStr = command.Write() FSERCMD.write(outStr+"\n")
import subprocess sys.path.append("../..") from dialog_server.classifier.TBayesClassifier import * from dialog_server.command.TCommand import * bayesClassifier = TBayesClassifier() def GetFeatures(command): command.Features = [ 'll: %s' % command.Preprocessed[-1], # get last letter 'pll: %s' % command.Preprocessed[-2], # get pre last letter 'fl: %s' % command.Preprocessed[0], # get first letter 'sl: %s' % command.Preprocessed[1], # get second letter ] command = TCommand() bayesClassifier.StartLearn() for line in open('names.txt'): sample = line.decode('utf-8').split() command.Preprocessed = sample[0] command.CmdType = sample[1] GetFeatures(command) bayesClassifier.Learn(command) bayesClassifier.FinishLearn() tmpStr = bayesClassifier.Model.Write() bayesClassifier.Model.Read(tmpStr) from dialog_server.classifier.TModelIO import * import weakref ModelIO = TModelIO()