Exemple #1
0
def myclip(_province,_kind,_terrain):
	clips.Reset()
	rules = [rule.strip() for rule in readfile.readfile('rules.txt')]
	provinces = [province.strip() for province in readfile.readfile('province.txt')]

	for province in provinces:
		clips.SendCommand(province.strip())
	
	
	clips.Assert("(province {0})".format(_province))
	clips.Run()
	
	for rule in rules:
		clips.SendCommand(rule.strip())

	clips.Assert("(kind {0})".format(_kind))
	if _terrain is not "":
		clips.FactList()[3].Retract()
		clips.Assert("(terrain {0})".format(_terrain))
	#BEFORE
	bf = len(clips.FactList())

	#RUN
	clips.Run()

	#AFTER
	af = len(clips.FactList())

	for i in range(af - (af - bf)):
		clips.FactList()[0].Retract()
	#clips.PrintRules()
	return [i.PPForm()[8+6:-1] for i in clips.FactList()]     
def diffdata(files):
    # Error: can only use 2 files to diff
    if len(files) != 2:
        sys.exit(3)
    (t1, w1, a1) = readfile(files.pop(0))
    (t2, w2, a2) = readfile(files.pop(0))    
    # Error: time or wavelength mismatch
    if list(t1) != list(t2) or list(w1) != list(w2):
        sys.exit(2)
    return t1, w1, a1 - a2
def avgdata(files):
    nfiles = float(len(files))
    (t, w, a) = readfile(files.pop(0))
    for file in files:
        (t1, w1, a1) = readfile(file)
        # Error: time or wavelength mismatch
        if list(t1) != list(t) or list(w1) != list(w):
            sys.exit(2)
        a += a1
    a = a / nfiles
    return t, w, a
Exemple #4
0
def kmeans():
    nr_of_clusters = 5
    blognames, words, data = readfile()
    kclust = kmeanscluster(data, k=5, max_iter=5)
    centroid_0 = []
    centroid_1 = []
    centroid_2 = []
    centroid_3 = []
    centroid_4 = []
    centroids = []
    for r in kclust[0]:
        centroid_0.append(blognames[r])
    for i in kclust[1]:
        centroid_1.append(blognames[i])
    for j in kclust[2]:
        centroid_2.append(blognames[j])
    for k in kclust[3]:
        centroid_3.append(blognames[k])
    for l in kclust[4]:
        centroid_4.append(blognames[l])
    centroids = ((centroid_0, len(centroid_0)), (centroid_1, len(centroid_1)),
                 (centroid_2, len(centroid_2)), (centroid_3, len(centroid_3)),
                 (centroid_4, len(centroid_4)))
    #print centroids
    return render_template('kmeans.html', kclust=centroids, noc=nr_of_clusters)
Exemple #5
0
def main():
    depots, stations, customers, demand, time_win, tolerate_time_win = readfile.readfile(
        "4depot_10station_40customer3.csv",
        depot_num=4,
        station_num=10,
        customer_num=40)
    dis_dep_cus = readfile.get_dis_matrix(depots,
                                          customers)  #depot到customer的距离矩阵
    dis_sta_cus = readfile.get_dis_matrix(stations,
                                          customers)  #station到customer的距离矩阵
    dis_cus_cus = readfile.get_dis_matrix(customers,
                                          customers)  #customer之间的距离矩阵

    min_dis_depot = readfile.get_min_dis(
        dis_dep_cus)  #获取距离每个customer最近的depot及距离
    min_dis_station = readfile.get_min_dis(
        dis_sta_cus)  #获取距离每个customer最近的station及距离

    mans = 40
    rows = 100
    times = 300

    # line = [3, 29, 14, 39, 19, 7, 35, 5, 21, 6, 33, 4, 26, 31, 11, 18, 20, 1, 30, 36, 15, 24, 23, 32, 34, 40, 37, 9, 22, 16, 8, 38, 12, 13, 10, 28, 2, 17, 25, 27]
    # result = calFitness.fitness(dis_cus_cus, dis_sta_cus, min_dis_depot, min_dis_station, time_win, tolerate_time_win, demand, line, True)
    # print result
    #
    genetic = GeneticAlgorithm(dis_dep_cus, dis_sta_cus, dis_cus_cus,
                               min_dis_depot, min_dis_station, demand,
                               time_win, tolerate_time_win, mans, rows, times)
    genetic.run()
def polygon_intersect(file):

# Read the input file in the correct format. Use the plot_boundary function
# to plot the polygons
    result = readfile.readfile(file)
    P1 = result['poly1']
    P2 = result['poly2']
    plt.cla()
    plt.axis('equal')
    plt.grid('on')
    plt.hold(True)
    plot_boundary(P2)
    plot_boundary(P1)

# Find the intersections of the polygons and compose the new polygon
    result = find_intersections(P1,P2)
    P3 = compose_new_polygon(result)
    P3 = [[int(float(j)) for j in i] for i in P3]
 
# Use the plot_boundary and flood_fill functions to plot the new polygon   
    poly = P3
    px = [poly[i][0] for i in range(len(poly))]
    py = [poly[i][1] for i in range(len(poly))]
    x_sorted = np.sort(px) # replace!!!
    y_sorted = np.sort(py) # replace!!!
    colors = plot_boundary(poly)
    plt.plot(int(np.median(px)),int(np.median(py)), marker='.', markersize = 15, markerfacecolor = 'g')
    flood_fill(int(np.median(px)),int(np.median(py)), 'w', 'g', x_sorted[0], y_sorted[0], colors)
    plt.show(block=False)
Exemple #7
0
 def __init__(self,):
     tablist=readfile()
     self.values=[]
     for i in range(3):
         value=tuple(tablist[0])
         del tablist[0]
         self.values.append(value)
     self.pfdict=dict(tablist)
Exemple #8
0
def main():
    fabdict=dict(f.readfile("fabtest.csv"))
    localpath=fabdict['localpath']
    remotepath=fabdict['remotepath']
    #putfile(localpath,remotepath)
    #check_file(localpath,remotepath)
    
    untar(remotepath)
Exemple #9
0
def top(path, f_type, hang, lie):

    f_list = readfile.readfile(path, f_type)

    for i in f_list:
        extract.extract(i, hang, lie)

    return
Exemple #10
0
 def __init__(self, ):
     tablist = readfile()
     self.values = []
     for i in range(3):
         value = tuple(tablist[0])
         del tablist[0]
         self.values.append(value)
     self.pfdict = dict(tablist)
Exemple #11
0
def run(path=resource_manager.Properties.getDefaultDataFold()+"txt"+resource_manager.getSeparator()+"build.txt",sep=' '):
    '''
    return cluster id
    i,j distance
    '''
    (dist,xxdist,ND,N) = readfile(path, dimensions = 2, sep=sep)
    XY, eigs = mds(dist)
    (rho,delta,ordrho,dc,nneigh) = rhodelta(dist, xxdist, ND, N, percent = 2.0)
    DCplot(dist, XY, ND, rho, delta,ordrho,dc,nneigh,17,0.1)
Exemple #12
0
def db(request):
	import sys,readfile
	from rice.models import Rice
	rules = [rule.strip() for rule in readfile.readfile("kem.csv")]
	for i in rules:
		i = i.split(',')
		r = Rice(name=i[0],kind=i[1],number=i[2],area=i[3],terrain=i[4],character=i[5],seed=i[6],detail=i[7],pic=i[8],ref=i[9])
		r.save()
	return HttpResponse("GG")
Exemple #13
0
def run(*args, **kwargs):
    '''
    return cluster id
    '''
    file = kwargs.get('fi')
    sep = kwargs.get('sep',' ')
    ########
    (dist,xxdist,ND,N) = readfile(file, dimensions = 2, sep=sep)
    XY, eigs = mds(dist)
    (rho,delta,ordrho,dc,nneigh) = rhodelta(dist, xxdist, ND, N, percent = 2.0)
    DCplot(dist, XY, ND, rho, delta,ordrho,dc,nneigh,17,0.1,31)
Exemple #14
0
    def ___init__(filename):
        file=readfile.readfile()
        file.getvaluefromconfig()
        firstline=file.firstline
        lastline=file.lastline
        linenum=file.linenum

        f.getvaluefromfile()
        ffirstline=file.firstline()
        flastline=file.lastline()
        flinenum=file.linenum()
Exemple #15
0
    def done(self):
        # Dictionary containing {filename:configuration} pairs
        results = {}

        i=0
        for file in self.filenames:
            results.update({self.trim_filename(self.filenames[i]):readfile(file)})
            i = i + 1
        
        openwindow(root, results)
        openwindow2(root, results)
Exemple #16
0
def draw_table_price():
    pl.ioff()
    contents = readfile.readfile("000007.csv")
    high, low = readfile.get_prices(contents)
    ranges = range(len(contents))
    prices = [ content[2] for content in contents ]

    pl.plot(ranges, prices, color="red")
    #pl.plot([(float(low) - float(low)*0.02), (float(high) + float(high)*0.02)])
    pl.plot([float(low), float(high)])
    pl.title("Hello world")

    pl.show()
Exemple #17
0
def main():
	fname = sys.argv[1]
	X, Y = readfile.readfile(fname)
	X, Y = np.array(X), np.array(Y)
	times = []
	np.random.seed(1208)
	for i in range(1126):
		w, updates = PLA(X, Y)
		times.append(updates)
		print(w, updates)
	print('average update steps : {}'.format(np.average(times)))
	plt.hist(np.array(times), bins=range(min(times), max(times),1))
	plt.xlabel('Update counts')
	plt.ylabel('Counts')
	plt.savefig('hw1_7.png')
Exemple #18
0
def convex_hull_plot(file):

    # Format input file and plot data
    result = readfile.readfile(file)
    points = result['points']
    plt.cla()
    plt.axis('equal')
    plt.grid('on')
    plt.hold(True)

    # Find convex hull and plot it using plot_boundary
    hull = convex_hull(points)
    plt.scatter(*zip(*points))
    plot_boundary(hull)
    plt.show(block=False)
Exemple #19
0
def main():
    depots, stations, customers, demand, time_win, tolerate_time_win = readfile.readfile(
        "4depot_10station_40customer3.csv",
        depot_num=4,
        station_num=10,
        customer_num=40)
    dis_dep_cus = readfile.get_dis_matrix(depots,
                                          customers)  #depot到customer的距离矩阵
    dis_sta_cus = readfile.get_dis_matrix(stations,
                                          customers)  #station到customer的距离矩阵
    dis_cus_cus = readfile.get_dis_matrix(customers,
                                          customers)  #customer之间的距离矩阵

    min_dis_depot = readfile.get_min_dis(
        dis_dep_cus)  #获取距离每个customer最近的depot及距离
    min_dis_station = readfile.get_min_dis(
        dis_sta_cus)  #获取距离每个customer最近的station及距离

    customer_means = []
    cus1 = []
    cus2 = []
    cus3 = []
    cus4 = []
    for i in range(0, len(min_dis_depot)):
        if min_dis_depot[i][0] == 0:
            cus1.append(i + 1)
        if min_dis_depot[i][0] == 1:
            cus2.append(i + 1)
        if min_dis_depot[i][0] == 2:
            cus3.append(i + 1)
        if min_dis_depot[i][0] == 3:
            cus4.append(i + 1)
    customer_means.append(cus1)
    customer_means.append(cus2)
    customer_means.append(cus3)
    customer_means.append(cus4)

    for i in range(len(customer_means)):
        customers = customer_means[i]
        mans = len(customers)
        dep_num = i + 1 + 40
        rows = 100
        times = 300
        genetic = GeneticAlgorithm(dep_num, customers, dis_dep_cus[i],
                                   dis_sta_cus, dis_cus_cus, min_dis_station,
                                   demand, time_win, tolerate_time_win, mans,
                                   rows, times)
        genetic.run()
Exemple #20
0
def draw_table_vol():
    pl.ioff()
    contents = readfile.readfile("000007.csv")
    high, low = readfile.get_prices(contents)
    ranges = range(len(contents))
    prices = [content[2] for content in contents]

    table_x = list()
    table_y = list()
    high, low = readfile.get_vol(contents)

    for x, content in enumerate(contents):
        table_x.append(x)
        table_y.append(float(content[1]))

    pl.plot(table_x, table_y, "-")
    pl.show()
