def main(): """ This file is the main method call. Execution starts from here. It accepts input data either correct or incorrect data. :return: Output of the data provided. :param: It accepts file directory,which contains input_data.txt file. """ simulation = RobotSimulation() execute_directory = sys.argv[1] if len(execute_directory) == len("input_data"): correct_files = fm.load("input_data", "r") for correct_file in correct_files: print("\n") print("FIle executed from Input Directory - ", correct_file) for values in fm.read(correct_file): for line in values.split("\n"): if len(line): simulation.run(line) elif len(execute_directory) == len("incorrect_data"): incorrect_files = fm.load("incorrect_data") for incorrect_file in incorrect_files: print("\n") print("FIle executed from Incorrect Directory - ", incorrect_file) for values in fm.read(incorrect_file): for line in values.split("\n"): if len(line): simulation.run(line) else: print("Please provide the exact directory")
def test_setup(): print(fm.create('resources')) print(fm.load('resources')) for i in fm.read('adverbs.dat'): print(i) print(fm.close('nouns.dat')) print(fm.close())
def learnFromFiles(): path = fm.load('resources') for f in path: isFirst = True lettercount = 0 for i in fm.read(f): if isFirst: for x in langs: if i.replace("\n", "") == x.get("language"): currentdict = x break else: langs.append({"language": i.replace("\n", "")}) currentdict = langs[len(langs) - 1] isFirst = False else: for singleletter in i: if (ord(singleletter) >= 97 and ord(singleletter) <= 122 ) or (ord(singleletter) >= 65 and ord(singleletter) <= 90): lettercount = lettercount + 1 if singleletter.lower() in currentdict: currentdict[singleletter.lower( )] = currentdict[singleletter.lower()] + 1 else: currentdict[singleletter.lower()] = 1 if "letternumber" in currentdict: currentdict["letternumber"] = currentdict["letternumber"] + float( lettercount) else: currentdict["letternumber"] = float(lettercount) for i in langs: handleFile(i)
def get_query_index(requete): """ fonction qui prend une requête écrite en entrée et donne son indice parmi les 64 requêtes proposées. """ index = 0 queries = fm.load("Requetes") for query in queries: for line in fm.read(query): if line == requete: index = query.split()[1] return index
def convent(files): all_words = [] for file in files: for line in fm.read(file): if '-' in line: index = line.index('-') english = line[:index:] russian = line[index + 1::] a = {english: russian.replace('\n', '')} all_words.append(a) return all_words
def main(): #opens all the files in the folder all_files = fm.load('test') docs = [] #instantiating Porter Stemming stemmer = PorterStemmer() for f in all_files: docStr = "" for i in fm.read(f): docStr += i # docStr.replace('\n', ' ') # docStr.replace('\\', ' ') # docStr.replace('.', ' ') # docStr.replace(',', ' ') docStr = remove_all("\n", docStr) docStr = remove_all("\\", docStr) docStr = remove_all('"', docStr) docStr = remove_all(':', docStr) docStr = remove_all(".", docStr) docStr = remove_all(",", docStr) #unicode encoding for stemming p = docStr.decode('cp850').replace(u"\u2019", u"\x27") res = " ".join([stemmer.stem(kw) for kw in p.split(" ")]) k = unicodedata.normalize('NFKD', res).encode('ascii', 'ignore') docs.append(k) docOfInterest = docs[5] #testing on article a = ArticleScorer(docOfInterest, docs) print(a.represent()) docs_and_similarities = [] for docOfInterest in docs: # print docOfInterest a = ArticleScorer(docOfInterest, docs) # print(a.represent()) docs_and_similarities.append((docOfInterest, a.represent())) print docs_and_similarities best_matches = queue.PriorityQueue() article = docs_and_similarities[0] for other in docs_and_similarities[1:]: print "score", cosine(article, other) best_matches.put((-1 * cosine(article, other), other[0])) for i in xrange(5): print extract_title(best_matches.get()[1])
def main(argv): print "\n" sequence = [] all_files = fm.load('Edge1_') for f in all_files: Sort_file(f) all_files = fm.load('Edge1_') for f in all_files: sampling_time = 5 # in mins file_ = open("Edge1_/" + f, 'r') line = file_.readline() data = line.split(' ') prev_time = data[1] + ' ' + data[2] date_format = "%Y-%m-%d %H:%M:%S" prev_time_ = datetime.strptime(prev_time, date_format) #print prev_time_ no_cars = 0 avg_velocity = 0 for line in fm.read(f): line = line.replace('\n', '') data = line.split(' ') current_time = data[1] + ' ' + data[2] current_time_ = datetime.strptime(current_time, date_format) time_interval = current_time_ - prev_time_ print time_interval if time_interval.seconds < sampling_time * 60: no_cars = no_cars + 1 avg_velocity = avg_velocity + float(data[9]) #print no_cars else: dd = str(data[5]) + ' ' + str( data[6]) + ' ' + str(no_cars) + ' ' + str( avg_velocity / no_cars) + '\n' print dd prev_time_ = current_time_ avg_velocity = float(data[9]) no_cars = 1 with open('feature_extracted.txt', 'a') as outfile: outfile.write(dd) return 0
def test_setup(): print("begin test_setup") print(fm.create('resources')) if 'words.dat' and 'nouns.dat' in fm.create('resources'): print('Files Missing') print(fm.load('resources')) for i in fm.read('adverbs.dat'): print(i) print(fm.close('words.dat')) print(fm.close()) try: print(fm.close('words.dat')) raise ValueError("test_setup failed") # pragma: no cover except IOError: print("test_setup passed")
def create_terms_pairs(folder_name): """ cree un dictionnaire de paires de term: term_id a partir de la collection """ documents = fm.load(folder_name) terms_pairs = {} id = 1 for document in documents: for line in fm.read(document): for word in line.split(): try: terms_pairs[word] except KeyError: terms_pairs[word] = id id += 1 return terms_pairs
def create_terms_doc_pairs(folder_name): """ cree une liste de paires de (terme_id, doc_id) """ documents_pairs = create_document_pairs(folder_name) terms_pairs = create_terms_pairs(folder_name) documents = fm.load(folder_name) terms_doc_pairs_list = [] for document in documents: for line in fm.read(document): for word in line.split(): term_id = terms_pairs[word] document_id = documents_pairs[document] pair = (term_id, document_id) terms_doc_pairs_list.append(pair) return terms_doc_pairs_list
def moyenne_precisions(): """ fonction qui ne prend rien en entrée et qui fait la moyenne des précisions pour les 64 requêtes de query.text """ queries = fm.load("Requetes") written_queries = [] precisions = [] rappel = [0] * (101) precision = [0] * (101) for query in queries: for line in fm.read(query): written_queries.append(line) for i in range(64): precisions.append(data_courbe(written_queries[i])[1]) for i in range(101): rappel[i] = i / 100 for j in range(64): precision[i] += precisions[j][i] / 64 return rappel, precision
def calculateDistance(text, lang, i, max, isLearning, isWiki): path = fm.load('constlearnt') dicts = [] distances = {} for f in path: fromFileInfo = {} isFirstLine = True for y in fm.read(f): if isFirstLine: isFirstLine = False else: arr = y.split('/') fromFileInfo[arr[0]] = arr[1].replace("\n", "") dicts.append(fromFileInfo) (dicts[len(dicts) - 1])["language"] = f.replace(".txt", "").replace( "learned_", "") newtextdict = getDictFromText(text, True) for dic in dicts: for ch in alphabet: if not ch in dic: dic[ch] = 0.0 if not ch in newtextdict: newtextdict[ch] = 0.0 distances[dic["language"]] = euclideanDistance(newtextdict, dic) print("Zgaduje, iz artykul jest w jezyku " + min(distances, key=lambda k: distances[k])) global tries global correct tries = tries + 1 if min(distances, key=lambda k: distances[k]) == lang: correct = correct + 1 print("Jezyk sie zgadza") else: print("Jezyk sie nie zgadza") print(("Skutecznosc zgadywania to w tym momencie " + str(float(correct) / float(tries) * 100) + " proby: " + str(tries) + " poprawne: " + str(correct))) print("\n") if isWiki: testAlgorithm(i, max, isLearning, isWiki)
""" import filemapper as fm files=fm.load('Mom Proj') fileslist=[] for elem in files: fileslist.append(elem.split('_')) fileslistr=[] for elem in fileslist: elem[-1]=elem[-1].split('.')[0] fileslistr.append(elem) print(fileslistr) loop=-1 final=[] for f in files: e=[] loop=loop+1 for i in fm.read(f): d=','.join(fileslistr[loop]) e.append(d+','+i) final.append(e) import csv loop=-1 for elem in final: loop=loop+1 name=str(''.join(fileslistr[loop]))+'1'+'.csv' myfile = open(name, 'w') wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) for ele in elem: wr.writerow(ele.split(','))
for t in types: results[t] = {} for r in routers: results[t][r] = {} for bs in bufferSize: results[t][r][bs] = {} for n in nodes: results[t][r][bs][n] = {} # Read data for t in types: res = results[t] files = fm.load(t) for f in files: r, bs, n, _ = f.split('_') # Getting the result's parameters for line in fm.read(f): try: stat, value = line.split(':') value = float(value) res[r][bs][n][stat] = value except ValueError: pass # Router Impact for t in types: for n in nodes: delivery_probs = [] latency_avgs = [] overhead_ratio = [] hopcount_avg = [] for bs in bufferSize:
import filemapper as fm all_files = fm.load('Pics') for f in all_files: for i in fm.read(f): for f in all_files: for i in fm.write
win_unicode_console.enable() if len(sys.argv) > 1: # len小於2也就是不帶參數啦 sys.argv = [arg.decode(sys.stdout.encoding) for arg in sys.argv] text = " ".join(sys.argv[1:]) # print(exec_segment(text)) text = str(text, 'utf-8') print(text) sys.exit() for f in all_files: if f == ".gitignore": continue elif f.endswith(".txt"): content = "" for i in filemapper.read(f): content = content + i # print(content) result = exec_segment(content) if isinstance(result, list): result = ",".join(result) # 加上欄位標題 line = [] if save_pos_tag_field == "true": line.append("seg") else: line.append("seg_pos") if save_pos_tag_field == "true" and enable_pos_tag == "true":