def readPhraseSpan(spanFile, outFile, tgtFile):
    'Reads the input phrase span file for src & tgt sentences, alignment and initial phrases'

    global tight_phrases_only
    global ruleDict, ruleIndxCntDict, tgtCntDict
    global srcWrds, tgtWrds
    sent_count = 0
    phrLst = []

    print "Reading the span file :", spanFile
    inF = open(spanFile, 'r')
    for line in inF:
        line = line.strip()
        if line.startswith('LOG: SRC: '):             # Read the source sentence
            src = line.replace('LOG: SRC: ', '')
            srcWrds = src.split()
        elif line.startswith('LOG: TGT: '):           # Read the target sentence
            tgt = line.replace('LOG: TGT: ', '')
            tgtWrds = tgt.split()
        elif line.startswith('LOG: ALT: '):           # Read the source-target alignment
            align = line.replace('LOG: ALT: ', '')
            for align_pos in align.split():
                m = align_pos.split('-')
                try:                                  # Store forward alignments
                    alignDoD[m[0]][m[1]] = 1
                except KeyError:
                    alignDoD[m[0]] = {}
                    alignDoD[m[0]][m[1]] = 1
                try:                                  # Store reverse alignments
                    revAlignDoD[m[1]][m[0]] = 1
                except KeyError:
                    revAlignDoD[m[1]] = {}
                    revAlignDoD[m[1]][m[0]] = 1
        elif line.startswith('LOG: PHRASES_BEGIN:'): continue
        elif line.startswith('LOG: PHRASES_END:'):    # End of the current sentence; now extract rules from it
            xtractRules()

            # For every extracted rule call the function compFeatureCounts() to:
            #   i. convert the word positions in the rules into lexical entries, and
            #   ii. find the alignment for the rule and compute the joint count p(s, t)
            for rule in ruleDict.keys(): compFeatureCounts(rule)

            # Clear the variables at the end of current sentence
            alignDoD.clear()
            revAlignDoD.clear()
            ruleDict.clear()
            sentInitDoD.clear()
            sent_count += 1
            if sent_count % 1000 == 0:
                print "Sentences processed : %6d ..." % sent_count
        else:
            unaligned_edge = False
            phrLst = [ int(x) for x in line.split() ]

            # If the boundary term of source or target phrase has an unaligned word, ignore the phrase-pair
            # Earlier bug fixed on March '09
            # Unless the tight-phrase options is set to False
            if not alignDoD.has_key( str(phrLst[0]) ) or not revAlignDoD.has_key( str(phrLst[2]) ) or \
                 not alignDoD.has_key( str(phrLst[1]) ) or not revAlignDoD.has_key( str(phrLst[3]) ):
                if tight_phrases_only: continue
                else: unaligned_edge = True

            sphr_len = phrLst[0] + 1 - phrLst[1]      # Find length of source phrase and its span
            init_phr_pair = (' '.join( [str(x) for x in xrange(phrLst[0], phrLst[1]+1) ] ), \
                             ' '.join( [str(x) for x in xrange(phrLst[2], phrLst[3]+1)] ) )
            if unaligned_edge:
                ruleDict[init_phr_pair] = 1.0
                continue

            # Create a dict of dict for storing initial phrase pairs (tuples of source and target spans)
            if not sentInitDoD.has_key(sphr_len):
                sentInitDoD[sphr_len] = {}
            sentInitDoD[sphr_len][init_phr_pair] = 1

    inF.close()

    # Write the rule counts, forward and reverse alignments to files
    with open(outFile, 'w') as oF:
        for rule in sorted( ruleIndxCntDict.iterkeys() ):
            r_indx, rule_count = ruleIndxCntDict[rule]
            f_alignments = ' ## '.join( fAlignDoD[r_indx].keys() )
            r_alignments = ' ## '.join( rAlignDoD[r_indx].keys() )
            oF.write( "%s ||| %g ||| %s ||| %s\n" % (rule, rule_count, r_alignments, f_alignments) )

    with open(tgtFile, 'w') as tF:
        for tgt in sorted( tgtCntDict.iterkeys() ):
            tF.write( "%s ||| %g\n" % (tgt, tgtCntDict[tgt]) )

    return None
dataset/ETHPy150Open sfu-natlang/Kriya/src/Kriya-Grammar-Xtrctr/SCFGXtractor_ph1.py/readPhraseSpan/swapped operands of `-`