Exemple #21
0
def tests(net,
          tims=-1,
          aimat=float(0.0),
          readf=readfile.readfile(),
          logs=None):
    aimat = float(aimat)
    net.eval()
    correct = 0
    total = 0
    tot = 0
    for data in readf.testsetsmallloader:

        images, labels = data
        outputs = net(Variable(images, volatile=True))
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum()
        if total > 100:
            break

    print('现实 : %d %%' % (100 * correct / total))
    logs.refline((100 * correct / total), "现实正确率")
    if float(100 * correct / total) > float(aimat):
        aimat = float(aimat)
        aimat = float(100 * correct / total)
        print("正确率" + str(float(aimat)) + "刷新数据")
        torch.save(net, "./mod2/handalexnetmax34" + str(tims))
    # net.state_dict(),
    correct = 0
    total = 0

    for data in readf.testselfloader:
        tot += 1
        images, labels = data
        outputs = net(Variable(images, volatile=True))
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum()
        if tot > 100:
            break
    print('自身 : %d %%' % (100 * correct / total))
    logs.refline((100 * correct / total), "自身正确率")
    return float(aimat)
Exemple #22
0
 def __init__(self,):
     tablist=readfile()
     self.values=[]
     for i in range(3):
         value=tuple(tablist[0])
         del tablist[0]
         self.values.append(value)
     self.pfdict=dict(tablist)
     try:
         self.conn=pymysql.connect(host=self.pfdict["host"],
                              port=int(self.pfdict['port']),
                              user=self.pfdict['user'],
                              passwd=self.pfdict['passwd'],
                              db=self.pfdict['db'],
                              charset=self.pfdict['charset'])
         
         self.cur=self.conn.cursor()      
     except:
         print("连接数据库失败!")
     else:
         print("连接数据库成功!")   
def polygon_color(file):

    # Read in input file and arrange points in desired format
    result = readfile.readfile(file)
    poly = result['poly1']
    px = [poly[i][0] for i in range(len(poly))]
    py = [poly[i][1] for i in range(len(poly))]

    # Sort the x and y coordinates
    x_sorted = np.sort(px)
    y_sorted = np.sort(py)

    # Call functions and plot
    plt.cla()
    plt.axis('equal')
    plt.grid('on')
    plt.hold(True)
    colors = plot_boundary(poly)
    flood_fill(int(np.median(px)), int(np.median(py)), 'w', 'g', x_sorted[0],
               y_sorted[0], colors)
    plt.show(block=False)
Exemple #24
0
    def __init__(self, ):
        tablist = readfile()
        self.values = []
        for i in range(3):
            value = tuple(tablist[0])
            del tablist[0]
            self.values.append(value)
        self.pfdict = dict(tablist)
        try:
            self.conn = pymysql.connect(host=self.pfdict["host"],
                                        port=int(self.pfdict['port']),
                                        user=self.pfdict['user'],
                                        passwd=self.pfdict['passwd'],
                                        db=self.pfdict['db'],
                                        charset=self.pfdict['charset'])

            self.cur = self.conn.cursor()
        except:
            print("连接数据库失败!")
        else:
            print("连接数据库成功!")
Exemple #25
0
def test_readcsv(capsys):
    dirpath = Path("C:/code/cohort4/python-IO")
    filename = "Census_by_Community_2019.csv"
    hdr = "************************************************************\n"
    hdr += "**************  Calgary Public Data Summary  ***************\n"
    hdr += "************************************************************\n"
    content1 = "Residential and SOUTH: 230129"
    content2 = "Industrial and NORTH: 0"
    content3 = "Overall Total: 1283177"
    content4 = "Total number of records: 307"
    ftr = "************************************************************\n"
    ftr += "***************  End of Report Summary  ********************\n"
    ftr += "************************************************************\n"

    result = readcsv(dirpath, filename)
    captured = capsys.readouterr()
    reportdata = readfile(dirpath, "report.txt")

    assert result == {
        "keyvaluepair": {
            "RES_CNT": 10,
            "CLASS": 1,
            "SECTOR": 5
        },
        "linenum": 308
    }
    assert hdr in captured.out
    assert ftr in captured.out
    assert content1 in captured.out
    assert content2 in captured.out
    assert content3 in captured.out
    assert content4 in captured.out
    assert os.path.isfile(os.path.join(dirpath, "report.txt")) == True
    assert content1 in reportdata
    assert content2 in reportdata
    assert content3 in reportdata
    assert content4 in reportdata
            'pricing': 0.05,
            'blog': 0.5,
            'payment': 1.0
        },
    }

    id_to_states = {
        0: 'Zero',
        1: 'Aware',
        2: 'Considering',
        3: 'Experiencing',
        4: 'Ready',
        5: 'Lost',
        6: 'Satisfied'
    }
    obs_seq, obs_seq_id = readfile("hmm_customer_1586733275338.txt")
    new_emmi_prop = new_emmi_prop(emit_p, obs_seq)

    tran_prob = convert_to_list(trans_p)
    emmi_prob = convert_to_list(new_emmi_prop)
    init_prob = np.array(list(start_p.values()))
    hidden_s, max_prob = viterbi(tran_prob, emmi_prob, init_prob, obs_seq_id)

    opt_seq = []
    for data in hidden_s:
        opt_seq.append(id_to_states[data])

    print("Observation sequence : " + str(obs_seq))
    print()
    print("Transition Probability: " + str(trans_p))
    print()
