Advertisement
overwater

Untitled

Apr 12th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. class ForContest(object):
  2.     def __init__(self, d=0):
  3.         self.d = d
  4.  
  5.     def parse_for_contest(self, str):
  6.         punctuation = "!\"#$%&()*+ ,-./:;<=>?@[\]^_`{|}~"
  7.         tokens = []
  8.         token = ''
  9.         number_token = ''
  10.         for symb in str.strip():
  11.             if symb not in punctuation:
  12.                 if symb in string.digits:
  13.                     if token != '':
  14.                         tokens.append(token)
  15.                         token = ''
  16.                     number_token += symb
  17.                 else:
  18.                     if number_token != '':
  19.                         tokens.append(number_token)
  20.                         number_token = ''
  21.                     token += symb
  22.             else:
  23.                 if number_token != '':
  24.                     tokens.append(number_token)
  25.                     number_token = ''
  26.                 if token != '':
  27.                     tokens.append(token)
  28.                     token = ''
  29.                 tokens.append(symb)
  30.         if number_token != '':
  31.             tokens.append(number_token)
  32.         if token != '':
  33.             tokens.append(token)
  34.         return tokens
  35.  
  36.     def chains_for_contest(self):
  37.         chains = [[], []]
  38.         text_lines = sys.stdin.readlines()
  39.         punctuation = "!\"#$%&()*+ ,-./:;<=>?@[\]^_`{|}~"
  40.         counter = collections.Counter([word for line in text_lines
  41.                                        for word in self.parse_for_contest(line)
  42.                                        if word[0] not in punctuation + string.digits])
  43.         chains[0] = counter.items()
  44.  
  45.         for i in xrange(1, self.d + 1):
  46.             dic = defaultdict(lambda: defaultdict(int))
  47.             for line in text_lines:
  48.                 all_tokens = [token for token in self.parse_for_contest(line)
  49.                               if token[0] not in punctuation + string.digits]
  50.                 for k in xrange(len(all_tokens) - i):
  51.                     dic[tuple(all_tokens[k: k + i])][all_tokens[k + i]] += 1
  52.             chains[1] += [(key, collections.Counter(value)) for (key, value) in dic.items()]
  53.  
  54.         answer = ['']
  55.         s = sum([pair[1] for pair in chains[0]])
  56.         for pair in sorted(chains[0], key=lambda x: x[0]):
  57.             answer += '  %s: %.2f' % (pair[0], pair[1] * 1.0 / s)
  58.  
  59.         for chain in chains[1:]:
  60.             for (word, probs) in sorted(chain, key=lambda x: x[0]):
  61.                 answer += ' '.join(word)
  62.                 s = sum([pair[1] for pair in probs.items()])
  63.                 for pair in sorted(probs.items(), key=lambda x: x[0]):
  64.                     answer += '  %s: %.2f' % (pair[0], pair[1] * 1.0 / s)
  65.         return answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement