From 6e4fb4d776c5236fc3e95d40bf2025756ffde681 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Thu, 5 Jun 2014 04:10:22 +0700 Subject: [PATCH] Update step 1 completed --- base.py | 14 +++++--- s1.py | 108 +++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 80 insertions(+), 42 deletions(-) diff --git a/base.py b/base.py index 229ac50..389c7d3 100644 --- a/base.py +++ b/base.py @@ -32,14 +32,15 @@ class Lo: 'H': '%s%s%s' % (r[10], r[22], r[27]), } for k, v in t.items(): - if v not in self.model[k]: self.model[k][v] = [] + if v not in self.model[k]: + self.model[k][v] = [] self.model[k][v].append(lo_id) self.los[lo_id] = t.copy() def get(self, lo_id): try: return self.los[lo_id] - except (IndexError, KeyError) as e: + except (IndexError, KeyError): return {} def get_lo_list(self): @@ -61,10 +62,13 @@ class Lo: return [] def get_mm_tuple(self, *args): - if len(args) > 1 or type(args) != type(()): return [] + if len(args) > 1 or not isinstance(args, tuple): + return [] r = [] for i in args[0]: - if len(i) != 2 or not i[1]: continue + if len(i) != 2 or not i[1]: + continue m = self.get_mm(i[0], i[1]) - if m: r.append(m) + if m: + r.append(m) return set.intersection(*[set(k) for k in r]) diff --git a/s1.py b/s1.py index 91d4b0e..d51a931 100755 --- a/s1.py +++ b/s1.py @@ -1,50 +1,84 @@ +# -*- coding:utf-8 -*- #!/usr/bin/env python from __future__ import print_function from base import Lo -import os, csv +import os +import csv +import sys los = Lo() -model_count = { - 'I': {}, 'P': {}, 'C': {}, 'M': {}, 'F': {}, - 'H': {}, 'S': {}, 'D': {}, 'F': {} -} -def get_rank_thing(filename): + +def get_rank_thing(filename, **kwargs): + model_count = { + 'I': {}, 'P': {}, 'C': {}, 'M': {}, 'F': {}, + 'H': {}, 'S': {}, 'D': {}, 'F': {} + } + output_filename = kwargs.get('output', '%s-output.csv' % filename) count = tot = lo_match = 0 - with open(os.path.join(os.getcwd(), 'raw', filename)) as f: - reader = csv.reader(f) - reader.next() - for r in reader: - mn, cnt = r[0], r[9] - q = ( - ('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]), - ('E', r[5]), ('F', r[6]), ('G', r[7]), ('H', r[8]), - ) - result = los.get_mm_tuple(q) - tot += 1 - lo_match += len(result) - if result: - # print(u'%s > %s' % (r[0], list(result))) - count += 1 - if cnt not in model_count[mn]: model_count[mn][cnt] = set() - model_count[mn][cnt] = model_count[mn][cnt].union(result) + with open(os.path.join(os.getcwd(), 'build', output_filename), 'wb') as o: + owriter = csv.writer(o, delimiter=',', quotechar='"', + quoting=csv.QUOTE_ALL) + with open(os.path.join(os.getcwd(), 'raw', filename)) as f: + reader = csv.reader(f) + reader.next() + for r in reader: + mn, cnt = r[0], r[9] + w = [r[10], r[11], r[12], r[13], r[14]] + q = ( + ('A', r[1]), ('B', r[2]), ('C', r[3]), ('D', r[4]), + ('E', r[5]), ('F', r[6]), ('G', r[7]), ('H', r[8]), + ) + result = los.get_mm_tuple(q) + tot += 1 + lo_match += len(result) + if result: + # print(u'%s > %s' % (r[0], list(result))) + count += 1 + owriter.writerow([','.join(list(result)), cnt] + w) + if cnt not in model_count[mn]: + model_count[mn][cnt] = set() + model_count[mn][cnt] = model_count[mn][cnt].union(result) print(u'[%s]\n Count = %s | Total = %s | Lo_w_match = %s' % ( - filename, count, tot, lo_match)) - -def main(): - fs = ('Case1_LS.csv', 'Case1_Gender.csv', 'Case1_Level.csv', 'Case1_SciF.csv') - for f in fs: - get_rank_thing(f) - - - for i, j in model_count.items(): - print('\n===== %s =====' % i) - for k, v in j.items(): - print('%s: \n' \ - ' %s' % (k, list(v))) + filename, count, tot, lo_match)) + return model_count +def main(*argv): + fs = ('Case1_LS.csv', 'Case1_Gender.csv', + 'Case1_Level.csv', 'Case1_SciF.csv') + result = ['IPC', 'MF', 'HS', 'FS'] + for f in xrange(0, len(fs)): + mc = get_rank_thing(fs[f]) + x = {} + for i in result[f]: + x[i] = mc[i] + # print('x: ', x) + mx = [set(j) for i, j in x.items()] + order = sorted(list(set.union(*mx)), reverse=True) + sum_file = '%s-result.csv' % fs[f] + with open(os.path.join(os.getcwd(), 'build', sum_file), 'wb') as o: + result_writer = csv.writer(o, delimiter=',', quotechar='"', + quoting=csv.QUOTE_ALL) + for i in xrange(0, 3): + result_writer.writerow(['count', order[i]]) + # print('count , ', order[i], ' / ', type(order[i])) + for j in x.keys(): + try: + # print(j, ', ', s) + lo_result = mc[j][order[i]] + # print(order[i], ' == ', len(lo_result)) + if i == 2 and len(mc[j]) > 3: + for _i in xrange(3, len(mc[j])): + lo_result = lo_result.union(mc[j][order[_i]]) + # print(' ==EX== ', len(lo_result)) + result_writer.writerow([j, ','.join(list(lo_result))]) + except KeyError: + print(' no key ', j, '[', order[i], ']') if __name__ == '__main__': - main() \ No newline at end of file + if len(sys.argv) > 1: + main(sys.argv[1:]) + else: + main()