Exemple #27
0
def chat():
  # keyword conditions
  condnext = False
  condweather = False
  condtime = False
  condlocation = False
  condtemp = False
  condkey = False
  condresponse = False
  foundinfo = False
  condtrain = False
  condcountry = False
  condspellcheck = True

  # global variables
  conversation = []
  location = ''
  prevlocation = location 
  time = 'today'
  key = ''
  keytemplate = []
  fulltime = ''
  numdays = ''
  logstr = ''
  printstr = ''
  responsedict = {} 	# Dictionary to hold all inputs without predefined responses. This dictionary will be written into predefined_responses.txt before exiting the program.


  # read data files
  citylist = readfile.readfile('cities.txt')
  keylist = readfile.readfile('keywords.txt')
  timelist = readfile.readfile('time.txt')
  condlist = readfile.readfile('conditions.txt')
  numlist = readfile.readfile('numbers.txt')
  countrylist = readfile.readfile('countries.txt')
  exitlist = ['exit', 'quit', 'bye', 'ok']

  # Greeting message
  printstr =  'Hello! You can ask me questions about the weather in any major city in the world. What would you like to know?'
  print printstr
  logstr += '\n\n' + printstr

  # Start main loop
  while True :
    foundinfo = False
    condtrain = False
    condcountry = False
    # read input from user
    input = raw_input('\nMe > ')
    logstr += '\nMe > ' + input + '\nBot > '
    if input in exitlist:
      if input == 'ok':
	exitans = raw_input("Do you want to quit? (y/n)")
	if exitans in ('y','Y','Yes','YES','yes'):
	  break
	else:
	  continue
      break
    
    if input == 'disable spellcheck':
      condspellcheck = False
      continue
    
    if input == 'enable spellcheck':
      condspellcheck = True
      continue
    
    condcorrected = False
    if condspellcheck:
      corrected_input = ''
      for i in input.split():
	str = spellcheck.correct(i)
	if str != i:
	  condcorrected = True
	corrected_input += str + ' '
      if condcorrected:
	print 'did you mean: \"' + corrected_input + '\"?'
	input = corrected_input
    
    currentstring = input.split()
    conversation.append(currentstring)
    
    # Start searching input for each of the keywords
    
    if input == 'train':
      condtrain = True
      printstr =  'Entering training mode. Enter input and response seperated by a "|": input|response. Type "exit" to quit training mode'
      print printstr
      logstr += '\n' + printstr + '\n'
      
      while True:
	traininput = raw_input('>')
	if traininput == 'exit':
	  break
	if traininput.find('|') < 0:
	  printstr =  'Format error: use input|response'
	  print printstr
	  logstr += '\n' + printstr + '\n'
	  continue
	traininput = traininput.split('|')
	responsedict[traininput[0]] = traininput[1]
    
    if condtrain:
      continue
    


    for i in countrylist:
      for j in currentstring:
	if lower(i[0]) == lower(j):
	  printstr = 'Which city in ' + i[0] + '?'
	  condcountry = True
	  foundinfo = True
	  break
      
    if condcountry:
      print printstr
      logstr += printstr
      continue
    

    if 'next' in input:
      foundinfo = True
      condnext = True
      condtime = False
      numdays = currentstring[currentstring.index('next') + 1]
      for i in numlist:
	if numdays == i[0]:
	  numdays = i[1]
	  break
      if re.match('[0-9]*$',numdays):
	numdays = int(numdays)
      else:
	numdays = ''
    
    if 'weather' in input:
      foundinfo = True
      condweather = True
      condkey = False
      condtemp = False
      key = ''
      keytemplate = []

    # get key from input
    for i in keylist:
      if i[0] in input:
	if 'sunday' in lower(input) and i[0] == 'sun':
	  break
	else:
	  foundinfo = True
	  condkey = True
	  condweather = False
	  condtemp = False
	  key = i[0]
	  keytemplate = i
	  break

    # get time from input
      for i in timelist:
	if lower(i[0]) in input:
	  foundinfo = True
	  condtime = True
	  numdays = ''
	  if lower(i[0]) != 'today' and lower(i[0]) != 'tomorrow':
	    time = i[1]
	    fulltime = i[0]
	    break
	  else:
	    time = i[0]
	    fulltime = time
	    break
    if fulltime == '':
      fulltime = time

    if numdays != '':
      condtime = True
      if numdays > 4:
	printstr =  'Forecast is available only for the next 4 days.'
	print printstr
	logstr += '\n' + printstr + '\n'
      else:
	time = ''
	fulltime = ''
	count = numdays
    
    # get location from input
    for i in citylist:
      if lower(i[0]) in input:
	foundinfo = True
	condlocation = True
	location = i[0]
	break
    
    # find if a new location has been mentioned. if not, don't fetch data again
    if location != prevlocation:
      newlocation = True
      condlocation = True
      prevlocation = location
    else:
      newlocation = False
    
    if location is '':
      if prevlocation is '':
	condlocation = False
      else:
	location = prevlocation
	newlocation = False
    
    location = location.replace(' ','-') #Google requires a '-' in 2-word city names
    result = False
    
    # get temperature from input
    if 'temperature' in input:
      foundinfo = True
      condtemp = True

    # User gave no infomation about weather. Switching to general predefined response based chat
    if not foundinfo:
      response = predefined_responses.respond(input, responsedict)
      if response == '':
	printstr =  "I don't know what that means. If I asked you the same question, what would you reply?"
	print printstr
	logstr += printstr
	responseinput = raw_input('Me > ')
	logstr += '\nMe > ' + responseinput
	if not responseinput in ('exit', 'quit'):
	  responsedict[input] = responseinput
	  print 'response learnt'
      else:
	printstr =  response
	print printstr
	logstr += printstr
      continue
    
    if condlocation:
      if newlocation:	#If location hasn't changed, don't fetch data again. It's already available
	printstr =  'Fetching weather information from Google...'
	print printstr
	logstr += printstr
	# Call Google weather to get current weather conditions
	google_result = weather.get_weather(location)
	if google_result == {}:
	  print 'Could not get data from google.'
	  continue
      
      
  # We have a valid location. Get further information

  # User has asked about temperature. Return temperature information and continue
      if condtemp:
	printstr =  temperature.temperature(google_result, time)
	print printstr
	logstr += printstr
	continue
      
  # User has asked about a specific weather condition. Print information. There are 2 possibilities:
  #    1. Find the condition in the next n days
  #    2. Find the condition in a specified day

      if condkey:

  # 1. User has asked about a specific condition in the 'next x days'. Return appropriate response
	printstr = ''
	timecounter = 0

	day_of_week = ''
	condition = ''
	if numdays != '':
	  for i in google_result['forecasts']:
	    count -= 1
	    if count < 0:
	      break
	    if key in lower(i['condition']):
	      result = True
	      day_of_week = i['day_of_week']
	      condition = i['condition']
	      break

	  for i in timelist:
	    if i[0] != 'today' and i[0] != 'tomorrow':
	      if i[1] == day_of_week:
		fulltime = i[0]
		break
	  if result:
	    printstr = keytemplate[3] + keytemplate[0] + ' on ' + fulltime
	  else:
	    printstr = keytemplate[4] + keytemplate[0] + ' in the next ' + str(numdays) + ' days.'

	  print printstr
	  logstr += printstr
	  continue

  # 2. User has asked about a particular condition on a particular day. Return appropriate response
	if time != 'today' and time != 'tomorrow':
	  for i in google_result['forecasts']:
	    if i['day_of_week'] == time:
	      if key in lower(i['condition']):
		printstr = keytemplate[3] + keytemplate[0] + ' on'
	      else:
		printstr = keytemplate[4] + keytemplate[0] + ' on'
	elif time == 'today':
	  fulltime = time
	  if key in lower(google_result['current_conditions']['condition']):
	    printstr = keytemplate[1] + keytemplate[0]
	  else:
	    printstr = keytemplate[2] + keytemplate[0]
	elif time == 'tomorrow':
	  fulltime = time
	  if key in lower(google_result['forecasts'][1]['condition']):
	    printstr = keytemplate[3] + keytemplate[0]
	  else:
	    printstr = keytemplate[4] + keytemplate[0]

	printstr =  printstr + ' ' + fulltime
	print printstr
	logstr += printstr
	continue

  # User is asking about today's weather. Print details
      elif time == '' or time == 'today' :
	printstr = sentence.sentence(google_result['current_conditions']['condition'], time)
	printstr += ' ' + fulltime + '. ' + google_result['current_conditions']['humidity'] + ' '
	if google_result['current_conditions'].has_key('wind_condition'):
	  printstr += google_result['current_conditions']['wind_condition']
	print printstr
	logstr += printstr
	continue

  # User is asking about weather of a particular day. Print details
      elif time == 'tomorrow':
	printstr = sentence.sentence(google_result['forecasts'][1]['condition'], time)
	printstr += ' ' + fulltime
	print printstr
	logstr += printstr
      else:
	found = False
	for i in range(4):
	  if google_result['forecasts'][i]['day_of_week'] == time:
	    printstr = sentence.sentence(google_result['forecasts'][i]['condition'], time)
	    printstr +=   " on" + ' ' +  fulltime
	    print printstr
	    logstr += printstr
	    found = True
	if not found:
	  printstr =  "Forecast for " + time + " is not available currently."
	  print printstr
	  logstr += printstr
	continue
      
    else:
      printstr =  'What\'s the location?'
      print printstr
      logstr += printstr
  # End of outermost while loop.

  # Print message before exiting program
  dictcount = 0
  for i in responsedict:
    dictcount += 1
  if dictcount > 0:
    printstr =  'Writing new entries to database...'
    print printstr
    logstr += printstr
  datafile = file('predefined_responses.txt', 'a')
  for i in responsedict.keys():
    trimmedi = re.sub('[^a-zA-Z0-9 ]+','', i)
    string = trimmedi + '|' + responsedict[i] + '\n'
    datafile.write(string)
  log.log(logstr)
  print 'Ending the program...'
  print 'Bye!'
  
