import os import sys import linecache import random import socket from linereader import getline s = socket.socket() host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() filename = c.recv(1024) print("received ", filename) c.send("received "+filename) position = int(c.recv(1024)) c.send("received "+ str(position)) #linecache.clearcache() newl = getline('testfile.txt',position) print newl c.send(newl) c.close()
def splitTestingSets(name): # Check file is csv if not name.endswith(".csv"): print("%s not CSV file." % name) return main_file = name[:-4] # Removes '.csv' data_directory = main_file + "_data" # Data directory working in filepath = data_dirs3[data_directory] # Gets filepath name = data_files3[name] # Get name of file files_created = [] # Array of files converted seenImgs = [] # Rows in Training CSV linecount = NUM_LINES_TEST_CW3 # Set linecount size num_of_files = 1 linecounter = [linecount - 2] filename = ["_rows_testing_set"] # Split if training set if (main_file.startswith("fer2017-training")): # Set linecount size linecount = NUM_LINES_TRAIN_CW3 # Split into 2 files num_of_files = 2 # Difference between total line linecounter # and the amount of lines to split by linecount_diff = linecount - LINE_SPLIT_BY - 2 # array to differenciate linecounter = [LINE_SPLIT_BY, linecount_diff] filename = [ "_" + str(LINE_SPLIT_BY) + "_rows_training_set", "_" + str(linecount_diff) + "_rows_training_set" ] for i in range(2, linecount): # Need to skip first line seenImgs.append(i) random.shuffle(seenImgs) mapHappy = {"Happy": "1", "NotHappy": "0"} # Overwrite all files? askUser = False canWriteAll = False # Split CSV into two files for j in range(num_of_files): # you need to change to the name of the file you want to create stringFile = main_file + filename[j] + ".csv" my_file = filepath / stringFile if my_file.is_file(): while True: if not canWriteAll and not askUser: userInput = input( "Do you wish to overwrite all %s random files? y/n \n" % main_file) if userInput == 'y': canWriteAll = True askUser = True f = my_file.open(mode='w') break elif userInput == 'n': askUser = True break else: print("Please enter only y/n/all") else: f = my_file.open(mode='w') break else: canWriteAll = True f = my_file.open(mode="x") if (canWriteAll): print("Creating file %s..." % stringFile) f.write("emotion") for x in range(1, 2305): f.write(",pixel" + str(x)) f.write("\n") # NUM_LINES_RAND_CSV - Change in config.py for i in range(linecounter[j]): line_to_add = getline(name, seenImgs.pop()) firstValue = line_to_add.split(",") if (main_file.endswith("happy")): size = len(firstValue) - 1 # Get last element in line f.write(mapHappy[firstValue[size].rstrip("\n")] + ",") splitPixels = firstValue[:-1] for x in range(0, len(splitPixels)): f.write(splitPixels[x]) if x < len(splitPixels) - 1: f.write(",") else: f.write("\n") else: f.write(firstValue[0] + ",") splitPixels = firstValue[1].split(" ") for x in range(0, len(splitPixels)): f.write(splitPixels[x]) if x < len(splitPixels) - 1: f.write(",") files_created.append(my_file) print("Created") return files_created
fprintData = open(outputDataFilePath, "w+") fprintLabels = open(outputLabelsFilePath, "w+") # Set variables goodSongIndex = 0 numLinesInFile = 60000 # Generate data points, randomized so roughly 50% are valid FSCs for i in range(0, numLinesInFile): indicator = random.randrange(0, 2) if (indicator == 1): # Generate a good song lineReader = fgood.readlines() filename = "Datasets/SongPairs.csv" line = getline(filename, (numLinesInFile - goodSongIndex)) notes = line.split(",") # Go through all pairs in the line for pair in notes: if (pair != "\n"): cp = int(pair.split("~")[0]) cf = int(pair.split("~")[1]) fprintData.write(str(cp) + "," + str(cf) + ",") fprintData.write("\n") fprintLabels.write("1\n") goodSongIndex = goodSongIndex + 1 # If index exceeds current line in valid data file, start over (impossible, but just in case) if (goodSongIndex == numLinesInFile): goodSongIndex = 0
comprehend = boto3.client(service_name='comprehend', region_name='eu-west-1') while 1: choice = str.lower(input("What would you like to do? (Play/ Quit)")) if choice != "": result = comprehend.detect_sentiment( Text=choice, LanguageCode='en') #, sort_keys=True, indent=4) p = result["SentimentScore"]["Positive"] n = result["SentimentScore"]["Negative"] else: print("No input detected, quitting...") sleep(1) break if p >= n: dname = getline("dogs.txt", randint(1, 30)) excersise = randint(1, 5) inteligence = randint(1, 100) friendliness = randint(1, 10) drool = randint(1, 10) print("Welcome!") print("Excersise: ", excersise) print("Intelignece: ", inteligence) print("Friendliness: ", friendliness) print("Drool: ", drool) print(dname) selection = input("Please select a category... \n") if selection.lower() == "drool": if drool <= randint(1, 10): print("You won!") else:
position = random.randint(1,filesize-1) s = socket.socket() host = socket.gethostname() port = 1234 s.connect((host,port)) s.send('testfile.txt') ack1=s.recv(1024) #print(ack1) s.send(str(position)) ack2 = s.recv(1024) elif sys.argv[2]=='xs': file = 'testfile.txt' startpos = position=random.randint(1,filesize-numreads-1) for reps in range(0, numreads): linecache.clearcache() position = startpos+reps linestr = getline('testfile.txt',position) if (reps>2000): print(line) elif sys.argv[2]=='xr': file = 'testfile.txt' pos = random.randint(1,filesize-1) for reps in range(0, numreads): linecache.clearcache() linestr = getline('testfile.txt',pos) if (reps>2000): print(line)