Exemple #1
0
def train(cll, collection):
   sockets.send("What's the label?")
   label    = sockets.recv(BUFFER_SIZE)
   sockets.send("What's the command?")
   command  = sockets.recv(BUFFER_SIZE)
   new_data = [(command, label)]
   cll.update(new_data)
   post = {"label":label, "command":command}
   collection.insert_one(post)
   f = open(classifier_file, 'wb')
   pickle.dump(cll, f)
   sockets.send("Got it!")
Exemple #2
0
def parseCommand(command, cll, classifier_file):
   confidence_threshold = config.confidence_threshold
   prob_dist            = cll.prob_classify(command)
   labels               = cll.labels()
   prob_label_dict      = dict()
   risk = algorithms.getRisk(command)
   mpl  = labels[0]

   # for correcting spelling mistakes, don't think i'll use this
   #command = str(TextBlob(command).correct())
   #print "Corrected command " + command
   
   # before using the classifier, check if it is a built in command
   if isBuiltIn(command):
      return command, algorithms.getRisk(command)

   for label in labels:
      if prob_dist.prob(label) > prob_dist.prob(mpl):
         mpl = label
      prob_label_dict[label] = prob_dist.prob(label)
   print "Most probable label: " + str(mpl) + ", prob: " + str(prob_dist.prob(mpl))

   # this is if the threshold wasn't passed. Add more to knowledge
   if prob_dist.prob(mpl) < confidence_threshold:
      sockets.send("Is this a " + mpl + " command?")
      a = sockets.recv(BUFFER_SIZE)
      if a == "yes" or a == "yeah":
         new_data = [(command, mpl)]
         addKnowledge(new_data, cll)
         return mpl, risk
      else:
         sockets.send("Want to add this to something I already know?")
         ans = sockets.recv(BUFFER_SIZE)
         if ans == "yes" or ans == "yeah":
            update_classifier(command, cll)
            return -1, 0
         else:
            sockets.send("Okay then!")
            return -1, 0

   # add what was just said to the classifier if it passed the threshold
   if prob_dist.prob(mpl) > confidence_threshold:
      new_data = [(command, mpl)]
      addKnowledge(new_data, cll)
   return mpl, risk
Exemple #3
0
def update_classifier(command, cll):
   sockets.send("Ok what type of command is this?")
   new_label = sockets.recv(BUFFER_SIZE)
   if new_label != "no command":
      sockets.send("Okay, let's try that again then!")
      new_data = [(command, new_label)]
      cll.update(new_data)
      f = open(classifier_file, 'wb')
      pickle.dump(cll, f)
Exemple #4
0
def test_command(cll):
   sockets.send("What command would you like to test?")
   command = sockets.recv(BUFFER_SIZE)
   labels = cll.labels()
   mpl = labels[0]
   prob_dist = cll.prob_classify(command)
   for label in labels:
      if prob_dist.prob(label) > prob_dist.prob(mpl):
         mpl = label
   per = prob_dist.prob(mpl)
   per = "{0:.4f}".format(per)
   per = int(float(per)*100.0)
   sockets.send("I am %s percent certain this is a %s command" %(str(per), str(mpl)))
Exemple #5
0
def dealClient(conn, addr):
	szBuf = sockets.recv(conn)
	logging.info(szBuf)
	# 执行命令
	if szBuf.find(exe_c) == 0:
		szBuf = szBuf.replace(exe_c, '')
		# 执行命令
		out = commands.getstatusoutput(szBuf)
		result = ""
		# 获取结果,根据返回值判断是否出错
		if out[0] == 0:
			result = "[resp]" + out[1]
		else:
			result = "[error]" + out[1]
	# 修改ip以及其他地址
	elif szBuf.find(ip_c) == 0:
		szBuf = szBuf.replace(ip_c, "")
		# config to auto get ip
		if '|' not in szBuf:
			if szBuf.find(ip_c_null) == 0:
				with open(ip_c_file, "w") as f:
					f.write(ip_c_str_null)
				result = "[resp]ip conf null ok"
			else:
				result = "[error]argvs error"
		# 设置静态ip
		else:
			argvs = szBuf.split("|")
			# 三个参数,否则出错
			if len(argvs) == 3:
				text = ip_c_str.format(argvs[0], argvs[1], argvs[2])
				with open(ip_c_file, "w") as f:
					f.write(text)
				out = commands.getstatusoutput("chmod a+x " + ip_c_file)
				if out[0] == 0:
					result = "[resp]ip conf ok"
				else:
					result = '[error]ip cong error file i/o error'
			else:
				result = "[error]argvs error"
	else:
		conn.close()
		return

	logging.info(result)
	conn.send(result)
	conn.close()
Exemple #6
0
#!/usr/bin/python
# coding:utf-8
import sockets
import logging

logging.basicConfig(level=logging.INFO)
s = sockets.connectServer()
exe = "mkdir /var/lib/test/ddd"
logging.info(exe)
exe = "[exec]" + exe
s.send(exe)
result = sockets.recv(s)
logging.info(result)
s.close()
Exemple #7
0
import sockets
import parser
import time
#from pymongo import MongoClient 
import built_in
import algorithms

cll = parser.cll
classifier_file = parser.classifier_file

#client = MongoClient('localhost', 27000)
#db = client.smartTalk_db
#collection = db.responses

while True:
   command = sockets.recv(1024)
   if not command: 
      break
   return_label, risk = parser.parseCommand(command, cll, classifier_file)
   print "return_label: " + str(return_label)
   print "risk: " + str(risk)
   if return_label == "greeting":
      #response = algorithms.greet(return_label, command, collection) 
      response = "Hello"
      sockets.send(str(response))
      continue
   elif return_label == "test command":
      built_in.test_command(cll)
      continue
   elif return_label == "train":
      built_in.train(cll, collection)