# End of function chat()
Exemple #28
0
#!/usr/bin/env python
#python

import logsend
import readfile
import configure
import judge

configure = configure.Configure()
first = configure.readoption
judge = judge.Judge(filename)

filename = 'aa.txt'
readfile.readfile(filename)
Exemple #29
0
if __name__ == "__main__":
    fileNames = [
        "a_example.txt", "b_read_on.txt", "c_incunabula.txt",
        "d_tough_choices.txt", "e_so_many_books.txt",
        "f_libraries_of_the_world.txt"
    ]
    for fileName in fileNames:

        Data.nBooks = 0
        Data.nLibraries = 0
        Data.nScanningDays = 0
        Data.bookScores = []
        Data.libraries = []

        Running.currentProcessing = None  # The current library being processed
        Running.daysLeft = 0  # How many days are left for the current library to be processed
        Running.librariesLeft = []  # The libraries still to be processed
        Running.processed = []  # Librarues that have been processed
        Running.numberProcessed = 0
        Running.totalScore = 0  # The total score

        readfile("data/{}".format(fileName))
        sortLibraries()
        for lib in Data.libraries:
            lib.sortBooks()
        print("Read/sort files")

        simulate(Data.libraries, Data.nScanningDays,
                 "out/out_{}.txt".format(fileName[0]))
Exemple #30
0
# from mtranslate import translate
from writefile import writefile
from readfile import readfile
from fileLocation import fileLocation
from languages import LANGUAGES
import urllib
from googleapiclient.discovery import build

# translator = Translator()
# print(LANGUAGES)
intents = readfile('en/intent.txt')
# print(intents)
service = build('translate', 'v2',
            developerKey='AIzaSyCN5X2fEWsdO4jRSgwB_PZ3v_0A4HbmXCY')

for key, value in LANGUAGES.items():
    print (key, value)
    list = []
    translated = service.translations().list(
      source='en',
      target=key,
      q=intents
    ).execute()
    for tranlation in translated['translations']:
        list.append(tranlation['translatedText'])

    # break
    writefile(key, list)
    # print(key + ' : ' , list)
    # break
Exemple #31
0
import sys
from readfile import readfile

fileA = sys.argv[1]
fileB = sys.argv[2]

mA = readfile(fileA)
mb = readfile(fileB)


def guass(a, b):
    n = len(a)
    for k in range(0, n - 1):
        for i in range(k + 1, n):
            if a[i][k] != 0.0:
                lam = a[i][k] / a[k][k]
                print(lam)
                #a[i,k+1:n] = a[i, k+1:n] - lam*a[k,k+1:n]
                #b[i] = b[i] - lam*b[k]


guass(mA, mb)
Exemple #32
0
#coding=utf-8
'''
Created on 2016年8月16日

@author: admin
'''
from fabric.api import *
from readfile import readfile

condict = dict(readfile())

env.host_string = condict["connext"]
env.password = condict["passwd"]

run("rm -rf /taokey")

run("yum -y remove mysql-libs*")
run("yum -y remove mysql-libs")
run("yum install -y perl-Module-Install.noarch")
run("yum install -y libaio")
run("mkdir -p /taokey/tools/")

