#!/usr/bin/python
'''
Copyright (c) 2011, phi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies, 
either expressed or implied, of the author.
'''
import sys

'''into the reducer we get a list of entries in one of two formats:
    a,b\t2   

    OR  

    a,b\ta,b,c.1
    a,b\ta,b,d.1
    ....

the first type is the frequency of that pattern in the corpus
the second type's value is the frequency of that VALUE's pattern in the corpus

since both types make it to the reducer we can take a,b\t2 AND a,b\ta,b,c.1 to get:
a,b IMPLIES a,b,c .5
in other words a,b => c 50%
'''

prevKey = None
arr = []  
# by using a second sort we can get rid of keeping content in @arr 
# for class association rule mining this should not be a big deal

freq = 0
for line in sys.stdin:
    line = line.strip().split('\t')

    if line[0] != prevKey: #the current line is a new key, done with old
        if prevKey != None:
            for item in arr:
                print "%s\t%s   %f" %(prevKey,item[0],float(item[1])/freq)
        arr = []
        prevKey = line[0]

    if '.' in line[1]:#this is apart of a rule
        conditional = line[1].split('.')
        arr.append(conditional)
    else:#this is the frequency of the key
        freq = float(line[1])
        
