Example #1
0
    def __getRuleSpans(self, i, j, span_phrase):
        '''Get the list of rules that match the phrase corresponding to the given span'''

        global consObjsLst
        matchLst = PhraseTable.findConsistentRules(span_phrase)

        for match in matchLst:
            rule = match[0]
            ## Terminal rule
            if len(match[1]) == 0:
                consObjsLst.append( ConsequentRule(rule) )

            ## Hierarchical rule with 1 NT
            elif len(match[1]) == 2:
                span1 = (match[1][0]+i, match[1][1]+i)
                if not Parse.chartDict[span1].has_X_tree: continue

                if settings.opts.shallow_hiero and not self.relaxed_decoding:     # for Shallow-n hiero
                    x_level = -1
                    for x_level_status in Parse.chartDict[span1].getXLevelStats(self.sh_order):
                        x_level += 1
                        if not x_level_status: continue
                        consObjsLst.append( ConsequentRule(rule, x_level, span1, (), x_level) )
                elif not settings.opts.shallow_hiero or self.relaxed_decoding:    # for Full-hiero/ relaxed decoding
                    consObjsLst.append( ConsequentRule(rule, 0, span1) )

            ## Hierarchical rule with 2 NTs
            elif len(match[1]) == 4:
                span1 = (match[1][0]+i, match[1][1]+i)
                span2 = (match[1][2]+i, match[1][3]+i)
                if not Parse.chartDict[span1].has_X_tree or not Parse.chartDict[span2].has_X_tree: continue

                if settings.opts.shallow_hiero and not self.relaxed_decoding:     # for Shallow-n hiero
                    X1Levels = Parse.chartDict[span1].getXLevelStats(self.sh_order)
                    X2Levels = Parse.chartDict[span2].getXLevelStats(self.sh_order)

                    x1_level = -1
                    top_x2_level = len(X2Levels) - 1
                    if X2Levels[top_x2_level]:
                        for x1_level_status in X1Levels:
                            x1_level += 1
                            if not x1_level_status or x1_level > top_x2_level: continue
                            consObjsLst.append( ConsequentRule(rule, top_x2_level, span1, span2, x1_level, top_x2_level) )

                    x2_level = -1
                    top_x1_level = len(X1Levels) - 1
                    if X1Levels[top_x1_level]:
                        for x2_level_status in X2Levels:
                            x2_level += 1
                            if not x2_level_status or x2_level > top_x1_level: continue
                            consObjsLst.append( ConsequentRule(rule, top_x1_level, span1, span2, top_x1_level, x2_level) )
                elif not settings.opts.shallow_hiero or self.relaxed_decoding:    # for Full-hiero/ relaxed decoding
                    consObjsLst.append( ConsequentRule(rule, 0, span1, span2) )
Example #2
0
    def __getRuleSpans(self, i, j, span_phrase):
        '''Get the list of rules that match the phrase corresponding to the given span'''

        global consObjsLst
        matchLst = PhraseTable.findConsistentRules(span_phrase)

        for match in matchLst:
            rule = match[0]
            ## Terminal rule
            if len(match[1]) == 0:
                consObjsLst.append(ConsequentRule(rule))

            ## Hierarchical rule with 1 NT
            elif len(match[1]) == 2:
                span1 = (match[1][0] + i, match[1][1] + i)
                if not Parse.chartDict[span1].has_X_tree: continue

                if settings.opts.shallow_hiero and not self.relaxed_decoding:  # for Shallow-n hiero
                    x_level = -1
                    for x_level_status in Parse.chartDict[
                            span1].getXLevelStats(self.sh_order):
                        x_level += 1
                        if not x_level_status: continue
                        consObjsLst.append(
                            ConsequentRule(rule, x_level, span1, (), x_level))
                elif not settings.opts.shallow_hiero or self.relaxed_decoding:  # for Full-hiero/ relaxed decoding
                    consObjsLst.append(ConsequentRule(rule, 0, span1))

            ## Hierarchical rule with 2 NTs
            elif len(match[1]) == 4:
                span1 = (match[1][0] + i, match[1][1] + i)
                span2 = (match[1][2] + i, match[1][3] + i)
                if not Parse.chartDict[span1].has_X_tree or not Parse.chartDict[
                        span2].has_X_tree:
                    continue

                if settings.opts.shallow_hiero and not self.relaxed_decoding:  # for Shallow-n hiero
                    X1Levels = Parse.chartDict[span1].getXLevelStats(
                        self.sh_order)
                    X2Levels = Parse.chartDict[span2].getXLevelStats(
                        self.sh_order)

                    x1_level = -1
                    top_x2_level = len(X2Levels) - 1
                    if X2Levels[top_x2_level]:
                        for x1_level_status in X1Levels:
                            x1_level += 1
                            if not x1_level_status or x1_level > top_x2_level:
                                continue
                            consObjsLst.append(
                                ConsequentRule(rule, top_x2_level, span1,
                                               span2, x1_level, top_x2_level))

                    x2_level = -1
                    top_x1_level = len(X1Levels) - 1
                    if X1Levels[top_x1_level]:
                        for x2_level_status in X2Levels:
                            x2_level += 1
                            if not x2_level_status or x2_level > top_x1_level:
                                continue
                            consObjsLst.append(
                                ConsequentRule(rule, top_x1_level, span1,
                                               span2, top_x1_level, x2_level))
                elif not settings.opts.shallow_hiero or self.relaxed_decoding:  # for Full-hiero/ relaxed decoding
                    consObjsLst.append(ConsequentRule(rule, 0, span1, span2))