with cd("/taokey/tools/"):
    run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-server-5.6.21-1.rhel5.x86_64.rpm"
        )
    run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-devel-5.6.21-1.rhel5.x86_64.rpm"
        )
    run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-client-5.6.21-1.rhel5.x86_64.rpm"
        )
    rpmlist = run("ls ./").split()
    for rpm in rpmlist:
Exemple #33
0
 def __init__(self):
     self.tab=dict(readfile())
def bot():
  conversation = []
  location = ''
  time = 'today'
  key = ''
  keytemplate = []
  fulltime = ''
  numdays = ''

  citylist = readfile.readfile('cities.txt')
  keylist = readfile.readfile('keywords.txt')
  timelist = readfile.readfile('time.txt')
  condlist = readfile.readfile('conditions.txt')
  numlist = readfile.readfile('numbers.txt')
  exitlist = ['exit', 'quit', 'bye', 'ok']

  print 'Hello! You can ask me questions about the weather in any major city in the world. What would you like to know?'
  while True :
    input = raw_input('Me > ')
    if input in exitlist:
      break
    
    currentstring = input.split()
    conversation.append(currentstring)
    
    if 'next' in currentstring:
      numdays = currentstring[currentstring.index('next') + 1]
      for i in numlist:
	if numdays == i[0]:
	  numdays = i[1]
	  break
      if re.match('[0-9]*$',numdays):
	numdays = int(numdays)
      else:
	numdays = ''
    
    if 'weather' in currentstring:
      key = ''
      keytemplate = []
    # get key from input
    for i in keylist:
      if i[0] in input:
	key = i[0]
	keytemplate = i
	break
    
    # get time from input

    for i in timelist:
      if lower(i[0]) in input:
	numdays = ''
	if lower(i[0]) != 'today' and lower(i[0]) != 'tomorrow':
	  time = i[1]
	  fulltime = i[0]
	  break
	else:
	  time = i[0]
	  fulltime = time
	  break
    if fulltime == '':
      fulltime = time

    if numdays != '':
      if numdays > 4:
	print 'Forecast is available only for the next 4 days.'
      else:
	time = ''
	fulltime = ''
	count = numdays
    prevlocation = location 
    #We store previous location to avoid re-fetching data if the location hasn't been changed
    
    
    # Below, we check if any token in the input matches a city name, and if so, set location to that city
    newlocation = False
    
    # get location from input
    foundLocation = False
    for i in citylist:
      if lower(i[0]) in input:
	location = i[0]
	foundLocation = True
	break
    
    #if not foundLocation:
      #if location != '':
	#print "I didn't find any city name in your input. I'll get you information about " + location
    # find if a new location has been mentioned. if not, don't fetch data again
    if location is not prevlocation:
      newlocation = True
    
    if location is '':
      if prevlocation is '':
	print 'City not found'
      else:
	location = prevlocation
	newlocation = False
    
    location = location.replace(' ','-') #Google requires a '-' in 2-word city names
    result = False
    
    
    if location is not '':
      if newlocation:	#If location hasn't changed, don't fetch data again. It's already available
	print 'Fetching weather information from Google...'
	# Call Google weather to get current weather conditions
	google_result = weather.get_weather(location)
      
      if 'temperature' in currentstring:
	print temperature.temperature(google_result, time)
	continue
      
      printed = False
      
      
      if key is not '':
	printstring = ''
	timecounter = 0
	
	day_of_week = ''
	condition = ''
	if numdays != '':
	  for i in google_result['forecasts']:
	    count -= 1
	    if count < 0:
	      break
	    if key in lower(i['condition']):
	      result = True
	      day_of_week = i['day_of_week']
	      condition = i['condition']
	      break
	  
	  for i in timelist:
	    if i[0] != 'today' and i[0] != 'tomorrow':
	      if i[1] == day_of_week:
		fulltime = i[0]
		break
	  if result:
	    printstring = keytemplate[3] + keytemplate[0] + ' on ' + fulltime
	  else:
	    printstring = keytemplate[4] + keytemplate[0] + ' in the next ' + str(numdays) + ' days.'
	  
	  print printstring
	  printed = True
	      
	if not printed:
	  if time != 'today' and time != 'tomorrow':
	    for i in google_result['forecasts']:
	      if i['day_of_week'] == time:
		if key in lower(i['condition']):
		  printstring = keytemplate[3] + keytemplate[0] + ' on'
		else:
		  printstring = keytemplate[4] + keytemplate[0] + ' on'
	  elif time == 'today':
	    fulltime = time
	    if key in lower(google_result['current_conditions']['condition']):
	      printstring = keytemplate[1] + keytemplate[0]
	    else:
	      printstring = keytemplate[2] + keytemplate[0]
	  elif time == 'tomorrow':
	    fulltime = time
	    if key in lower(google_result['forecasts'][1]['condition']):
	      printstring = keytemplate[3] + keytemplate[0]
	    else:
	      printstring = keytemplate[4] + keytemplate[0]
		
	  print printstring, fulltime

      elif time == '' or time == 'today' :
	  printstring = sentence.sentence(google_result['current_conditions']['condition'], time)
	  print printstring, fulltime,  google_result['current_conditions']['humidity'], google_result['current_conditions']['wind_condition']
      else :
	if time == 'tomorrow':
	  printstring = sentence.sentence(google_result['forecasts'][1]['condition'], time)
	  print printstring, fulltime
	else:
	  found = False
	  for i in range(4):
	    if google_result['forecasts'][i]['day_of_week'] == time:
	      printstring = sentence.sentence(google_result['forecasts'][i]['condition'], time)
	      print printstring, "on", fulltime
	      found = True
	  if not found:
	    print "Forecast for " + time + " is not available currently."

    
    else:
      print 'What\'s the location?'
  #end of outermost while loop
  print 'ending the program...'
  print 'bye!'
Exemple #35
0
#!/usr/bin/python3

from readfile import readfile
from emailsetting import sendmail
""" Buscar por nombre y estado"""
search = 'Cassandra'
enable = 'Activo'

""" Lectura de datos """
query = 'SELECT * FROM test'

