#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
Reads the standard input,
converts the content into an object of class Analogies and
prints back the result on the standard output.
The input and the output should be exactly the same (if spaces are consistent...).

The format of the standard file should be as follows (Backus-Naur notation):

    <file> ::= [ <line> '\\n' ]*
    <line> ::= <string> ' : ' <string> ' :: ' <string> ' : x => x = ' <string> [ '\\t' <string> ]
    <string> ::= [ <character> ]*
    <character> is any character different from the colon (:).

If the 5th <sentence> is given,
    it is the reference for the solution of the analogy, and then
    the 4th <sentence> is the output given by some method used to solve the analogy.

Usage from the command line:
> cat file_of_analogies.txt | AnalogyLoader.py

Usage from a Python program:
from AnalogyLoader import Analogies

with open(filename) as f:
	nlgs = Analogies(f)
'''

import sys

################################################################################

__author__ = "WANG Liyan <wangliyan0905@toki.waseda.jp>"
__date__, __version__ = "16/04/2021", "1.0" # Creation.

################################################################################
class Analogy(list):
    '''
    Class for a list of 5 strings.
    Internally, the object is:
        [ string1, string2, string3, string4, string5 ]
    Externally, in a file, it should be one line in the following format:
        string1 : string2 :: string3 : x  =>  x = string4 \t string5
    Strings 1 to 4 should stand for an analogy,
        where string4 was output by some method to solve the analogy (hypothesis)
        and string5 is the correct answer (reference) of the analogy.
    '''

    _soln_sep = 'x  =>  x ='
    _nlg_fmt = '{} : {} :: {} : ' + _soln_sep + ' {}'

    def __init__(self, analogy=[]):
        list.__init__(self, analogy)

    @classmethod
    def fromFile(cls, line):
        line = line.strip('\n')
        if '\t' in line:
            analogy, d_ref = line.split('\t')
            d_ref = d_ref.strip()
        else:
            analogy, d_ref = line, None
        a, b, _, c, d = [ x.strip() for x in analogy.replace(cls._soln_sep, '').split(':') ]
        return cls(analogy=[a, b, c, d, d_ref])

    def __repr__(self):
        analogy = self._nlg_fmt.format(*self[:4])
        d_ref = '' if self[4] == None else f'\t{self[4]}'
        return analogy + d_ref

################################################################################
class Analogies(list):
    '''
    Class for a list of objects of class Analogy.
    '''

    def __init__(self, analogies=[]):
        list.__init__(self, analogies)

    @classmethod
    def fromFile(cls, file):
        return cls([Analogy.fromFile(line) for line in file])

    def __repr__(self):
        return '\n'.join(f'{line}' for line in self)

###############################################################################

def read_argv():
    import argparse
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    return parser.parse_args()

###############################################################################

if __name__ == '__main__':
    read_argv()
    print(Analogies.fromFile(sys.stdin))

