def check4Subphrase(sphr_len):

    global tot_src_terms, X1_only, tot_rules_derived
    global ppairRulesSet
    ppairRulesDict = {}

    for phr_pair in sentInitDoD[sphr_len].keys():
        rule_cnt = 0.0
        tot_rules_derived = 0           # Clear the global variables for every phrase-pair
        ppairRulesSet.clear()
        ppairRulesDict.clear()

        # Constraint: Rules are limited to seven non-terminals and terminals on source side
        # Initial phrase pairs having less than TOT_TERMS (default 7) terms in source side are added to the set of rules
        if sphr_len <= tot_src_terms:
            tot_rules_derived += 1
            if ppairRulesDict.has_key(phr_pair): ppairRulesDict[phr_pair] += 1
            else: ppairRulesDict[phr_pair] = 1

        # Do a breadth-first search: extract all possible rules for the given initial phrase pair
        iterateInitPhrPairs(sphr_len, phr_pair)
        while ppairRulesSet:
            sub_phr_pair = ppairRulesSet.pop()
            sub_s_span, sub_t_span = sub_phr_pair
            sub_phr_slen = len( sub_s_span.split() )

            if sub_phr_slen <= tot_src_terms:
                if ppairRulesDict.has_key(sub_phr_pair): ppairRulesDict[sub_phr_pair] += 1
                else: ppairRulesDict[sub_phr_pair] = 1

            # Constraint-1: Check if the source span already has 2 nonterminals, then do not process it further
            if (X1_only and sub_s_span.find('X__1') != -1) or (sub_s_span.find('X__2') != -1):
                pass
            # If the source span has 1 non-terminal, but further simplification is not possible
            elif sub_s_span.find('X__1') != -1 and not isRuleDecomposable( sub_s_span ):
                pass
            else:
                iterateInitPhrPairs(sub_phr_slen, sub_phr_pair)

        # Distribute the unit count for each initial phrase-pair equally among the rules extracted from it
        if tot_rules_derived > 0: rule_cnt = tot_rules_derived/ 1.0

        # Weight each rule by the number of times it was observed
        for rule in ppairRulesDict.keys():
            weighted_cnt = rule_cnt
            if weight_rules: weighted_cnt *= ppairRulesDict[rule]
            if ruleDict.has_key(rule): ruleDict[rule] += weighted_cnt
            else: ruleDict[rule] = weighted_cnt
            #sys.stderr.write('    %s ||| %s ||| %g ||| %d\n' % (rule[0], rule[1], ruleDict[rule], ppairRulesDict[rule]))
dataset/ETHPy150Open sfu-natlang/Kriya/src/Kriya-Grammar-Xtrctr/SCFGXtractor_ph1.py/check4Subphrase/swapped operands of `/`