""" Obteniendo informacion de lectura """
file = readfile(1, query)
""" Realizando busqueda """
file = file[file['name'].str.contains(search)]

""" Enviar corrreo """
for reg in file.itertuples():
    name = reg[1]
    receiver_mail = reg[2]
    mail2 = reg[3]
    subject = reg[4]
    body = reg[5]
    attachment = reg[8]
    filemail = reg[9]
    sendmail('*****@*****.**', receiver_mail, name, subject, body, attachment, filemail)
Exemple #36
0
from readfile import readfile
import linecache
import string

OCCURENCE_LIST_PATH = "./Occurence_List.dat"  ## occurence list save address

if __name__ == '__main__':
    keyTrie = readfile()
    print("FILE LOAD FINISH.")
    print("##################################")
    print("INPUT -h FOR HELP")
    print("INPUT -q TO QUIT THE SEARCH ENGINE")
    print(
        "This search engine support multiple search. If you want to search multiple keyword, you should use space to separate the keywords."
    )
    print("Ex: 'nltk data'")
    print("This search engine also support prefix search.")
    print(
        "Ex: We have 'data', 'database' and 'dat' in trie. We input 'da', algorithm will return the most frequently in data, database and dat."
    )
    print("##################################\n")
    lastSearchKey = ""
    lastSearchResult = []
    while (True):
        inputString = input("\nWhat key word you want to search now:  ")
        inputString = inputString.strip()
        ###### INSTRUCTION ######
        if inputString == "-h" or inputString == "-H" or inputString == "-help":
            print("------- HELP LIST: -------")
            print("-h -H -help ======> HELP")
            print("-q -Q -quit ======> TO QUIT THE SEARCH ENGINE")
Exemple #37
0
import clips,readfile,sys
rules = [rule.strip() for rule in readfile.readfile(sys.argv[1])]
provinces = [province.strip() for province in readfile.readfile(sys.argv[2])]

for province in provinces:
	clips.SendCommand(province)

clips.Assert("(province Bangkok)")

clips.Run()

for rule in rules:
	clips.SendCommand(rule)

clips.Assert("(kind round shaped rice)")
print "condition : "
clips.PrintFacts()

#BEFORE
bf = len(clips.FactList())

#RUN
clips.Run()

#AFTER
af = len(clips.FactList())

for i in range(af - (af - bf)):
	#print "retract Fact",i
	clips.FactList()[0].Retract()
Exemple #38
0

#-------------入口函数,开始执行-----------------------------
"""
输入参数的的意义依次为

        self.rows = rows                            #排列个数
        self.times = times                          #迭代次数
        self.mans = mans                            #客户数量
        self.cars = cars                            #车辆总数
        self.tons = tons                            #车辆载重
        self.distance = distance                    #车辆一次行驶的最大距离
        self.PW = PW                                #当生成一个不可行路线时的惩罚因子

"""
d, q, et, lt, eet, llt = readfile("sss.csv")
ga = GeneticAlgorithm(dist=d,
                      dema=q,
                      e_time=et,
                      l_time=lt,
                      ee_time=eet,
                      ll_time=llt,
                      rows=5,
                      times=5,
                      depots=3,
                      mans=34,
                      cars=999,
                      tons=100,
                      distance=150,
                      server=10,
                      PW=10000)
import numpy as np
from gridworld import grid_mat, print_policy ,print_values, all_actions_ingrid, all_rewards_ingrid
from value_iteration_p3 import value_iteration
from policy_iteration_p3 import policy_iteration
from readfile import readfile
import time


if __name__ == '__main__':

    # Enter the file name for which you want to excute value and policy iteration.
    gamma, noise, gridworld = readfile("Input/i3.txt")
    gridworld = np.asarray(gridworld)
    all_actions = all_actions_ingrid(gridworld)
    all_rewards = all_rewards_ingrid(gridworld)
    grid = grid_mat(all_actions, all_rewards)


    #Value Iteration
    start = time.time()
    V,policy = value_iteration(grid,gamma,noise)
    end = time.time()

    print("Values:")
    print_values(V, gridworld.shape)
    print("Policy:")
    print_policy(policy, gridworld.shape)
    print("Runtime of Value iteration", end-start)

    #Policy Iteration
    start = time.time()
Exemple #40
0
#coding=utf-8
'''
Created on 2016年8月16日

@author: admin
'''
from fabric.api import *
from readfile import readfile
import sys

condict=dict(readfile())

env.host_string=condict["connext"] 
env.password=condict["passwd"]
file="linux_mysql_install.log"
path_file=condict["log_path"]+file
f=open(path_file,"w+",1)
sys.stdout=f
def mysql_install():
    run("rm -rf /taokey")
    
    run("yum -y remove mysql-libs*")
    run("yum -y remove mysql-libs")
    run("yum install -y perl-Module-Install.noarch")
    run("yum install -y libaio")
    run("mkdir -p /taokey/tools/")
    
    with cd("/taokey/tools/"):
        run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-server-5.6.21-1.rhel5.x86_64.rpm")
        run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-devel-5.6.21-1.rhel5.x86_64.rpm")
        run("wget http://dev.mysql.com/Downloads/MySQL-5.6/MySQL-client-5.6.21-1.rhel5.x86_64.rpm")
Exemple #41
0
    def trains(self, net):
        self.logs = logger.logger()
        # logs.refline(1,"2334",1)
        # logs.refline(4,"2334",5)
        # logs.refline(1,"2334",6)
        # logs.refline()
        readf = readfile.readfile()
        aimat = float(0.0)
        # test.tests(net, 0, float(aimat), readf, self.logs)
        net.train(mode=True)
        torch.set_num_threads(8)
        criterion = nn.CrossEntropyLoss(
        )  # use a Classification Cross-Entropy loss

        optimizer = optim.SGD([{
            'params': net.features.parameters(),
            'lr': 0.00001
        }, {
            'params': net.lin.parameters(),
            'lr': 0.001
        }, {
            'params': net.classifier.parameters(),
            'lr': 0.0005
        }],
                              momentum=0.9)
        # scheduler = MultiStepLR(optimizer, milestones=[10, 80], gamma=0.1)
        tot = 0
        for epoch in range(2000):  # loop over the dataset multiple times
            # scheduler.step()
            running_loss = 0.0
            net.train(mode=True)
            for i, data in enumerate(readf.dataloader, 0):
                tot += 1
                # get the inputs
                inputs, labels = data

                # wrap them in Variable
                inputs, labels = Variable(inputs), Variable(labels)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward + backward + optimize
                outputs = net(inputs)
                loss = criterion(outputs, labels)
                loss.backward()
                optimizer.step()

                # print statistics
                running_loss += loss.data[0]

                if i % 25 == 24:  # print every 2000 mini-batches
                    print('[%d, %5d] loss: %.3f' %
                          (epoch + 1, i + 1, running_loss / 25))

                    self.logs.defx = tot
                    # print(self.logs.defx)
                    self.logs.refline(running_loss / 25 * 100, "loss")
                    running_loss = 0.0
                    aimat = float(aimat)
                    aimat = test.tests(net, epoch, float(aimat), readf,
                                       self.logs)
                    net.train(mode=True)

            torch.save(net, "./mod2/handalexnet34")
            print("saved")

        print('Finished Training')
