duire.py (1006B)
1 #!/usr/bin/python3 -O 2 # -*- encoding: utf-8 3 4 import operator 5 import sys 6 7 # no "broken pipe" errors 8 # http://stackoverflow.com/a/16865106 9 from signal import signal, SIGPIPE, SIG_DFL 10 signal(SIGPIPE,SIG_DFL) 11 12 prefixes = {} 13 words = set() 14 interesting = {} 15 exp = 0.2 16 17 fwords = open(sys.argv[1]) 18 for l in fwords.readlines(): 19 words.add(l.rstrip()) 20 fwords.close() 21 fpref = open(sys.argv[2]) 22 for l in fpref.readlines(): 23 t = l.rstrip().split(' ') 24 prefixes[t[0]] = int(t[1]) 25 fpref.close() 26 27 for w in words: 28 for p, v in prefixes.items(): 29 if w.startswith(p): 30 w2 = w[len(p):] 31 if w2 in words: 32 continue 33 if w2 not in interesting.keys(): 34 interesting[w2] = set() 35 interesting[w2].add((w, v)) 36 37 sortint = sorted(interesting.items(), 38 key=lambda x: -sum((t[1]**exp for t in x[1]), 0)) 39 40 for (wi, ws) in sortint: 41 ws = sorted(ws, key=operator.itemgetter(1), reverse=True) 42 print(wi+":", ' '.join(t[0] for t in ws)) 43