def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing if not os.path.exists(os.path.dirname(output_file)): try: os.makedirs(os.path.dirname(output_file)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise with open(output_file, "w") as outfile: for key in results.keys(): #print(key) #print(results[key]) for inst_tuples in results[key]: #print(inst_tuples) lexelt = main.replace_accented(key) instance_id = main.replace_accented(inst_tuples[0]) label = inst_tuples[1] outfile.write(lexelt + ' ' + instance_id + ' ' + label + '\n')
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results alphabetically by lexelt_item, then on # instance_id before printing remove_accent = {} for lexelt in results.keys(): listOfTuple = [] for tuple_k in results[lexelt]: listOfTuple.append((replace_accented(unicode(tuple_k[0])),replace_accented(unicode(tuple_k[1])))) sorted_tuple = sorted(listOfTuple,key = lambda x : x[0]) remove_accent[replace_accented(lexelt)] = sorted_tuple sorted_rm = sorted(remove_accent.items(), key=lambda d:d[0]) f = file(output_file,'w') for tuple_z in sorted_rm: for l in tuple_z[1]: line = tuple_z[0] + " " + l[0] + " " + l[1] + '\n' f.write(line) f.close()
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing reload(sys) sys.setdefaultencoding('utf-8') output = open(output_file, 'w') ret = [] for item in results: instance_list = results[item] for instance in instance_list: item = replace_accented(item) instance_id = replace_accented(instance[0]) label = replace_accented(unicode(instance[1])) line = (item, instance_id, label) ret.append(line) sorted(ret, key=lambda x: x[1]) for line in ret: output_str = line[0] + " " + line[1] + " " + line[2] + "\n" output.write(output_str) output.close()
def print_results(results, output_file): """ :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output """ f = open(output_file, "w") for item in results: results[item].sort(key=lambda x: x[1].lower()) for element in results[item]: # f.write(str(item) + ' ' + str(element[0]) + ' ' + str(element[1]) + '\n') f.write( str(replace_accented(item)) + " " + str(replace_accented(element[0])) + " " + str(element[1]) + "\n" ) f.close() f = open(output_file, "r") lines = f.readlines() lines.sort() f.close() f = open(output_file, "w") for item in lines: f.write(item) f.close()
def most_frequent_sense(data, sense_dict, language): outfile = codecs.open(language + '.baseline', encoding='utf-8', mode='w') for lexelt, instances in sorted(data.iteritems(), key=lambda d: main.replace_accented(d[0].split('.')[0])): for instance in sorted(instances, key=lambda d: int(d[0].split('.')[-1])): instance_id = instance[0] sid = getFrequentSense(lexelt, sense_dict) outfile.write(main.replace_accented(lexelt + ' ' + instance_id + ' ' + sid + '\n')) outfile.close()
def most_frequent_sense(data, sense_dict, language): outfile = codecs.open(language + '.baseline', encoding='utf-8', mode='w') for lexelt, instances in sorted( data.iteritems(), key=lambda d: main.replace_accented(d[0].split('.')[0])): for instance in sorted(instances, key=lambda d: int(d[0].split('.')[-1])): instance_id = instance[0] sid = getFrequentSense(lexelt, sense_dict) outfile.write( main.replace_accented(lexelt + ' ' + instance_id + ' ' + sid + '\n')) outfile.close()
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing with open(output_file, 'w') as f: for lexelt in sorted(results): for (instance_id, label) in sorted(results[lexelt]): _str = str(replace_accented(lexelt)) + ' ' + str(replace_accented(instance_id)) + ' ' + str(replace_accented(unicode(label[0]))) + '\n' print _str f.write(_str)
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing fo = codecs.open(output_file, encoding='utf-8', mode='w') for lexelt, instances in sorted(results.iteritems(), key=lambda d: main.replace_accented(d[0].split('.')[0])): for instance_id, sid in sorted(instances, key=lambda d: int(d[0].split('.')[-1])): fo.write(main.replace_accented(lexelt + ' ' + instance_id + ' ' + sid + '\n')) fo.close()
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing outfile = codecs.open(output_file, encoding='utf-8', mode='w') for lexelt, instances in sorted(results.iteritems(), key=lambda d: replace_accented(d[0].split('.')[0])): for instance in sorted(instances, key=lambda d: int(d[0].split('.')[-1])): instance_id = instance[0] sid = instance[1] outfile.write(replace_accented(lexelt + ' ' + instance_id + ' ' + sid + '\n')) outfile.close()
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing with open(output_file, 'w') as output: for lexelt, value in results.iteritems(): for instance in sorted(value, key=lambda x:x[0]): x = replace_accented(lexelt) y = replace_accented(instance[0]) z = instance[1] output.write(x + ' ' + y + ' ' + z + '\n')
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing f = open(output_file, 'w') for key, value in sorted(results.items()): for instance in sorted(value): f.write(replace_accented(key) + ' ' + replace_accented(instance[0]) + ' ') f.write(instance[1]) f.write('\n') f.close()
def print_results(results, output_file): """ :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output """ # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing from main import replace_accented outfile = codecs.open(output_file, encoding="utf-8", mode="w") for lexelt, instances in sorted(results.iteritems(), key=lambda d: replace_accented(d[0].split(".")[0])): for instance in sorted(instances, key=lambda d: int(d[0].split(".")[-1])): outfile.write(replace_accented(lexelt + " " + instance[0] + " " + instance[1] + "\n")) outfile.close()
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' output = [] for key, values in results.items(): for i in values: lexelt_item = replace_accented(key) instance_id = replace_accented(i[0]) label = i[1] output.append((lexelt_item, instance_id, label)) output_sorted = sorted(output, key=itemgetter(0, 1)) out = open(output_file, 'w') for item in output_sorted: out.write(item[0] + " " + item[1] + " " + item[2] + "\n") out.close()
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' output=[] for key,values in results.items(): for i in values: lexelt_item = replace_accented(key) instance_id = replace_accented(i[0]) label = i[1] output.append((lexelt_item, instance_id, label)) output_sorted= sorted(output, key = itemgetter(0,1)) out=open(output_file, 'w') for item in output_sorted: out.write(item[0]+" "+item[1]+" "+item[2]+"\n") out.close()
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing lines = [] with open(output_file, 'w') as fp: for lexelt, predictions in results.iteritems(): predictions.sort(key=lambda x: x[0]) for instance_id, sense_id in predictions: lines.append((replace_accented(lexelt), replace_accented(instance_id), replace_accented(unicode(sense_id)))) lines.sort(key=lambda x: x[0]) for line in lines: fp.write('{} {} {}\n'.format(*line))
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing f = open(output_file, 'w') for key, value in sorted(results.items()): for instance in sorted(value): f.write( replace_accented(key) + ' ' + replace_accented(instance[0]) + ' ') f.write(instance[1]) f.write('\n') f.close()
def print_results(results ,output_file): ''' Output the predicted result. :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing outfile = open(output_file, 'wb') for lexelt in sorted(results.keys()): tuple_list = results[lexelt] for tup in sorted(tuple_list): # print tup outfile.write('{0} {1} {2} \n'.format( replace_accented(lexelt), replace_accented(tup[0]), replace_accented(unicode(tup[1])), ) ) outfile.close()
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing lines = [] with open(output_file, 'w') as fp: for lexelt, predictions in results.iteritems(): predictions.sort(key=lambda x: x[0]) for instance_id, sense_id in predictions: lines.append( (replace_accented(lexelt), replace_accented(instance_id), replace_accented(unicode(sense_id)))) lines.sort(key=lambda x: x[0]) for line in lines: fp.write('{} {} {}\n'.format(*line))
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing content = "" for l,results in results.items(): ll=replace_accented(l).encode('ascii') sresults = sorted(results) for result in sresults: iid = replace_accented(result[0]).encode('ascii') #label = replace_accented(result[1]).encode('ascii') label = result[1] line = "%s %s %s\n"%(ll,iid,label) content+=line with open(output_file,'w+') as f: f.write(content)
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results alphabetically by lexelt_item, then on # instance_id before printing out = open(output_file, 'w') for lexelt, tuples in sorted(results.items()): x = replace_accented(lexelt) for instance in sorted(tuples): y = replace_accented(instance[0]) z = instance[1] out.write(x +' ' + y +' '+ z + '\n') out.close()
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing with open(output_file, 'w') as f: try: map( lambda item: map( lambda t: f.write('{0} {1} {2}\n'.format( replace_accented(item[0]), replace_accented(t[0]), replace_accented(unicode(t[1])))), item[1]), map(lambda result: (result[0], sorted(result[1], cmp)), sorted(results.items()))) except Exception as e: print e pass
def print_results(results, output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing with open(output_file, 'w') as f: try: map(lambda item: map(lambda t: f.write('{0} {1} {2}\n'.format( replace_accented(item[0]), replace_accented(t[0]), replace_accented(unicode(t[1])))), item[1]), map(lambda result: (result[0], sorted(result[1], cmp)), sorted(results.items()))) except Exception as e: print e pass
def print_results(results ,output_file): ''' :param results: A dictionary with key = lexelt and value = a list of tuples (instance_id, label) :param output_file: file to write output ''' # implement your code here # don't forget to remove the accent of characters using main.replace_accented(input_str) # you should sort results on instance_id before printing f = codecs.open(output_file, encoding='utf-8', mode='w') for lexelt in results: for instance_id,label in results[lexelt]: f.write(replace_accented(lexelt + ' ' + instance_id + ' ' + label + '\n')) f.close()