Exemple #42
0
import numpy as np
import preprocess_data as p
import readfile as r
import cal_probability as cd
import naivebayes as nb

data_train = r.readfile("Text_data_for_Project1_train_data.txt")
data_test = r.readfile("Text_Data_for_Project1_test_data.txt")

alabel_train, clabel_train, adata_train, cdata_train = p.p_traindata(
    data_train)
alabel_test, adata_test = p.p_testdata(data_test)

atraintestmix = np.vstack((adata_train, adata_test))

att_num = np.shape(adata_train)[1]
dat_num = np.shape(adata_train)[0]

adata_traintest_dummy, a_label = p.indexing(atraintestmix,
                                            att_num,
                                            dat_num,
                                            key="test")

adata_train_dummy = adata_traintest_dummy[:-1, :]
adata_test_dummy = np.array([adata_traintest_dummy[-1, :]])

cdata_train_dummy, c_label = p.indexing(cdata_train, 1, dat_num, key="test")

m, p = 0, 0
prior_probability = cd.cal_prior(adata_train_dummy, cdata_train_dummy, a_label,
                                 c_label, dat_num, m, p)
Exemple #43
0
from Block import Block
from Cube import Cube
from Drone import Drone
from readfile import readfile
import math
import sys

filename = sys.argv[1]
cube = readfile(filename)
size = cube.size
sizec = size**3
mode = 1
stop = 0
storHop = int(math.sqrt(sizec) / 2)
drone = Drone(storHop, cube, 0, 0, size)
print(cube)
drone.hop()
x_limit = size - 1
y_limit = size - 1


def mover(mode, x_limit, y_limit, stop):
    if (mode == 1):
        drone.MoveRight()
        if (drone.x == x_limit):
            mode = 3
    elif (mode == 2):
        drone.MoveLeft()
        if (drone.x == size - (x_limit + 1)):
            mode = -1
    elif (mode == 3):
Exemple #44
0
		for v in range(voca_size):
			fout.write(str(beta[i][v]) + ' ');
		fout.write('\n');



def train(max_iter):
	global alpha,beta,Gamma,Phi,doc,doc_cnt;
	for i in range(max_iter):
		now = mle();
		print(now);
		print('Estep');
		for d in range(doc_size):
			Estep(d, 20); # the e step of em algorithm
			if (d % 100 == 0):
				print('*');
		now = mle();
		print(now);
		print('Mstep');
		Mstep(20); # the m step of em algorith
		print(alpha);
		savemodel(i);


if __name__ == '__main__':
	fin = readfile('ap.dat');
	doc, voca, doc_cnt = fin.read();
	voca_size = len(voca);
	doc_size = len(doc);
	init();
	train(10);
Exemple #45
0
from KEM import settings
from django.core.management import setup_environ
setup_environ(settings)
from rice.models import Rice
import sys,readfile
rules = [rule.strip() for rule in readfile.readfile("kem.csv")]
for i in rules:
	i = i.split(',')
	r = Rice(name=i[0],kind=i[1],number=i[2],area=i[3],terrain=i[4],character=i[5],seed=i[6],detail=i[7],pic=i[8],ref=i[9])
	r.save()
print Rice.objects.all()
Exemple #46
0
ls_nsub = []
ls_subset = []
ls_rel = []
ls_times = []
lbound = []
nelements = []
nsubsets = []
means = ['Mean']

# The first two values printed on the list correspond to the total cost and
# the numbers of subsets chosen respectively
for name in files:

    # Read file
    path = os.path.join(datasets_path, name)
    df, costs = readfile(path)
    nelements.append(df.shape[0])
    nsubsets.append(df.shape[1])

    # Lower bound
    lb = lowerbound(df, costs)
    lbound.append(lb)

    # VND
    print('GA')
    start_ga = time.perf_counter()
    ga_cost, ga_subsets = GA(df, costs, npop, maxtime, nchilds, pmut)
    time_ga = time.perf_counter() - start_ga
    print('C:\t', [ga_cost, len(ga_subsets)] + ga_subsets, '\t', time_ga)
    ga_scores.append(ga_cost)
    ga_rel.append(np.float32(np.round(ga_cost / lb, 3)))
Exemple #47
0
import sys,readfile
class rule(object):
	def __init__(self,name,kind,terrain,area,count):
		self.name = name
		self.terrain = terrain
		self.area = area
		self.kind = kind
		self.count = count
	def getrule(self):
		return "(defrule {0}.{4} \"{0}\" (kind {1}) (terrain {2}) (area {3}) => (assert (rice {0})))\n".format(self.name,self.kind,self.terrain,self.area,self.count)

data = [i.strip() for i in readfile.readfile(sys.argv[2])]
lst = []
for i in data:
	x = i.split(",")
	lst.append([x[0],x[1],x[3],x[4]])
print lst
for i in lst:
	with open(sys.argv[1],"a") as data:
		name = i[0]
		kind = i[1]
		area = i[2]
		terrain	= i[3]
		count = 0
		for k in area.split("/"):
			for j in terrain.split("/"):
				tmp_rule = rule(name,kind,j.lower(),k.title(),count)
				count+=1
				print tmp_rule.getrule()
				data.write(tmp_rule.getrule())