#!/usr/bin/python
# Encoding: utf8

# INPUT: non-zero matrix (filtered)
# ARGUMENT 1: word1
# ARGUMENT 2: word2
# OUTPUT: all contexts shared by both words (number of shared contexts and frequency of both words)


import sys
import re
import math
from collections import defaultdict

w1 = sys.argv[1]
w2 = sys.argv[2]

w = defaultdict(dict)

w1_f=0
w2_f=0
for line in sys.stdin:
    line = line.strip()  ## chomp $line
    line = line.split()
    if len(line) >= 3:
        word = line[0]
        context = line[1]
        fr =  float (line[2])

        if word == w1 or word == w2:
            w[word][context] = fr
        if word == w1:
            w1_f += 1
        if word == w2:
            w2_f += 1



count=0
for c in w[w1]:
    if c in w[w2]:
        print '%s\t%s\t%s\t%.3f\t%.3f' % (w1, w2, c, w[w1][c],w[w2][c])
        count += 1
print "shared contexts = ", count, ", ", w1, " = ", w1_f, ", ", w2, " = ", w2_f
